Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
The previous commit failed the test.
  • Loading branch information
kaorukobo committed Feb 22, 2024
1 parent e6a0069 commit 6de2c48
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
11 changes: 6 additions & 5 deletions lib/php_serialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PhpObject < OpenStruct
attr_accessor :_php_classname

def to_assoc
each_pair
each_pair.to_a
end
end

Expand Down Expand Up @@ -90,8 +90,8 @@ def PHP.serialize(var, assoc = false) # {{{
if var.respond_to?(:to_assoc)
v = var.to_assoc
# encode as Object with same name
class_name = var&._php_classname || var.class.to_s
s << "O:#{class_name.bytesize}:\"#{class_name.downcase}\":#{v.length}:{"
class_name = var.respond_to?(:_php_classname) ? var._php_classname : var.class.to_s.downcase
s << "O:#{class_name.bytesize}:\"#{class_name}\":#{v.length}:{"
v.each do |k,v|
s << "#{PHP.serialize(k.to_s, assoc)}#{PHP.serialize(v, assoc)}"
end
Expand Down Expand Up @@ -221,7 +221,8 @@ def PHP.do_unserialize(string, classmap, assoc)
when 'O' # object, O:length:"class":length:{[attribute][value]...}
# class name (lowercase in PHP, grr)
len = string.read_until(':').to_i + 3 # quotes, seperator
klass = string.read(len)[1...-2].capitalize.intern # read it, kill useless quotes
klass_in_php = string.read(len)[1...-2]
klass = klass_in_php.capitalize.intern # read it, kill useless quotes

# read the attributes
attrs = []
Expand All @@ -247,7 +248,7 @@ def PHP.do_unserialize(string, classmap, assoc)
val = val.new
rescue NameError # Nope; make a new PhpObject
val = PhpObject.new.tap { |php_obj|
php_obj._php_classname = klass.to_s
php_obj._php_classname = klass_in_php.to_s
}
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/php_serialize_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ def test_sessions
def test_creates_php_object_instance_if_class_undefined
assert_nothing_raised do
phps = 'O:8:"stdClass":2:{s:3:"url";s:17:"/legacy/index.php";s:8:"dateTime";s:19:"2012-10-24 22:29:13";}'
serialized = PHP.unserialize(phps)
unserialized = PHP.unserialize(phps)

assert_kind_of PHP::PhpObject, serialized
assert_equal "/legacy/index.php", serialized.url
assert_kind_of PHP::PhpObject, unserialized
assert_equal "/legacy/index.php", unserialized.url

reserialized = PHP.serialize(phps)
reserialized = PHP.serialize(unserialized)
assert_equal phps, reserialized
end
end
Expand Down

0 comments on commit 6de2c48

Please sign in to comment.