Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
58.82% |
20 / 34 |
|
58.33% |
7 / 12 |
CRAP | |
0.00% |
0 / 1 |
| IsTranslatable | |
58.82% |
20 / 34 |
|
58.33% |
7 / 12 |
47.93 | |
0.00% |
0 / 1 |
| translate | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| isTranslatable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| translateMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| supportedTranslateModes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| supportsTranslateMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isSymmetricallyTranslated | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isAsymmetricallyTranslated | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTranslatableStructure | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| getTranslatableFileStructure | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| localeValueMap | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
5.12 | |||
| localeListMap | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| isList | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Field\Capability; |
| 6 | |
| 7 | use Cosray\Schema\TranslateMode; |
| 8 | |
| 9 | trait IsTranslatable |
| 10 | { |
| 11 | protected ?TranslateMode $translateMode = null; |
| 12 | |
| 13 | public function translate(TranslateMode $mode = TranslateMode::Symmetric): static |
| 14 | { |
| 15 | $this->translateMode = $mode; |
| 16 | |
| 17 | return $this; |
| 18 | } |
| 19 | |
| 20 | public function isTranslatable(): bool |
| 21 | { |
| 22 | return $this->translateMode !== null; |
| 23 | } |
| 24 | |
| 25 | public function translateMode(): ?TranslateMode |
| 26 | { |
| 27 | return $this->translateMode; |
| 28 | } |
| 29 | |
| 30 | /** @return list<TranslateMode> */ |
| 31 | protected function supportedTranslateModes(): array |
| 32 | { |
| 33 | return [TranslateMode::Symmetric]; |
| 34 | } |
| 35 | |
| 36 | public function supportsTranslateMode(TranslateMode $mode): bool |
| 37 | { |
| 38 | return in_array($mode, $this->supportedTranslateModes(), true); |
| 39 | } |
| 40 | |
| 41 | protected function isSymmetricallyTranslated(): bool |
| 42 | { |
| 43 | return $this->translateMode === TranslateMode::Symmetric; |
| 44 | } |
| 45 | |
| 46 | protected function isAsymmetricallyTranslated(): bool |
| 47 | { |
| 48 | return $this->translateMode === TranslateMode::Asymmetric; |
| 49 | } |
| 50 | |
| 51 | protected function getTranslatableStructure(string $type, mixed $value = null): array |
| 52 | { |
| 53 | unset($type); |
| 54 | |
| 55 | $value ??= $this->default; |
| 56 | |
| 57 | return [ |
| 58 | 'type' => $this::class, |
| 59 | 'value' => $this->isTranslatable() |
| 60 | ? $this->localeValueMap($value) |
| 61 | : [self::NEUTRAL_LOCALE => $value], |
| 62 | ]; |
| 63 | } |
| 64 | |
| 65 | protected function getTranslatableFileStructure(string $type, mixed $value = null): array |
| 66 | { |
| 67 | unset($type); |
| 68 | |
| 69 | return [ |
| 70 | 'type' => $this::class, |
| 71 | 'value' => $this->localeListMap($value), |
| 72 | ]; |
| 73 | } |
| 74 | |
| 75 | protected function localeValueMap(mixed $value = null): array |
| 76 | { |
| 77 | $result = is_array($value) ? $value : []; |
| 78 | |
| 79 | foreach ($this->owner->locales() as $locale) { |
| 80 | $result[$locale->id] ??= null; |
| 81 | } |
| 82 | |
| 83 | if (!is_array($value) && $value !== null) { |
| 84 | $result[$this->owner->defaultLocale()->id] = $value; |
| 85 | } |
| 86 | |
| 87 | return $result; |
| 88 | } |
| 89 | |
| 90 | protected function localeListMap(mixed $value = null): array |
| 91 | { |
| 92 | if (is_array($value) && !$this->isList($value)) { |
| 93 | return $value; |
| 94 | } |
| 95 | |
| 96 | $result = []; |
| 97 | |
| 98 | foreach ($this->owner->locales() as $locale) { |
| 99 | $result[$locale->id] = []; |
| 100 | } |
| 101 | |
| 102 | return $result; |
| 103 | } |
| 104 | |
| 105 | private function isList(array $value): bool |
| 106 | { |
| 107 | return array_is_list($value); |
| 108 | } |
| 109 | } |