1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Admin Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015-2016 LIN3S <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LIN3S\CMSKernel\Infrastructure\Lin3sAdminBundle\Twig; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Twig action translation filter. |
18
|
|
|
* |
19
|
|
|
* @author Beñat Espiña <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class TwigActionTranslationFilter extends \Twig_Extension |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The translator. |
25
|
|
|
* |
26
|
|
|
* @var TranslatorInterface |
27
|
|
|
*/ |
28
|
|
|
private $translator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param TranslatorInterface $translator The translator |
34
|
|
|
*/ |
35
|
|
|
public function __construct(TranslatorInterface $translator) |
36
|
|
|
{ |
37
|
|
|
$this->translator = $translator; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function getFilters() |
44
|
|
|
{ |
45
|
|
|
return [ |
|
|
|
|
46
|
|
|
'action_translation' => new \Twig_SimpleFilter('action_translation', [$this, 'actionTranslation']), |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Callback of action translation Twig filter that returns resultant array. |
52
|
|
|
* |
53
|
|
|
* @param string $options The action options |
54
|
|
|
* |
55
|
|
|
* @return array|string |
56
|
|
|
*/ |
57
|
|
|
public function actionTranslation($options) |
58
|
|
|
{ |
59
|
|
|
if (!is_array($options)) { |
60
|
|
|
return $this->translator->trans($options, [], 'CmsKernelAdminBridge'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$resultOptions = []; |
64
|
|
|
foreach ($options as $optionKey => $option) { |
65
|
|
|
$option = null === json_decode($option, true) ? $option : json_decode($option, true); |
66
|
|
|
|
67
|
|
|
if (is_array($option)) { |
68
|
|
|
foreach ($option as $iterationKey => $iteration) { |
69
|
|
|
if (is_array($iteration)) { |
70
|
|
|
foreach ($iteration as $iKey => $i) { |
71
|
|
|
if (is_array($i)) { |
72
|
|
|
foreach ($i as $iiKey => $ii) { |
73
|
|
|
$resultOptions[$optionKey][$iterationKey][$iKey][$iiKey] = $this->translator->trans($ii, [], 'CmsKernelAdminBridge'); |
74
|
|
|
} |
75
|
|
|
} else { |
76
|
|
|
$resultOptions[$optionKey][$iterationKey][$iKey] = $this->translator->trans($i, [], 'CmsKernelAdminBridge'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} else { |
80
|
|
|
$resultOptions[$optionKey][$iterationKey] = $this->translator->trans($iteration, [], 'CmsKernelAdminBridge'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} else { |
84
|
|
|
$resultOptions[$optionKey] = $this->translator->trans($option, [], 'CmsKernelAdminBridge'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $resultOptions; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.