1 | <?php |
||
24 | final class Excluder |
||
25 | { |
||
26 | /** |
||
27 | * Version, if set, will be used with [@see Since] and [@see Until] annotations |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | private $version; |
||
32 | |||
33 | /** |
||
34 | * Which modifiers are excluded |
||
35 | * |
||
36 | * By default only static properties are excluded |
||
37 | * |
||
38 | * @var int |
||
39 | */ |
||
40 | private $excludedModifiers = ReflectionProperty::IS_STATIC; |
||
41 | |||
42 | /** |
||
43 | * If this is true, properties will need to explicitly have an [@see Expose] annotation |
||
44 | * to be serialized or deserialized |
||
45 | * |
||
46 | * @var bool |
||
47 | */ |
||
48 | private $requireExpose = false; |
||
49 | |||
50 | /** |
||
51 | * Exclusions strategies during serialization |
||
52 | * |
||
53 | * @var ExclusionStrategy[] |
||
54 | */ |
||
55 | private static $serializationStrategies = []; |
||
56 | |||
57 | /** |
||
58 | * Exclusion strategies during deserialization |
||
59 | * |
||
60 | * @var ExclusionStrategy[] |
||
61 | */ |
||
62 | private static $deserializationStrategies = []; |
||
63 | |||
64 | /** |
||
65 | * Constructor |
||
66 | */ |
||
67 | 31 | public function __construct() |
|
72 | |||
73 | |||
74 | /** |
||
75 | * Set the version to test against |
||
76 | * |
||
77 | * @param string $version |
||
78 | * @return Excluder |
||
79 | */ |
||
80 | 12 | public function setVersion(?string $version): Excluder |
|
86 | |||
87 | /** |
||
88 | * Set an integer representing the property modifiers that should be excluded |
||
89 | * |
||
90 | * @param int $modifiers |
||
91 | * @return Excluder |
||
92 | */ |
||
93 | 2 | public function setExcludedModifiers(int $modifiers): Excluder |
|
99 | |||
100 | /** |
||
101 | * Require the [@see Expose] annotation to serialize properties |
||
102 | * |
||
103 | * @param bool $requireExpose |
||
104 | * @return Excluder |
||
105 | */ |
||
106 | 5 | public function setRequireExpose(bool $requireExpose): Excluder |
|
112 | |||
113 | /** |
||
114 | * Add an exclusion strategy and specify if it should be used during serialization or deserialization |
||
115 | * |
||
116 | * @param ExclusionStrategy $strategy |
||
117 | * @param bool $serialization |
||
118 | * @param bool $deserialization |
||
119 | */ |
||
120 | 3 | public static function addExclusionStrategy(ExclusionStrategy $strategy, bool $serialization, bool $deserialization) |
|
130 | |||
131 | /** |
||
132 | * Returns true if we should exclude the class for a given serialization direction |
||
133 | * |
||
134 | * @param ClassMetadata $classMetadata |
||
135 | * @param bool $serialize |
||
136 | * @return bool |
||
137 | * @throws \InvalidArgumentException If the type does not exist |
||
138 | */ |
||
139 | 8 | public function excludeClass(ClassMetadata $classMetadata, bool $serialize): bool |
|
143 | |||
144 | /** |
||
145 | * @param ClassMetadata $classMetadata |
||
146 | * @param bool $serialize |
||
147 | * @return bool |
||
148 | */ |
||
149 | 2 | public static function excludeClassByStrategy(ClassMetadata $classMetadata, bool $serialize): bool |
|
160 | |||
161 | /** |
||
162 | * Returns true if we should exclude the class for a given serialization direction |
||
163 | * |
||
164 | * @param PropertyMetadata $propertyMetadata |
||
165 | * @param bool $serialize |
||
166 | * @return bool |
||
167 | */ |
||
168 | 19 | public function excludeProperty(PropertyMetadata $propertyMetadata, bool $serialize): bool |
|
177 | |||
178 | /** |
||
179 | * Returns true if we should exclude the class for a given serialization direction |
||
180 | * |
||
181 | * Uses user-defined strategies |
||
182 | * |
||
183 | * @param PropertyMetadata $property |
||
184 | * @param bool $serialize |
||
185 | * @return bool |
||
186 | */ |
||
187 | 2 | public static function excludePropertyByStrategy(PropertyMetadata $property, bool $serialize): bool |
|
198 | |||
199 | /** |
||
200 | * Checks various annotations to see if the property should be excluded |
||
201 | * |
||
202 | * - [@see Since] / [@see Until] |
||
203 | * - [@see Exclude] |
||
204 | * - [@see Expose] (if requireExpose is set) |
||
205 | * |
||
206 | * @param AnnotationSet $annotations |
||
207 | * @param bool $serialize |
||
208 | * @param int $filter |
||
209 | * @return bool |
||
210 | */ |
||
211 | 24 | private function excludeByAnnotation(AnnotationSet $annotations, bool $serialize, int $filter): bool |
|
234 | |||
235 | /** |
||
236 | * Returns true if the set version is valid for [@see Since] and [@see Until] annotations |
||
237 | * |
||
238 | * @param AnnotationSet $annotations |
||
239 | * @param int $filter |
||
240 | * @return bool |
||
241 | */ |
||
242 | 24 | private function validVersion(AnnotationSet $annotations, int $filter): bool |
|
246 | |||
247 | /** |
||
248 | * Returns true if we should skip based on the [@see Since] annotation |
||
249 | * |
||
250 | * @param AnnotationSet $annotations |
||
251 | * @param int $filter |
||
252 | * @return bool |
||
253 | */ |
||
254 | 24 | private function shouldSkipSince(AnnotationSet $annotations, int $filter): bool |
|
264 | |||
265 | /** |
||
266 | * Returns true if we should skip based on the [@see Until] annotation |
||
267 | * |
||
268 | * @param AnnotationSet $annotations |
||
269 | * @param int $filter |
||
270 | * @return bool |
||
271 | */ |
||
272 | 22 | private function shouldSkipUntil(AnnotationSet $annotations, int $filter): bool |
|
282 | } |
||
283 |