Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
52.63% covered (warning)
52.63%
20 / 38
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Code
52.63% covered (warning)
52.63%
20 / 38
60.00% covered (warning)
60.00%
3 / 5
34.83
0.00% covered (danger)
0.00%
0 / 1
 control
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 value
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 structure
55.56% covered (warning)
55.56%
5 / 9
0.00% covered (danger)
0.00%
0 / 1
5.40
 shape
41.67% covered (danger)
41.67%
10 / 24
0.00% covered (danger)
0.00%
0 / 1
16.73
 properties
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Field;
6
7use Celema\Sire\Shape;
8use Cosray\Validation\Shapes;
9use Cosray\Value\Code as CodeValue;
10use Override;
11
12class Code extends Text implements Capability\SyntaxAware
13{
14    use Capability\IsSyntaxAware;
15
16    #[Override]
17    public function control(): Control
18    {
19        return Control::code();
20    }
21
22    #[Override]
23    public function value(): CodeValue
24    {
25        return new CodeValue($this->owner, $this, $this->valueContext);
26    }
27
28    #[Override]
29    public function structure(mixed $value = null): array
30    {
31        $syntax = $this->valueContext->data['meta']['syntax'][self::NEUTRAL_LOCALE] ?? $this->getDefaultSyntax();
32
33        if (is_array($value) && array_key_exists('value', $value)) {
34            $syntax = is_string($value['meta']['syntax'][self::NEUTRAL_LOCALE] ?? null)
35                ? $value['meta']['syntax'][self::NEUTRAL_LOCALE]
36                : $syntax;
37            $value = $value['value'];
38        }
39
40        $result = $this->getTranslatableStructure('code', $value);
41        $result['meta']['syntax'] = [self::NEUTRAL_LOCALE => $syntax];
42
43        return $result;
44    }
45
46    #[Override]
47    public function shape(): Shape
48    {
49        $shape = Shapes::create();
50        $this->addType($shape);
51
52        if ($this->isTranslatable()) {
53            $locales = $this->owner->locales();
54            $defaultLocale = $locales->getDefault()->id;
55            $i18nShape = Shapes::create();
56
57            foreach ($locales as $locale) {
58                $localeValidators = [];
59
60                if ($this->isRequired() && $locale->id === $defaultLocale) {
61                    $localeValidators[] = 'required';
62                }
63
64                $localeField = $i18nShape
65                    ->add($locale->id, 'string')
66                    ->label($this->valueLabel($locale))
67                    ->rules(...$localeValidators);
68
69                if (!in_array('required', $localeValidators, true)) {
70                    $localeField->optional()->nullable();
71                }
72            }
73
74            $value = $shape->add('value', $i18nShape)->rules(...$this->validators);
75        } else {
76            $value = $shape->add('value', $this->zxxShape('string', $this->validators));
77        }
78
79        if (!$this->isRequired()) {
80            $value->optional()->nullable();
81        }
82
83        $meta = Shapes::create();
84        $meta->add('syntax', $this->zxxShape('string', ['in:' . implode(',', $this->getSyntaxes())]));
85        $shape->add('meta', $meta)->optional()->nullable();
86
87        return $shape;
88    }
89
90    #[Override]
91    public function properties(): array
92    {
93        $result = parent::properties();
94        $result['syntaxes'] = $this->getSyntaxes();
95
96        return $result;
97    }
98}