-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectSelfSerializer.php
More file actions
46 lines (44 loc) · 1.4 KB
/
ObjectSelfSerializer.php
File metadata and controls
46 lines (44 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
namespace SmoothPhp\Serialization;
use SmoothPhp\Contracts\Serialization\Serializer;
use SmoothPhp\Contracts\Serialization\Serializable;
use SmoothPhp\Serialization\Exception\SerializationException;
/**
* Class ObjectSelfSerializer
* @package SmoothPhp\Serialization
* @author Simon Bennett <simon@bennett.im>
*/
final class ObjectSelfSerializer implements Serializer
{
/**
* {@inheritDoc}
*/
public function serialize($object)
{
if (! $object instanceof Serializable) {
throw new SerializationException(sprintf(
'Object \'%s\' does not implement Serializable',
get_class($object)
));
}
return array(
'class' => get_class($object),
'payload' => $object->serialize()
);
}
/**
* {@inheritDoc}
*/
public function deserialize(array $serializedObject)
{
if (! in_array(Serializable::class, class_implements($serializedObject['class']))) {
throw new SerializationException(
sprintf(
'Class \'%s\' does not implement Serializable',
$serializedObject['class']
)
);
}
return $serializedObject['class']::deserialize($serializedObject['payload']);
}
}