Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.67% |
13 / 15 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| TranslateHandler | |
86.67% |
13 / 15 |
|
0.00% |
0 / 2 |
6.09 | |
0.00% |
0 / 1 |
| apply | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
3.02 | |||
| properties | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Field\Schema; |
| 6 | |
| 7 | use Cosray\Exception\RuntimeException; |
| 8 | use Cosray\Field\Capability\Translatable; |
| 9 | use Cosray\Field\Field; |
| 10 | use Cosray\Schema\Translate; |
| 11 | |
| 12 | class TranslateHandler extends Handler |
| 13 | { |
| 14 | public function apply(object $meta, Field $field): void |
| 15 | { |
| 16 | /** @var Translate $meta */ |
| 17 | if ($field instanceof Translatable) { |
| 18 | if (!$field->supportsTranslateMode($meta->mode)) { |
| 19 | throw new RuntimeException( |
| 20 | $field::class . " does not support {$meta->mode->value} translation", |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | $field->translate($meta->mode); |
| 25 | |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | throw new RuntimeException($this->capabilityErrorMessage($field, Translatable::class)); |
| 30 | } |
| 31 | |
| 32 | public function properties(object $meta, Field $field): array |
| 33 | { |
| 34 | if ($field instanceof Translatable) { |
| 35 | $properties = ['translate' => $field->isTranslatable()]; |
| 36 | $mode = $field->translateMode(); |
| 37 | |
| 38 | if ($mode !== null) { |
| 39 | $properties['translateMode'] = $mode->value; |
| 40 | } |
| 41 | |
| 42 | return $properties; |
| 43 | } |
| 44 | |
| 45 | return []; |
| 46 | } |
| 47 | } |