1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelLangBundler\BundleItems; |
4
|
|
|
|
5
|
|
|
class BundleItem |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Lang id of translation. |
9
|
|
|
* |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
protected $id; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Namespace of id. The last value in the id. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $namespace; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Key to be used in return array. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $key; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Collection of valid parameters. |
30
|
|
|
* |
31
|
|
|
* @var Collection |
32
|
|
|
*/ |
33
|
|
|
protected $parameters = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The translation value from lang. Returned in return array. |
37
|
|
|
* |
38
|
|
|
* @var mixed |
39
|
|
|
*/ |
40
|
|
|
protected $value = ''; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* If not null, trans_choice will be called and value passed as countable. |
44
|
|
|
* |
45
|
|
|
* @var null|int|Countable |
46
|
|
|
*/ |
47
|
|
|
protected $choice = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Construct. |
51
|
|
|
* |
52
|
|
|
* @param string $id |
53
|
|
|
*/ |
54
|
|
|
public function __construct($id) |
55
|
|
|
{ |
56
|
|
|
$namespace = $this->setNamespace($id); |
57
|
|
|
|
58
|
|
|
$this->setKey($namespace); |
59
|
|
|
|
60
|
|
|
$this->setId($id); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return the value of choice. If not null, trans_choice will be called. |
65
|
|
|
* |
66
|
|
|
* @return int|Countable|null |
67
|
|
|
*/ |
68
|
|
|
public function hasChoice() |
69
|
|
|
{ |
70
|
|
|
return $this->choice; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Return the translation key for return array. |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getKey() |
79
|
|
|
{ |
80
|
|
|
return $this->key; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the Laravel lang id of translation. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getId() |
89
|
|
|
{ |
90
|
|
|
return $this->id; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return the namespace for the translation. |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getNamespace() |
99
|
|
|
{ |
100
|
|
|
return $this->namespace; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Return array of valid parameters. |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function getParameters() |
109
|
|
|
{ |
110
|
|
|
return $this->parameters->all(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Return the final key/value pair array. |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
public function getReturnArray() |
119
|
|
|
{ |
120
|
|
|
return [$this->getKey() => $this->getValue()]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return the value. |
125
|
|
|
* |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
public function getValue() |
129
|
|
|
{ |
130
|
|
|
return $this->value; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set choice value on object. |
135
|
|
|
* |
136
|
|
|
* @param int|Countable $value |
137
|
|
|
*/ |
138
|
|
|
public function setChoice($value) |
139
|
|
|
{ |
140
|
|
|
$this->choice = $value; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set only valid parameters. If namespaced, only parameters with |
145
|
|
|
* translation namespace will be set. |
146
|
|
|
* |
147
|
|
|
* @param array $parameters |
148
|
|
|
*/ |
149
|
|
|
public function setParameters($parameters) |
150
|
|
|
{ |
151
|
|
|
$this->parameters = collect($parameters)->mapWithKeys(function ($value, $key) { |
|
|
|
|
152
|
|
|
if (!$key = $this->getNamespacedKey($key)) { |
153
|
|
|
return []; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ($key === 'choice') { |
157
|
|
|
$this->setChoice($value); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return [$key => $value]; |
161
|
|
|
}); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get only global and keys with this item's namespace. |
166
|
|
|
* |
167
|
|
|
* @param string $key |
168
|
|
|
* |
169
|
|
|
* @return string|null |
170
|
|
|
*/ |
171
|
|
|
protected function getNamespacedKey($key) |
172
|
|
|
{ |
173
|
|
|
$keyArray = explode('.', $key); |
174
|
|
|
|
175
|
|
|
if (count($keyArray) !== 2) { |
176
|
|
|
return $key; |
177
|
|
|
} elseif ($keyArray[0] === $this->getNamespace()) { |
178
|
|
|
return $keyArray[1]; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set the lang value on the object. |
184
|
|
|
* |
185
|
|
|
* @param mixed $value |
186
|
|
|
*/ |
187
|
|
|
public function setValue($value) |
188
|
|
|
{ |
189
|
|
|
$this->value = $value; |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Set the translation namespace on the object. |
196
|
|
|
*/ |
197
|
|
|
protected function setNamespace($id) |
198
|
|
|
{ |
199
|
|
|
return $this->namespace = collect(explode('.', $id))->last(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get key for translation value. |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
protected function setKey($namespace) |
208
|
|
|
{ |
209
|
|
|
$transformMethod = config('lang-bundler.key_transform'); |
210
|
|
|
|
211
|
|
|
if ($transformMethod === 'none') { |
212
|
|
|
return $this->key = $namespace; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $this->key = $transformMethod($namespace); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Prefix the translation id with global_key_namespace, if set. |
220
|
|
|
* |
221
|
|
|
* @param string $id |
222
|
|
|
* |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
protected function setId($id) |
226
|
|
|
{ |
227
|
|
|
$prefix = config('lang-bundler.global_key_namespace'); |
228
|
|
|
|
229
|
|
|
if (!empty($prefix)) { |
230
|
|
|
$id = $prefix.'.'.$id; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$this->id = $id; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..