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 Celemas\Sire\Shape;
8use Cosray\Field\Field;
9use Cosray\Field\FieldHydrator;
10use Cosray\Locales;
11use Cosray\Node\Factory;
12
13class ValidatorFactory
14{
15    protected readonly Shape $shape;
16
17    public function __construct(
18        protected readonly object $node,
19        protected readonly Locales $locales,
20        private readonly FieldHydrator $hydrator = new FieldHydrator(),
21    ) {
22        $this->shape = Shapes::create();
23        $this->shape->add('uid', 'string')->rules('required', 'maxlen:64');
24        $this->shape
25            ->add('handle', 'string')
26            ->rules('maxlen:64', 'regex:/^(?!.*[.][.])[A-Za-z0-9](?:[A-Za-z0-9._-]{0,62}[A-Za-z0-9])?$/')
27            ->optional()
28            ->nullable();
29        $this->shape->add('parent', 'string')->rules('maxlen:64')->optional()->nullable();
30        $this->shape->add('published', 'bool')->rules('required');
31        $this->shape->add('locked', 'bool')->empty('missing', 'null')->default(false);
32        $this->shape->add('hidden', 'bool')->empty('missing', 'null')->default(false);
33    }
34
35    public function create(): Shape
36    {
37        $contentShape = Shapes::create();
38
39        foreach (Factory::fieldNamesFor($this->node) as $fieldName) {
40            $this->add($contentShape, $fieldName, $this->hydrator->getField($this->node, $fieldName));
41        }
42
43        $this->shape->add('content', $contentShape)->optional()->nullable();
44
45        return $this->shape;
46    }
47
48    protected function add(Shape $shape, string $fieldName, Field $field): void
49    {
50        $shape
51            ->add($fieldName, $field->shape())
52            ->label($field->getLabel())
53            ->optional()
54            ->nullable();
55    }
56}