Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Reference | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
| control | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| value | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| structure | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| shape | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Field; |
| 6 | |
| 7 | use Celema\Sire\Shape; |
| 8 | use Cosray\Validation\Prepare; |
| 9 | use Cosray\Validation\Shapes; |
| 10 | use Cosray\Value; |
| 11 | use Override; |
| 12 | |
| 13 | /** |
| 14 | * A reference to one or more other nodes, stored language-neutral as an |
| 15 | * ordered list of `{uid}` items. Unbounded by default; #[Limit(max: 1)] |
| 16 | * makes it single. #[Pick(...)] constrains which nodes the picker offers |
| 17 | * (enforced by the search endpoint). |
| 18 | */ |
| 19 | class Reference extends Field implements Capability\Limitable |
| 20 | { |
| 21 | use Capability\IsLimitable; |
| 22 | |
| 23 | #[Override] |
| 24 | public function control(): Control |
| 25 | { |
| 26 | return Control::reference(); |
| 27 | } |
| 28 | |
| 29 | #[Override] |
| 30 | public function value(): Value\Reference |
| 31 | { |
| 32 | return new Value\Reference($this->owner, $this, $this->valueContext); |
| 33 | } |
| 34 | |
| 35 | #[Override] |
| 36 | public function structure(mixed $value = null): array |
| 37 | { |
| 38 | return $this->getFileStructure('reference', $value); |
| 39 | } |
| 40 | |
| 41 | #[Override] |
| 42 | public function shape(): Shape |
| 43 | { |
| 44 | $shape = Shapes::create(); |
| 45 | $this->addType($shape); |
| 46 | |
| 47 | $itemShape = Shapes::list(); |
| 48 | $itemShape->add('uid', 'string')->rules('required'); |
| 49 | |
| 50 | $value = $shape |
| 51 | ->add('value', $this->zxxShape($itemShape, $this->limitValidators())) |
| 52 | ->rules(...$this->validators) |
| 53 | ->prepare(Prepare::nullAsEmpty(...)); |
| 54 | |
| 55 | if (!$this->isRequired()) { |
| 56 | $value->optional()->nullable(); |
| 57 | } |
| 58 | |
| 59 | $this->addMeta($shape); |
| 60 | |
| 61 | return $shape; |
| 62 | } |
| 63 | } |