1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ICanBoogie package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ICanBoogie\Accessor; |
13
|
|
|
|
14
|
|
|
use ReflectionException; |
15
|
|
|
|
16
|
|
|
use function array_combine; |
17
|
|
|
use function array_keys; |
18
|
|
|
use function get_object_vars; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Improves serialization of objects, exporting façade properties and removing properties for |
22
|
|
|
* which lazy getters are defined. |
23
|
|
|
* |
24
|
|
|
* @see HasAccessor |
25
|
|
|
*/ |
26
|
|
|
trait SerializableTrait |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @throws ReflectionException |
30
|
|
|
*/ |
31
|
|
|
public function __sleep() |
32
|
|
|
{ |
33
|
|
|
return $this->accessor_sleep(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function __wakeup() |
37
|
|
|
{ |
38
|
|
|
$this->accessor_wakeup(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The method returns an array of key/key pairs. |
43
|
|
|
* |
44
|
|
|
* Properties for which a lazy getter is defined are discarded. For instance, if the property |
45
|
|
|
* `next` is defined and the class of the instance defines the getter `lazy_get_next()`, the |
46
|
|
|
* property is discarded. |
47
|
|
|
* |
48
|
|
|
* Note that façade properties are also included. |
49
|
|
|
* |
50
|
|
|
* @throws ReflectionException |
51
|
|
|
*/ |
52
|
|
|
private function accessor_sleep(): array |
53
|
|
|
{ |
54
|
|
|
$properties = array_keys(get_object_vars($this)); |
55
|
|
|
|
56
|
|
|
if ($properties) { |
|
|
|
|
57
|
|
|
$properties = array_combine($properties, $properties); |
58
|
|
|
|
59
|
|
|
foreach ($properties as $property) { |
60
|
|
|
if ( |
61
|
|
|
$this->has_method(static::accessor_format( |
|
|
|
|
62
|
|
|
$property, |
63
|
|
|
HasAccessor::ACCESSOR_TYPE_GETTER, |
64
|
|
|
HasAccessor::ACCESSOR_IS_LAZY |
65
|
|
|
)) |
66
|
|
|
) { |
67
|
|
|
unset($properties[$property]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
foreach (AccessorReflection::resolve_facade_properties($this) as $name => $property) { |
73
|
|
|
$properties[$name] = "\x00" . $property->class . "\x00" . $name; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $properties; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Unsets null properties for which a lazy getter is defined so that it is called when |
81
|
|
|
* the property is accessed. |
82
|
|
|
*/ |
83
|
|
|
private function accessor_wakeup(): void |
84
|
|
|
{ |
85
|
|
|
$properties = get_object_vars($this); |
86
|
|
|
|
87
|
|
|
foreach ($properties as $property => $value) { |
88
|
|
|
if ( |
89
|
|
|
$this->has_method(static::accessor_format( |
90
|
|
|
$property, |
91
|
|
|
HasAccessor::ACCESSOR_TYPE_GETTER, |
92
|
|
|
HasAccessor::ACCESSOR_IS_LAZY |
93
|
|
|
)) |
94
|
|
|
) { |
95
|
|
|
unset($this->$property); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.