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