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\Operation\OnVar\PluginInterface; |
15
|
|
|
use WebinoDraw\VarTranslator\Translation; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class OnVar |
19
|
|
|
*/ |
20
|
|
|
class OnVar |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $plugins = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param PluginInterface $plugin |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
|
|
public function setPlugin(PluginInterface $plugin) |
32
|
|
|
{ |
33
|
|
|
$key = lcfirst(substr(strrchr(get_class($plugin), "\\"), 1)); |
34
|
|
|
$this->plugins[$key] = $plugin; |
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Translation $varTranslation |
40
|
|
|
* @param array $spec |
41
|
|
|
* @param callable $callback |
42
|
|
|
* @return $this |
43
|
|
|
* @throws Exception\InvalidInstructionException |
44
|
|
|
*/ |
45
|
|
|
public function apply(Translation $varTranslation, array $spec, callable $callback) |
46
|
|
|
{ |
47
|
|
|
foreach ($spec as $subSpec) { |
48
|
|
|
if (!array_key_exists('var', $subSpec)) { |
49
|
|
|
// TODO logger |
50
|
|
|
// 'Expected `var` option in ' . print_r($subSpec, true) |
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->invokePlugins($varTranslation, $subSpec, $callback); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Translation $varTranslation |
62
|
|
|
* @param array $spec |
63
|
|
|
* @param callable $callback |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
protected function invokePlugins(Translation $varTranslation, array $spec, callable $callback) |
67
|
|
|
{ |
68
|
|
|
$value = $varTranslation->removeVars($varTranslation->translateString($spec['var'])); |
69
|
|
|
$pass = false; |
70
|
|
|
|
71
|
|
|
foreach ($spec as $key => $subValues) { |
72
|
|
|
if (null === $subValues) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
foreach ((array) $subValues as $subValue) { |
77
|
|
|
|
78
|
|
|
$isAnd = (0 === strpos($key, 'and')); |
79
|
|
|
$isAnd and $key = lcfirst(substr($key, 3)); |
80
|
|
|
|
81
|
|
|
if (empty($this->plugins[$key])) { |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$expected = $varTranslation->removeVars($varTranslation->translateString($subValue)); |
86
|
|
|
$this->fixTypes($value, $expected); |
87
|
|
|
$bool = $this->plugins[$key]->__invoke($value, $expected); |
88
|
|
|
$pass = $isAnd ? $pass && $bool : $pass || $bool; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$pass and $callback($spec); |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Fix value types for equation |
98
|
|
|
* |
99
|
|
|
* @param mixed $valA |
100
|
|
|
* @param mixed $valB |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
|
|
private function fixTypes(&$valA, &$valB) |
104
|
|
|
{ |
105
|
|
|
if (empty($valA) && is_array($valA)) { |
106
|
|
|
$valA = (string) null; |
107
|
|
|
$valB = (string) $valB; |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
if (is_numeric($valA)) { |
111
|
|
|
$valA = (float) $valA; |
112
|
|
|
$valB = (float) $valB; |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
if (is_string($valA)) { |
116
|
|
|
$valA = (string) $valA; |
117
|
|
|
$valB = (string) $valB; |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|