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