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 | |||
| unwrap | |
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 | |
| 12 | /** |
| 13 | * @property-read Field\Entries $field |
| 14 | */ |
| 15 | class Entries extends Value implements IteratorAggregate |
| 16 | { |
| 17 | /** @var list<Entry> */ |
| 18 | protected array $entries = []; |
| 19 | |
| 20 | public function __construct( |
| 21 | Field\Owner $owner, |
| 22 | Field\Entries $field, |
| 23 | ValueContext $context, |
| 24 | ) { |
| 25 | parent::__construct($owner, $field, $context); |
| 26 | |
| 27 | $this->prepareEntries(); |
| 28 | } |
| 29 | |
| 30 | public function __toString(): string |
| 31 | { |
| 32 | throw new RuntimeException( |
| 33 | "The entries field '{$this->fieldName}' has no string representation. Iterate its rows in the template.", |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | public function json(): array |
| 38 | { |
| 39 | return array_map(static fn(Entry $entry): array => $entry->json(), $this->entries); |
| 40 | } |
| 41 | |
| 42 | public function unwrap(): array |
| 43 | { |
| 44 | return array_map(static fn(Entry $entry): array => $entry->unwrap(), $this->entries); |
| 45 | } |
| 46 | |
| 47 | public function getIterator(): Generator |
| 48 | { |
| 49 | foreach ($this->entries as $entry) { |
| 50 | yield $entry; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public function count(): int |
| 55 | { |
| 56 | return count($this->entries); |
| 57 | } |
| 58 | |
| 59 | public function first(): ?Entry |
| 60 | { |
| 61 | return $this->entries[0] ?? null; |
| 62 | } |
| 63 | |
| 64 | public function last(): ?Entry |
| 65 | { |
| 66 | return $this->entries[count($this->entries) - 1] ?? null; |
| 67 | } |
| 68 | |
| 69 | public function get(int $index): ?Entry |
| 70 | { |
| 71 | return $this->entries[$index] ?? null; |
| 72 | } |
| 73 | |
| 74 | public function isset(): bool |
| 75 | { |
| 76 | return count($this->entries) > 0; |
| 77 | } |
| 78 | |
| 79 | protected function prepareEntries(): void |
| 80 | { |
| 81 | $data = $this->data['value'][Field\Field::NEUTRAL_LOCALE] ?? []; |
| 82 | |
| 83 | if (!is_array($data)) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | foreach ($data as $entryData) { |
| 88 | if (!is_array($entryData)) { |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | $type = $entryData['type'] ?? null; |
| 93 | |
| 94 | if (!is_string($type) || !$this->field->allows($type)) { |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | $this->entries[] = new Entry( |
| 99 | $this->owner, |
| 100 | $this->field, |
| 101 | new ValueContext($this->fieldName, $entryData), |
| 102 | $type, |
| 103 | ); |
| 104 | } |
| 105 | } |
| 106 | } |