Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ValidatorFactory
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 add
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Validation;
6
7use Celema\Sire\Shape;
8use Cosray\Field\Field;
9use Cosray\Locales;
10use Cosray\Node\Factory;
11
12class ValidatorFactory
13{
14    protected readonly Shape $shape;
15
16    public function __construct(
17        protected readonly object $node,
18        protected readonly Locales $locales,
19    ) {
20        $this->shape = Shapes::create();
21        $this->shape->add('uid', 'string')->rules('required', 'maxlen:64');
22        $this->shape
23            ->add('handle', 'string')
24            ->rules('maxlen:64', 'regex:/^(?!.*[.][.])[A-Za-z0-9](?:[A-Za-z0-9._-]{0,62}[A-Za-z0-9])?$/')
25            ->optional()
26            ->nullable();
27        $this->shape->add('parent', 'string')->rules('maxlen:64')->optional()->nullable();
28        $this->shape->add('published', 'bool')->rules('required');
29        $this->shape->add('locked', 'bool')->empty('missing', 'null')->default(false);
30        $this->shape->add('hidden', 'bool')->empty('missing', 'null')->default(false);
31    }
32
33    public function create(): Shape
34    {
35        $contentShape = Shapes::create();
36
37        foreach (Factory::fieldNamesFor($this->node) as $fieldName) {
38            $this->add($contentShape, $fieldName, Factory::fieldFor($this->node, $fieldName));
39        }
40
41        $this->shape->add('content', $contentShape)->optional()->nullable();
42
43        return $this->shape;
44    }
45
46    protected function add(Shape $shape, string $fieldName, Field $field): void
47    {
48        $shape
49            ->add($fieldName, $field->shape())
50            ->label($field->getLabel() ?? $fieldName)
51            ->optional()
52            ->nullable();
53    }
54}