|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Gerard van Helden <[email protected]> |
|
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Zicht\Bundle\UrlBundle\Url\Params\Translator; |
|
8
|
|
|
|
|
9
|
|
|
use Zicht\Bundle\UrlBundle\Url\Params\Translator; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Static translator which holds mappings for keys and values. |
|
13
|
|
|
*/ |
|
14
|
|
|
class StaticTranslator implements Translator |
|
15
|
|
|
{ |
|
16
|
|
|
protected $keyName; |
|
17
|
|
|
protected $keyTranslation; |
|
18
|
|
|
protected $valueTranslations; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $keyName |
|
24
|
|
|
* @param string $keyTranslation |
|
25
|
|
|
* @param array $valueTranslations |
|
26
|
|
|
*/ |
|
27
|
6 |
|
public function __construct($keyName, $keyTranslation, $valueTranslations = array()) |
|
28
|
|
|
{ |
|
29
|
6 |
|
$this->keyName = $keyName; |
|
30
|
6 |
|
$this->keyTranslation = $keyTranslation; |
|
31
|
6 |
|
$this->valueTranslations = $valueTranslations; |
|
32
|
6 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @{inheritDoc} |
|
37
|
|
|
*/ |
|
38
|
5 |
|
public function translateKeyInput($keyTranslation) |
|
39
|
|
|
{ |
|
40
|
5 |
|
if ($keyTranslation == $this->keyTranslation) { |
|
41
|
5 |
|
return $this->keyName; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
return false; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @{inheritDoc} |
|
50
|
|
|
*/ |
|
51
|
3 |
|
public function translateValueInput($keyTranslation, $valueTranslation) |
|
52
|
|
|
{ |
|
53
|
3 |
|
if ($keyTranslation == $this->keyTranslation) { |
|
54
|
3 |
|
if (false !== ($value = array_search($valueTranslation, $this->valueTranslations))) { |
|
55
|
3 |
|
return $value; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
return false; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @{inheritDoc} |
|
65
|
|
|
*/ |
|
66
|
5 |
|
public function translateKeyOutput($keyName) |
|
67
|
|
|
{ |
|
68
|
5 |
|
if ($keyName == $this->keyName) { |
|
69
|
5 |
|
return $this->keyTranslation; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
3 |
|
return false; |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @{inheritDoc} |
|
78
|
|
|
*/ |
|
79
|
3 |
|
public function translateValueOutput($keyName, $value) |
|
80
|
|
|
{ |
|
81
|
3 |
|
if ($keyName == $this->keyName) { |
|
82
|
3 |
|
if (isset($this->valueTranslations[$value])) { |
|
83
|
3 |
|
return $this->valueTranslations[$value]; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
return false; |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Adds a value translation |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $in |
|
95
|
|
|
* @param string $out |
|
96
|
|
|
* @return self |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function addTranslation($in, $out) |
|
99
|
|
|
{ |
|
100
|
1 |
|
$this->valueTranslations[$in] = $out; |
|
101
|
1 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: