Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.66% |
26 / 29 |
|
90.91% |
10 / 11 |
CRAP | |
0.00% |
0 / 1 |
| Entries | |
89.66% |
26 / 29 |
|
90.91% |
10 / 11 |
17.32 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| json | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| raw | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getIterator | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| first | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| last | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| prepareEntries | |
80.00% |
12 / 15 |
|
0.00% |
0 / 1 |
6.29 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Value; |
| 6 | |
| 7 | use Cosray\Exception\RuntimeException; |
| 8 | use Cosray\Field; |
| 9 | use Generator; |
| 10 | use IteratorAggregate; |
| 11 | use Override; |
| 12 | |
| 13 | /** |
| 14 | * @property-read Field\Entries $field |
| 15 | * @implements IteratorAggregate<int, Entry> |
| 16 | */ |
| 17 | class Entries extends Value implements IteratorAggregate |
| 18 | { |
| 19 | /** @var list<Entry> */ |
| 20 | protected array $entries = []; |
| 21 | |
| 22 | public function __construct( |
| 23 | Field\Owner $owner, |
| 24 | Field\Entries $field, |
| 25 | ValueContext $context, |
| 26 | ) { |
| 27 | parent::__construct($owner, $field, $context); |
| 28 | |
| 29 | $this->prepareEntries(); |
| 30 | } |
| 31 | |
| 32 | #[Override] |
| 33 | public function __toString(): string |
| 34 | { |
| 35 | throw new RuntimeException( |
| 36 | "The entries field '{$this->fieldName}' has no string representation. Iterate its rows in the template.", |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | /** @return list<array{uid: ?string, type: string, fields: array<string, mixed>}> */ |
| 41 | #[Override] |
| 42 | public function json(): array |
| 43 | { |
| 44 | return array_map(static fn(Entry $entry): array => $entry->json(), $this->entries); |
| 45 | } |
| 46 | |
| 47 | /** @return list<array{uid: ?string, type: string, fields: array<string, mixed>}> */ |
| 48 | #[Override] |
| 49 | public function raw(): array |
| 50 | { |
| 51 | return array_map(static fn(Entry $entry): array => $entry->raw(), $this->entries); |
| 52 | } |
| 53 | |
| 54 | /** @return Generator<int, Entry> */ |
| 55 | #[Override] |
| 56 | public function getIterator(): Generator |
| 57 | { |
| 58 | foreach ($this->entries as $entry) { |
| 59 | yield $entry; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public function count(): int |
| 64 | { |
| 65 | return count($this->entries); |
| 66 | } |
| 67 | |
| 68 | public function first(): ?Entry |
| 69 | { |
| 70 | return $this->entries[0] ?? null; |
| 71 | } |
| 72 | |
| 73 | public function last(): ?Entry |
| 74 | { |
| 75 | return $this->entries[count($this->entries) - 1] ?? null; |
| 76 | } |
| 77 | |
| 78 | public function get(int $index): ?Entry |
| 79 | { |
| 80 | return $this->entries[$index] ?? null; |
| 81 | } |
| 82 | |
| 83 | #[Override] |
| 84 | public function isset(): bool |
| 85 | { |
| 86 | return count($this->entries) > 0; |
| 87 | } |
| 88 | |
| 89 | protected function prepareEntries(): void |
| 90 | { |
| 91 | $data = $this->data['value'][Field\Field::NEUTRAL_LOCALE] ?? []; |
| 92 | |
| 93 | if (!is_array($data)) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | foreach ($data as $entryData) { |
| 98 | if (!is_array($entryData)) { |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | $type = $entryData['type'] ?? null; |
| 103 | |
| 104 | if (!is_string($type) || !$this->field->allows($type)) { |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | $this->entries[] = new Entry( |
| 109 | $this->owner, |
| 110 | $this->field, |
| 111 | new ValueContext($this->fieldName, $entryData), |
| 112 | $type, |
| 113 | ); |
| 114 | } |
| 115 | } |
| 116 | } |