Completed
Branch master (195c43)
by
unknown
04:51
created

FieldSetFilter::hasFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 isset($this->getAllowedFields($parentType)[$position->getParentRelationship()]);
81
        }
82
83 17
        return true;
84
    }
85
86
    /**
87
     * @param string $type
88
     *
89
     * @return bool
90
     */
91 57
    protected function hasFilter(string $type): bool
92
    {
93 57
        return isset($this->fieldSets[$type]) === true;
94
    }
95
96
    /**
97
     * @param string $type
98
     *
99
     * @return array
100
     */
101 11
    protected function getAllowedFields(string $type): array
102
    {
103 11
        \assert($this->hasFilter($type) === true);
104
105 11
        return $this->fieldSets[$type];
106
    }
107
108
    /**
109
     * @param string   $type
110
     * @param iterable $fields
111
     *
112
     * @return iterable
113
     */
114 56
    protected function filterFields(string $type, iterable $fields): iterable
115
    {
116 56
        if ($this->hasFilter($type) === false) {
117 49
            yield from $fields;
118
119 49
            return;
120
        }
121
122 10
        $allowedFields = $this->getAllowedFields($type);
123 10
        foreach ($fields as $name => $value) {
124 10
            if (isset($allowedFields[$name]) === true) {
125 10
                yield $name => $value;
126
            }
127
        }
128 10
    }
129
}
130