|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Copyright 2013 Johannes M. Schmitt <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace JMS\Serializer\Exclusion; |
|
22
|
|
|
|
|
23
|
|
|
use JMS\Serializer\Context; |
|
24
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
|
25
|
|
|
use JMS\Serializer\Metadata\ClassMetadata; |
|
26
|
|
|
use JMS\Serializer\Metadata\PropertyMetadata; |
|
27
|
|
|
|
|
28
|
|
|
class GroupsExclusionStrategy implements ExclusionStrategyInterface |
|
29
|
|
|
{ |
|
30
|
|
|
const DEFAULT_GROUP = 'Default'; |
|
31
|
|
|
|
|
32
|
|
|
private $groups = array(); |
|
33
|
|
|
|
|
34
|
33 |
|
public function __construct(array $groups) |
|
35
|
|
|
{ |
|
36
|
33 |
|
if (empty($groups)) { |
|
37
|
4 |
|
$groups = array(self::DEFAULT_GROUP); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
33 |
|
$this->groups = $groups; |
|
41
|
33 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritDoc} |
|
45
|
|
|
*/ |
|
46
|
15 |
|
public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext):bool |
|
47
|
|
|
{ |
|
48
|
15 |
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritDoc} |
|
53
|
|
|
*/ |
|
54
|
33 |
|
public function shouldSkipProperty(PropertyMetadata $property, Context $navigatorContext):bool |
|
55
|
|
|
{ |
|
56
|
33 |
|
$groups = $this->getGroupsFor($navigatorContext); |
|
57
|
|
|
|
|
58
|
33 |
|
if (!$property->groups) { |
|
59
|
11 |
|
return !in_array(self::DEFAULT_GROUP, $groups); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
31 |
|
return $this->shouldSkipUsingGroups($property, $groups); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
31 |
|
private function shouldSkipUsingGroups(PropertyMetadata $property, $groups) |
|
66
|
|
|
{ |
|
67
|
31 |
|
foreach ($property->groups as $group) { |
|
68
|
31 |
|
if (in_array($group, $groups)) { |
|
69
|
31 |
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
20 |
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
33 |
|
private function getGroupsFor(Context $navigatorContext) |
|
77
|
|
|
{ |
|
78
|
33 |
|
$paths = $navigatorContext->getCurrentPath(); |
|
79
|
|
|
|
|
80
|
33 |
|
$groups = $this->groups; |
|
81
|
33 |
|
foreach ($paths as $index => $path) { |
|
82
|
7 |
|
if (!array_key_exists($path, $groups)) { |
|
83
|
7 |
|
if ($index > 0) { |
|
84
|
2 |
|
$groups = array(self::DEFAULT_GROUP); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
7 |
|
break; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
if (!is_array($groups[$path])) { |
|
91
|
|
|
throw new RuntimeException(sprintf('The group value for the property path "%s" should be an array, "%s" given', $index, gettype($groups[$path]))); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
$groups = $groups[$path]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
33 |
|
return $groups; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|