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;
10
11class Code extends Text implements Capability\SyntaxAware
12{
13    use Capability\IsSyntaxAware;
14
15    public function control(): Control
16    {
17        return Control::code();
18    }
19
20    public function value(): CodeValue
21    {
22        return new CodeValue($this->owner, $this, $this->valueContext);
23    }
24
25    public function structure(mixed $value = null): array
26    {
27        $syntax = $this->valueContext->data['meta']['syntax'][self::NEUTRAL_LOCALE] ?? $this->getDefaultSyntax();
28
29        if (is_array($value) && array_key_exists('value', $value)) {
30            $syntax = is_string($value['meta']['syntax'][self::NEUTRAL_LOCALE] ?? null)
31                ? $value['meta']['syntax'][self::NEUTRAL_LOCALE]
32                : $syntax;
33            $value = $value['value'];
34        }
35
36        $result = $this->getTranslatableStructure('code', $value);
37        $result['meta']['syntax'] = [self::NEUTRAL_LOCALE => $syntax];
38
39        return $result;
40    }
41
42    public function shape(): Shape
43    {
44        $shape = Shapes::create();
45        $this->addType($shape);
46
47        if ($this->isTranslatable()) {
48            $locales = $this->owner->locales();
49            $defaultLocale = $locales->getDefault()->id;
50            $i18nShape = Shapes::create();
51
52            foreach ($locales as $locale) {
53                $localeValidators = [];
54
55                if ($this->isRequired() && $locale->id === $defaultLocale) {
56                    $localeValidators[] = 'required';
57                }
58
59                $localeField = $i18nShape
60                    ->add($locale->id, 'string')
61                    ->label($this->valueLabel($locale))
62                    ->rules(...$localeValidators);
63
64                if (!in_array('required', $localeValidators, true)) {
65                    $localeField->optional()->nullable();
66                }
67            }
68
69            $value = $shape->add('value', $i18nShape)->rules(...$this->validators);
70        } else {
71            $value = $shape->add('value', $this->zxxShape('string', $this->validators));
72        }
73
74        if (!$this->isRequired()) {
75            $value->optional()->nullable();
76        }
77
78        $meta = Shapes::create();
79        $meta->add('syntax', $this->zxxShape('string', ['in:' . implode(',', $this->getSyntaxes())]));
80        $shape->add('meta', $meta)->optional()->nullable();
81
82        return $shape;
83    }
84
85    public function properties(): array
86    {
87        $result = parent::properties();
88        $result['syntaxes'] = $this->getSyntaxes();
89
90        return $result;
91    }
92}