Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix spl types #29

Merged
merged 2 commits into from
Sep 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/JsonSerializer/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ protected function serializeObject($value)

$paramsToSerialize = $this->getObjectProperties($ref, $value);
$data = array(static::CLASS_IDENTIFIER_KEY => $className);

if($value instanceof \SplDoublyLinkedList){
return $data + array('value' => $value->serialize());
}

$data += array_map(array($this, 'serializeData'), $this->extractObjectData($value, $ref, $paramsToSerialize));
return $data;
}
Expand Down Expand Up @@ -406,8 +411,19 @@ protected function unserializeObject($value)
return $obj;
}

$ref = new ReflectionClass($className);
$obj = $ref->newInstanceWithoutConstructor();
if (!$this->isSplList($className)) {
$ref = new ReflectionClass($className);
$obj = $ref->newInstanceWithoutConstructor();
} else {
$obj = new $className();
}

if ($obj instanceof \SplDoublyLinkedList ) {
$obj->unserialize($value['value']);
$this->objectMapping[$this->objectMappingIndex++] = $obj;
return $obj;
}

$this->objectMapping[$this->objectMappingIndex++] = $obj;
foreach ($value as $property => $propertyValue) {
try {
Expand All @@ -424,6 +440,14 @@ protected function unserializeObject($value)
return $obj;
}

/**
* @return boolean
*/
protected function isSplList($className)
{
return in_array($className, array('SplQueue', 'SplDoublyLinkedList', 'SplStack'));
}

protected function restoreUsingUnserialize($className, $attributes)
{
$obj = (object)$attributes;
Expand Down
14 changes: 14 additions & 0 deletions tests/JsonSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,4 +522,18 @@ public function testNamespaceRename()
$this->setExpectedException('Zumba\Exception\JsonSerializerException');
$this->serializer->serialize($f);
}

/**
* Test serialization of SplDoubleLinkedList
*
* @return void
*/
public function testSerializationOfSplDoublyLinkedList()
{
$list = new \SplDoublyLinkedList();
$list->push('fizz');
$list->push(42);
$unserialized = $this->serializer->unserialize($this->serializer->serialize($list));
$this->assertTrue($list->serialize() === $unserialized->serialize());
}
}