1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JMS\Serializer\Exclusion; |
6
|
|
|
|
7
|
|
|
use JMS\Serializer\Context; |
8
|
|
|
use JMS\Serializer\Metadata\ClassMetadata; |
9
|
|
|
use JMS\Serializer\Metadata\PropertyMetadata; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Disjunct Exclusion Strategy. |
13
|
|
|
* |
14
|
|
|
* This strategy is short-circuiting and will skip a class, or property as soon as one of the delegates skips it. |
15
|
|
|
* |
16
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
final class DisjunctExclusionStrategy implements ExclusionStrategyInterface, ValueExclusionStrategyInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ExclusionStrategyInterface[] |
22
|
|
|
*/ |
23
|
|
|
private $delegates; |
24
|
|
|
|
25
|
6 |
|
/** |
26
|
|
|
* @param ExclusionStrategyInterface[] $delegates |
27
|
6 |
|
*/ |
28
|
6 |
|
public function __construct(array $delegates = []) |
29
|
|
|
{ |
30
|
|
|
$this->delegates = $delegates; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ExclusionStrategyInterface|ValueExclusionStrategyInterface $strategy |
35
|
|
|
*/ |
36
|
|
|
public function addStrategy($strategy): void |
37
|
|
|
{ |
38
|
|
|
if ( |
39
|
|
|
!($strategy instanceof ExclusionStrategyInterface) |
40
|
|
|
|| !($strategy instanceof ValueExclusionStrategyInterface) |
41
|
|
|
) { |
42
|
3 |
|
throw new \InvalidArgumentException( |
43
|
|
|
sprintf( |
44
|
3 |
|
'Strategy should be one of %s, %s instances', |
45
|
|
|
ExclusionStrategyInterface::class, |
46
|
3 |
|
ValueExclusionStrategyInterface::class |
47
|
3 |
|
) |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
$this->delegates[] = $strategy; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Whether the class should be skipped. |
56
|
|
|
*/ |
57
|
|
|
public function shouldSkipClass(ClassMetadata $metadata, Context $context): bool |
58
|
|
|
{ |
59
|
|
|
foreach ($this->delegates as $delegate) { |
60
|
|
|
if ($delegate instanceof ExclusionStrategyInterface && $delegate->shouldSkipClass($metadata, $context)) { |
61
|
3 |
|
return true; |
62
|
|
|
} |
63
|
3 |
|
} |
64
|
|
|
|
65
|
3 |
|
return false; |
66
|
3 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Whether the property should be skipped. |
70
|
1 |
|
*/ |
71
|
|
|
public function shouldSkipProperty(PropertyMetadata $property, Context $context): bool |
72
|
|
|
{ |
73
|
|
|
foreach ($this->delegates as $delegate) { |
74
|
|
|
if ($delegate instanceof ExclusionStrategyInterface && $delegate->shouldSkipProperty($property, $context)) { |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Whether the property should be skipped. |
84
|
|
|
*/ |
85
|
|
|
public function shouldSkipPropertyWithValue(PropertyMetadata $property, Context $context, $value): bool |
86
|
|
|
{ |
87
|
|
|
foreach ($this->delegates as $delegate) { |
88
|
|
|
if ($delegate instanceof ValueExclusionStrategyInterface && $delegate->shouldSkipPropertyWithValue($property, $context, $value)) { |
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|