Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.00% covered (warning)
70.00%
14 / 20
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ChainedFilter
70.00% covered (warning)
70.00%
14 / 20
33.33% covered (danger)
33.33%
1 / 3
11.19
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isFiltered
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 followField
66.67% covered (warning)
66.67%
10 / 15
0.00% covered (danger)
0.00%
0 / 1
5.93
1<?php
2namespace Apie\Serializer\FieldFilters;
3
4final class ChainedFilter implements FieldFilterInterface
5{
6    /** @var array<int, FieldFilterInterface> */
7    private array $filters;
8
9    public function __construct(FieldFilterInterface... $filters)
10    {
11        $this->filters = $filters;
12    }
13    public function isFiltered(string $fieldName): bool
14    {
15        foreach ($this->filters as $filter) {
16            if ($filter->isFiltered($fieldName)) {
17                return true;
18            }
19        }
20
21        return false;
22    }
23
24    public function followField(string $fieldName): FieldFilterInterface
25    {
26        $list = [];
27        foreach ($this->filters as $filter) {
28            if ($filter->isFiltered($fieldName)) {
29                $list[] = $this->followField($fieldName);
30            }
31        }
32
33        $list = array_filter(
34            $list,
35            function (FieldFilterInterface $filter) {
36                return !($filter instanceof NoFiltering);
37            }
38        );
39
40        if (empty($list)) {
41            return new NoFiltering();
42        }
43        if (count($list) === 1) {
44            return reset($list);
45        }
46        return new ChainedFilter(...$list);
47    }
48}