|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Neomerx\JsonApi\Representation; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015-2019 [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
|
|
|
use Neomerx\JsonApi\Contracts\Parser\PositionInterface; |
|
22
|
|
|
use Neomerx\JsonApi\Contracts\Parser\ResourceInterface; |
|
23
|
|
|
use Neomerx\JsonApi\Contracts\Representation\FieldSetFilterInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @package Neomerx\JsonApi |
|
27
|
|
|
*/ |
|
28
|
|
|
class FieldSetFilter implements FieldSetFilterInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $fieldSets; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param array|null $fieldSets |
|
37
|
|
|
*/ |
|
38
|
71 |
|
public function __construct(array $fieldSets) |
|
39
|
|
|
{ |
|
40
|
71 |
|
$this->fieldSets = []; |
|
41
|
|
|
|
|
42
|
71 |
|
foreach ($fieldSets as $type => $fields) { |
|
43
|
12 |
|
assert(is_string($type) === true && empty($type) === false); |
|
44
|
12 |
|
assert(is_iterable($fields) === true); |
|
45
|
|
|
|
|
46
|
12 |
|
$this->fieldSets[$type] = []; |
|
47
|
|
|
|
|
48
|
12 |
|
foreach ($fields as $field) { |
|
49
|
12 |
|
assert(is_string($field) === true && empty($field) === false); |
|
50
|
12 |
|
assert(isset($this->fieldSets[$type][$field]) === false); |
|
51
|
|
|
|
|
52
|
12 |
|
$this->fieldSets[$type][$field] = true; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
71 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @inheritdoc |
|
59
|
|
|
*/ |
|
60
|
57 |
|
public function getAttributes(ResourceInterface $resource): iterable |
|
61
|
|
|
{ |
|
62
|
57 |
|
yield from $this->filterFields($resource->getType(), $resource->getAttributes()); |
|
63
|
57 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @inheritdoc |
|
67
|
|
|
*/ |
|
68
|
57 |
|
public function getRelationships(ResourceInterface $resource): iterable |
|
69
|
|
|
{ |
|
70
|
57 |
|
yield from $this->filterFields($resource->getType(), $resource->getRelationships()); |
|
71
|
57 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritdoc |
|
75
|
|
|
*/ |
|
76
|
21 |
|
public function shouldOutputRelationship(PositionInterface $position): bool |
|
77
|
|
|
{ |
|
78
|
21 |
|
$parentType = $position->getParentType(); |
|
79
|
21 |
|
if ($this->hasFilter($parentType) === true) { |
|
80
|
7 |
|
return array_key_exists($position->getParentRelationship(), $this->getAllowedFields($parentType)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
17 |
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
11 |
|
protected function getFieldSets(): array |
|
90
|
|
|
{ |
|
91
|
11 |
|
return $this->fieldSets; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $type |
|
96
|
|
|
* |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
57 |
|
protected function hasFilter(string $type): bool |
|
100
|
|
|
{ |
|
101
|
57 |
|
return array_key_exists($type, $this->fieldSets) === true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $type |
|
106
|
|
|
* |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
11 |
|
protected function getAllowedFields(string $type): array |
|
110
|
|
|
{ |
|
111
|
11 |
|
assert($this->hasFilter($type) === true); |
|
112
|
|
|
|
|
113
|
11 |
|
return $this->getFieldSets()[$type]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $type |
|
118
|
|
|
* @param iterable $fields |
|
119
|
|
|
* |
|
120
|
|
|
* @return iterable |
|
121
|
|
|
*/ |
|
122
|
56 |
|
protected function filterFields(string $type, iterable $fields): iterable |
|
123
|
|
|
{ |
|
124
|
56 |
|
if ($this->hasFilter($type) === false) { |
|
125
|
49 |
|
yield from $fields; |
|
126
|
|
|
|
|
127
|
49 |
|
return; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
10 |
|
$allowedFields = $this->getAllowedFields($type); |
|
131
|
10 |
|
foreach ($fields as $name => $value) { |
|
132
|
10 |
|
if (array_key_exists($name, $allowedFields) === true) { |
|
133
|
10 |
|
yield $name => $value; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
10 |
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|