Total Complexity | 12 |
Total Lines | 75 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
13 | trait SerializableTrait |
||
14 | { |
||
15 | /** |
||
16 | * @var array List of attribute names which should be stored in serialized form |
||
17 | * |
||
18 | * protected $serializable = []; |
||
19 | */ |
||
20 | |||
21 | |||
22 | /** |
||
23 | * The custom Carbon JSON serializer. |
||
24 | * |
||
25 | * @var callable|null |
||
26 | */ |
||
27 | protected static $serializer; |
||
28 | |||
29 | public function __serialize(): array |
||
30 | { |
||
31 | $properties = $this->__sleep(); |
||
32 | $data = []; |
||
33 | foreach ($properties as $property) { |
||
34 | $data[$property] = $this->{$property}; |
||
35 | } |
||
36 | return $data; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @internal |
||
41 | */ |
||
42 | public function serialize(): string |
||
43 | { |
||
44 | return serialize($this->__serialize()); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param callable $callback |
||
49 | * @return void |
||
50 | */ |
||
51 | public static function serializeUsing($callback) |
||
52 | { |
||
53 | static::$serializer = $callback; |
||
54 | } |
||
55 | |||
56 | public function __unserialize(array $data): void |
||
57 | { |
||
58 | foreach ($data as $property => $value) { |
||
59 | $this->{$property} = $value; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param $data |
||
65 | */ |
||
66 | public function unserialize($data) |
||
67 | { |
||
68 | $data = @unserialize($data); |
||
69 | if (!is_array($data)) { |
||
70 | return; |
||
71 | } |
||
72 | |||
73 | $this->__unserialize($data); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return array |
||
78 | */ |
||
79 | public function __sleep() |
||
82 | } |
||
83 | |||
84 | public function __wakeup() |
||
85 | { |
||
86 | if (get_parent_class() && method_exists(parent::class, '__wakeup')) { |
||
88 | } |
||
89 | } |
||
91 |