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