1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* transform Translatable instance to default string value being stored on record |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\I18nBundle\Form\DataTransformer; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
9
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
10
|
|
|
use Graviton\I18nBundle\Service\I18nUtils; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
14
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
15
|
|
|
* @link http://swisscom.ch |
16
|
|
|
*/ |
17
|
|
|
class TranslatableToDefaultStringTransformer implements DataTransformerInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var I18nUtils |
21
|
|
|
*/ |
22
|
|
|
private $utils; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param I18nUtils $utils i18n utils for various needs |
26
|
|
|
*/ |
27
|
|
|
public function __construct(I18nUtils $utils) |
28
|
|
|
{ |
29
|
|
|
$this->utils = $utils; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* return as array |
34
|
|
|
* |
35
|
|
|
* the value returned here will be translated by the i18n listener later on |
36
|
|
|
* |
37
|
|
|
* @param Translatable|null $translatable value from model |
38
|
|
|
* |
39
|
|
|
* @return array |
|
|
|
|
40
|
|
|
*/ |
41
|
|
|
public function transform($translatable) |
42
|
|
|
{ |
43
|
|
|
if ($translatable == null) { |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
$translated = [ |
47
|
|
|
$this->utils->getDefaultLanguage() => $translatable |
48
|
|
|
]; |
49
|
|
|
return $translated; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* return simple string |
54
|
|
|
* |
55
|
|
|
* all we want to store after going through a for is a simple string. If there |
56
|
|
|
* are any other languages being sent, this is where they get stored. |
57
|
|
|
* |
58
|
|
|
* @param array $default value from client |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
* |
62
|
|
|
* @throws TransformationFailedException |
63
|
|
|
*/ |
64
|
|
|
public function reverseTransform($default) |
65
|
|
|
{ |
66
|
|
|
$defaultLang = $this->utils->getDefaultLanguage(); |
67
|
|
|
if (!isset($default[$defaultLang])) { |
68
|
|
|
throw new TransformationFailedException(sprintf('Value must contain "%s" string', $defaultLang)); |
69
|
|
|
} |
70
|
|
|
$this->utils->insertTranslatable($default); |
71
|
|
|
return $default[$defaultLang]; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.