1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright 2016 Johannes M. Schmitt <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace JMS\Serializer\Exclusion; |
20
|
|
|
|
21
|
|
|
use JMS\Serializer\Context; |
22
|
|
|
use JMS\Serializer\Metadata\ClassMetadata; |
23
|
|
|
use JMS\Serializer\Metadata\PropertyMetadata; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Disjunct Exclusion Strategy. |
27
|
|
|
* |
28
|
|
|
* This strategy is short-circuiting and will skip a class, or property as soon as one of the delegates skips it. |
29
|
|
|
* |
30
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class DisjunctExclusionStrategy implements ExclusionStrategyInterface |
33
|
|
|
{ |
34
|
|
|
private $delegates = array(); |
35
|
|
|
/** |
36
|
|
|
* @param ExclusionStrategyInterface[] $delegates |
37
|
|
|
*/ |
38
|
352 |
|
public function __construct(array $delegates = array()) |
39
|
|
|
{ |
40
|
352 |
|
$this->delegates = $delegates; |
41
|
352 |
|
} |
42
|
|
|
|
43
|
26 |
|
public function addStrategy(ExclusionStrategyInterface $strategy):void |
44
|
|
|
{ |
45
|
26 |
|
$this->delegates[] = $strategy; |
46
|
26 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Whether the class should be skipped. |
50
|
|
|
* |
51
|
|
|
* @param ClassMetadata $metadata |
52
|
|
|
* |
53
|
|
|
* @return boolean |
54
|
|
|
*/ |
55
|
173 |
|
public function shouldSkipClass(ClassMetadata $metadata, Context $context):bool |
56
|
|
|
{ |
57
|
173 |
|
foreach ($this->delegates as $delegate) { |
58
|
|
|
/** @var $delegate ExclusionStrategyInterface */ |
59
|
29 |
|
if ($delegate->shouldSkipClass($metadata, $context)) { |
60
|
29 |
|
return true; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
167 |
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Whether the property should be skipped. |
69
|
|
|
* |
70
|
|
|
* @param PropertyMetadata $property |
71
|
|
|
* |
72
|
|
|
* @return boolean |
73
|
|
|
*/ |
74
|
168 |
|
public function shouldSkipProperty(PropertyMetadata $property, Context $context):bool |
75
|
|
|
{ |
76
|
168 |
|
foreach ($this->delegates as $delegate) { |
77
|
|
|
/** @var $delegate ExclusionStrategyInterface */ |
78
|
25 |
|
if ($delegate->shouldSkipProperty($property, $context)) { |
79
|
25 |
|
return true; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
166 |
|
return false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|