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