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
Text
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 value
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 structure
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 shape
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Field;
6
7use Celemas\Sire\Shape;
8use Cosray\Validation\Shapes;
9use Cosray\Value\Text as TextValue;
10
11class Text extends Field implements Capability\Translatable
12{
13    use Capability\IsTranslatable;
14
15    public function value(): TextValue
16    {
17        return new TextValue($this->owner, $this, $this->valueContext);
18    }
19
20    public function structure(mixed $value = null): array
21    {
22        return $this->getTranslatableStructure('text', $value);
23    }
24
25    public function shape(): Shape
26    {
27        $shape = Shapes::create();
28        $this->addType($shape);
29
30        if ($this->isTranslatable()) {
31            $locales = $this->owner->locales();
32            $defaultLocale = $locales->getDefault()->id;
33            $i18nShape = Shapes::create();
34
35            foreach ($locales as $locale) {
36                $localeValidators = [];
37
38                if ($this->isRequired() && $locale->id === $defaultLocale) {
39                    $localeValidators[] = 'required';
40                }
41
42                $localeField = $i18nShape->add($locale->id, 'string')->rules(...$localeValidators);
43
44                if (!in_array('required', $localeValidators, true)) {
45                    $localeField->optional()->nullable();
46                }
47            }
48
49            $value = $shape->add('value', $i18nShape)->rules(...$this->validators);
50        } else {
51            $value = $shape->add('value', $this->zxxShape('string', $this->validators));
52        }
53
54        if (!$this->isRequired()) {
55            $value->optional()->nullable();
56        }
57
58        $this->addMeta($shape);
59
60        return $shape;
61    }
62}