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