|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @jayS-de <[email protected]> |
|
4
|
|
|
* @created: 26.01.15, 15:19 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Commercetools\Core\Model\Common; |
|
8
|
|
|
|
|
9
|
|
|
use Commercetools\Core\Error\Message; |
|
10
|
|
|
use Commercetools\Core\Error\InvalidArgumentException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @package Commercetools\Core\Model\Type |
|
14
|
|
|
* @link https://dev.commercetools.com/http-api-types.html#localized-string |
|
15
|
|
|
* @example |
|
16
|
|
|
* ```php |
|
17
|
|
|
* LocalizedString::fromArray(['en' => 'Hello World', 'de' => 'Hallo Welt'])->add('fr', 'Bonjour le monde'); |
|
18
|
|
|
* ``` |
|
19
|
|
|
*/ |
|
20
|
|
|
class LocalizedString implements \JsonSerializable, JsonDeserializeInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use ContextTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $values = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param array $values |
|
31
|
|
|
* @param Context|callable $context |
|
32
|
|
|
*/ |
|
33
|
229 |
|
public function __construct(array $values, $context = null) |
|
34
|
|
|
{ |
|
35
|
229 |
|
$this->setContext($context); |
|
36
|
229 |
|
foreach ($values as $locale => $value) { |
|
37
|
228 |
|
$this->add($locale, $value); |
|
38
|
|
|
} |
|
39
|
229 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param $locale |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
48 |
|
public function __get($locale) |
|
46
|
|
|
{ |
|
47
|
48 |
|
$context = new Context(); |
|
48
|
48 |
|
$context->setLanguages([$locale])->setGraceful($this->getContext()->isGraceful()); |
|
|
|
|
|
|
49
|
48 |
|
return $this->get($context); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param Context $context |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function getLocalized(Context $context = null) |
|
57
|
|
|
{ |
|
58
|
2 |
|
return $this->get($context); |
|
59
|
|
|
} |
|
60
|
|
|
/** |
|
61
|
|
|
* @param Context $context |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
85 |
|
protected function getLanguage(Context $context) |
|
65
|
|
|
{ |
|
66
|
85 |
|
$locale = null; |
|
67
|
85 |
|
foreach ($context->getLanguages() as $locale) { |
|
68
|
85 |
|
$locale = \Locale::canonicalize($locale); |
|
69
|
85 |
|
if (isset($this->values[$locale])) { |
|
70
|
75 |
|
return $locale; |
|
71
|
|
|
} |
|
72
|
11 |
|
$language = \Locale::getPrimaryLanguage($locale); |
|
73
|
11 |
|
if ($locale == $language) { |
|
74
|
7 |
|
continue; |
|
75
|
|
|
} |
|
76
|
4 |
|
if (isset($this->values[$language])) { |
|
77
|
4 |
|
return $language; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
6 |
|
return $locale; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param Context $context |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
85 |
View Code Duplication |
public function get(Context $context = null) |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
85 |
|
if (is_null($context)) { |
|
90
|
20 |
|
$context = $this->getContext(); |
|
91
|
|
|
} |
|
92
|
85 |
|
$locale = $this->getLanguage($context); |
|
93
|
85 |
|
if (!isset($this->values[$locale])) { |
|
94
|
6 |
|
if (!$context->isGraceful()) { |
|
95
|
1 |
|
throw new InvalidArgumentException(Message::NO_VALUE_FOR_LOCALE); |
|
96
|
|
|
} |
|
97
|
5 |
|
return ''; |
|
98
|
|
|
} |
|
99
|
79 |
|
return $this->values[$locale]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $locale |
|
104
|
|
|
* @param string $value |
|
105
|
|
|
* @return $this |
|
106
|
|
|
*/ |
|
107
|
229 |
|
public function add($locale, $value) |
|
108
|
|
|
{ |
|
109
|
229 |
|
$locale = \Locale::canonicalize($locale); |
|
110
|
229 |
|
$this->values[$locale] = $value; |
|
111
|
|
|
|
|
112
|
229 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
8 |
|
public function merge(LocalizedString $localizedString) |
|
116
|
|
|
{ |
|
117
|
8 |
|
$this->values = array_merge($this->values, $localizedString->toArray()); |
|
118
|
8 |
|
} |
|
119
|
|
|
|
|
120
|
17 |
|
public function __toString() |
|
121
|
|
|
{ |
|
122
|
17 |
|
return $this->get(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
179 |
|
public function toArray() |
|
129
|
|
|
{ |
|
130
|
179 |
|
$values = $this->values; |
|
131
|
|
|
|
|
132
|
179 |
|
$data = []; |
|
133
|
179 |
|
foreach ($values as $key => $value) { |
|
134
|
179 |
|
$data[str_replace('_', '-', $key)] = $value; |
|
135
|
|
|
} |
|
136
|
179 |
|
return $data; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
169 |
|
public function jsonSerialize() |
|
143
|
|
|
{ |
|
144
|
169 |
|
return $this->toArray(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param Context|callable $context |
|
149
|
|
|
* @return $this |
|
150
|
|
|
*/ |
|
151
|
|
|
public static function of($context = null) |
|
152
|
|
|
{ |
|
153
|
|
|
return new static([], $context); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param array $data |
|
158
|
|
|
* @param Context|callable $context |
|
159
|
|
|
* @return static |
|
160
|
|
|
*/ |
|
161
|
108 |
|
public static function fromArray(array $data, $context = null) |
|
162
|
|
|
{ |
|
163
|
108 |
|
return new static($data, $context); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param string $language |
|
168
|
|
|
* @param string $text |
|
169
|
|
|
* @param Context|callable $context |
|
170
|
|
|
* @return LocalizedString |
|
171
|
|
|
*/ |
|
172
|
147 |
|
public static function ofLangAndText($language, $text, $context = null) |
|
173
|
|
|
{ |
|
174
|
147 |
|
return new static([$language => $text], $context); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: