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