Sha256: 66586547d335c847e26c94de9d319294211353b59308863a82c7338c9c405d0c

Contents?: true

Size: 877 Bytes

Versions: 9

Compression:

Stored size: 877 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 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

9 entries across 9 versions & 1 rubygems

Version Path
firebug-0.1.10 lib/firebug/serializer.rb
firebug-0.1.9 lib/firebug/serializer.rb
firebug-0.1.8 lib/firebug/serializer.rb
firebug-0.1.7 lib/firebug/serializer.rb
firebug-0.1.6 lib/firebug/serializer.rb
firebug-0.1.5 lib/firebug/serializer.rb
firebug-0.1.4 lib/firebug/serializer.rb
firebug-0.1.3 lib/firebug/serializer.rb
firebug-0.1.2 lib/firebug/serializer.rb