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