1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino (http://webino.sk) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino/WebinoDraw for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk) |
7
|
|
|
* @author Peter Bačinský <[email protected]> |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace WebinoDraw\VarTranslator\Operation; |
12
|
|
|
|
13
|
|
|
use WebinoDraw\Exception; |
14
|
|
|
use WebinoDraw\VarTranslator\Translation; |
15
|
|
|
use Zend\Filter\FilterPluginManager; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Filter |
19
|
|
|
*/ |
20
|
|
|
class Filter |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var FilterPluginManager |
24
|
|
|
*/ |
25
|
|
|
protected $filters; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param FilterPluginManager $filters |
29
|
|
|
*/ |
30
|
|
|
public function __construct(FilterPluginManager $filters) |
31
|
|
|
{ |
32
|
|
|
$this->filters = $filters; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Apply functions and filters on variables |
37
|
|
|
* |
38
|
|
|
* Call function if exists else call filter. |
39
|
|
|
* |
40
|
|
|
* @param Translation $translation Variables with values to modify |
41
|
|
|
* @param array $spec Filter options |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
|
|
public function apply(Translation $translation, array $spec) |
45
|
|
|
{ |
46
|
|
|
foreach ($spec as $key => $subSpec) { |
47
|
|
|
if ($translation->offsetExists($key)) { |
48
|
|
|
$this->iterateFilterSpec((array) $subSpec, $key, $translation); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $spec |
57
|
|
|
* @param mixed $key |
58
|
|
|
* @param Translation $translation |
59
|
|
|
* @return $this |
60
|
|
|
* @throws Exception\InvalidInstructionException |
61
|
|
|
*/ |
62
|
|
|
protected function iterateFilterSpec(array $spec, $key, Translation $translation) |
63
|
|
|
{ |
64
|
|
|
foreach ($spec as $filter => $options) { |
65
|
|
|
$varTranslation = $translation->getVarTranslation(); |
66
|
|
|
$varTranslation->translate($options); |
67
|
|
|
|
68
|
|
|
if (!is_array($options)) { |
69
|
|
|
throw new Exception\InvalidInstructionException( |
70
|
|
|
'Expected array options for spec ' . print_r($spec, true) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$varTranslation->translate($filter); |
75
|
|
|
|
76
|
|
|
if (is_callable($filter)) { |
77
|
|
|
$translation[$key] = call_user_func_array($filter, $options); |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->callFilter($filter, $key, $translation, $options); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Call ZF filter |
89
|
|
|
* |
90
|
|
|
* @param string $filter |
91
|
|
|
* @param mixed $key |
92
|
|
|
* @param Translation $translation |
93
|
|
|
* @param array $options |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
protected function callFilter($filter, $key, Translation $translation, array $options) |
97
|
|
|
{ |
98
|
|
|
if (empty($options[0])) { |
99
|
|
|
$translation[$key] = ''; |
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
empty($options[1]) |
104
|
|
|
and $options[1] = []; |
105
|
|
|
|
106
|
|
|
$translation[$key] = $this->filters->get($filter, $options[1])->filter($options[0]); |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.