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 | |
| 12 | /** |
| 13 | * A reference to one or more other nodes, stored language-neutral as an |
| 14 | * ordered list of `{uid}` items. Unbounded by default; #[Limit(max: 1)] |
| 15 | * makes it single. #[Pick(...)] constrains which nodes the picker offers |
| 16 | * (enforced by the search endpoint). |
| 17 | */ |
| 18 | class Reference extends Field implements Capability\Limitable |
| 19 | { |
| 20 | use Capability\IsLimitable; |
| 21 | |
| 22 | public function control(): Control |
| 23 | { |
| 24 | return Control::reference(); |
| 25 | } |
| 26 | |
| 27 | public function value(): Value\Reference |
| 28 | { |
| 29 | return new Value\Reference($this->owner, $this, $this->valueContext); |
| 30 | } |
| 31 | |
| 32 | public function structure(mixed $value = null): array |
| 33 | { |
| 34 | return $this->getFileStructure('reference', $value); |
| 35 | } |
| 36 | |
| 37 | public function shape(): Shape |
| 38 | { |
| 39 | $shape = Shapes::create(); |
| 40 | $this->addType($shape); |
| 41 | |
| 42 | $itemShape = Shapes::list(); |
| 43 | $itemShape->add('uid', 'string')->rules('required'); |
| 44 | |
| 45 | $value = $shape |
| 46 | ->add('value', $this->zxxShape($itemShape, $this->limitValidators())) |
| 47 | ->rules(...$this->validators) |
| 48 | ->prepare(Prepare::nullAsEmpty(...)); |
| 49 | |
| 50 | if (!$this->isRequired()) { |
| 51 | $value->optional()->nullable(); |
| 52 | } |
| 53 | |
| 54 | $this->addMeta($shape); |
| 55 | |
| 56 | return $shape; |
| 57 | } |
| 58 | } |