Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.48% |
19 / 21 |
|
83.33% |
10 / 12 |
CRAP | |
0.00% |
0 / 1 |
| Reference | |
90.48% |
19 / 21 |
|
83.33% |
10 / 12 |
19.31 | |
0.00% |
0 / 1 |
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| uids | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
8 | |||
| uid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| unwrap | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| json | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| rewind | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| current | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| key | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| next | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| valid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Value; |
| 6 | |
| 7 | use Cosray\Field\Field; |
| 8 | use Iterator; |
| 9 | |
| 10 | /** |
| 11 | * The stored value of a Reference field: an ordered, language-neutral |
| 12 | * list of target node uids. Resolution to node objects is a consumer |
| 13 | * concern (e.g. `$cms->node($uid)`); the value itself exposes the uids. |
| 14 | * |
| 15 | * @implements Iterator<int, string> |
| 16 | */ |
| 17 | class Reference extends Value implements Iterator |
| 18 | { |
| 19 | private int $pointer = 0; |
| 20 | |
| 21 | /** @var list<string>|null */ |
| 22 | private ?array $cache = null; |
| 23 | |
| 24 | public function __toString(): string |
| 25 | { |
| 26 | return $this->uid() ?? ''; |
| 27 | } |
| 28 | |
| 29 | /** @return list<string> */ |
| 30 | public function uids(): array |
| 31 | { |
| 32 | if ($this->cache !== null) { |
| 33 | return $this->cache; |
| 34 | } |
| 35 | |
| 36 | $value = $this->data['value'] ?? []; |
| 37 | $items = is_array($value) ? $value[Field::NEUTRAL_LOCALE] ?? [] : []; |
| 38 | $uids = []; |
| 39 | |
| 40 | foreach (is_array($items) ? $items : [] as $item) { |
| 41 | $uid = is_array($item) ? $item['uid'] ?? null : null; |
| 42 | |
| 43 | if (is_string($uid) && $uid !== '') { |
| 44 | $uids[] = $uid; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return $this->cache = $uids; |
| 49 | } |
| 50 | |
| 51 | public function uid(): ?string |
| 52 | { |
| 53 | return $this->uids()[0] ?? null; |
| 54 | } |
| 55 | |
| 56 | public function count(): int |
| 57 | { |
| 58 | return count($this->uids()); |
| 59 | } |
| 60 | |
| 61 | public function unwrap(): array |
| 62 | { |
| 63 | return $this->uids(); |
| 64 | } |
| 65 | |
| 66 | public function json(): mixed |
| 67 | { |
| 68 | return $this->data; |
| 69 | } |
| 70 | |
| 71 | public function isset(): bool |
| 72 | { |
| 73 | return $this->uids() !== []; |
| 74 | } |
| 75 | |
| 76 | public function rewind(): void |
| 77 | { |
| 78 | $this->pointer = 0; |
| 79 | } |
| 80 | |
| 81 | public function current(): string |
| 82 | { |
| 83 | return $this->uids()[$this->pointer]; |
| 84 | } |
| 85 | |
| 86 | public function key(): int |
| 87 | { |
| 88 | return $this->pointer; |
| 89 | } |
| 90 | |
| 91 | public function next(): void |
| 92 | { |
| 93 | $this->pointer++; |
| 94 | } |
| 95 | |
| 96 | public function valid(): bool |
| 97 | { |
| 98 | return isset($this->uids()[$this->pointer]); |
| 99 | } |
| 100 | } |