Sha256: 8ac9f007661cf49becb7819f22a28d5d57f986c371c1abd1a3c9d461be58e416

Contents?: true

Size: 885 Bytes

Versions: 3

Compression:

Stored size: 885 Bytes

Contents

# frozen_string_literal: true

module Firebug
  class Serializer
    # Convert a ruby object into a PHP serialized string.
    #
    # @param [Object] obj
    # @raise [ArgumentError] for unsupported types
    # @return [String]
    def self.parse(obj) # rubocop:disable AbcSize,CyclomaticComplexity
      case obj
      when NilClass
        'N;'
      when TrueClass
        'b:1;'
      when FalseClass
        'b:0;'
      when Integer
        "i:#{obj};"
      when Float
        "d:#{obj};"
      when String, Symbol
        "s:#{obj.to_s.bytesize}:\"#{obj}\";"
      when Array
        "a:#{obj.length}:{#{obj.collect.with_index { |e, i| "#{parse(i)}#{parse(e)}" }.join}}"
      when Hash
        "a:#{obj.length}:{#{obj.collect { |k, v| "#{parse(k)}#{parse(v)}" }.join}}"
      else
        raise ArgumentError, "unsupported type #{obj.class.name}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
firebug-0.1.1 lib/firebug/serializer.rb
firebug-0.1.0 lib/firebug/serializer.rb
firebug-0.0.7 lib/firebug/serializer.rb