1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Translation\Bundle\Model; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Message metadata abstraction. |
16
|
|
|
* |
17
|
|
|
* @author Tobias Nyholm <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
final class Metadata |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $metadata; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $notes = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $metadata |
33
|
|
|
*/ |
34
|
3 |
|
public function __construct($metadata) |
35
|
|
|
{ |
36
|
3 |
|
if (empty($metadata)) { |
37
|
3 |
|
$metadata = []; |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
$this->metadata = $metadata; |
41
|
3 |
|
if (isset($metadata['notes'])) { |
42
|
1 |
|
$this->notes = $metadata['notes']; |
43
|
|
|
} |
44
|
3 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string|null |
48
|
|
|
*/ |
49
|
1 |
View Code Duplication |
public function getState() |
|
|
|
|
50
|
|
|
{ |
51
|
1 |
|
$notes = $this->getAllInCategory('state'); |
52
|
1 |
|
foreach ($notes as $note) { |
53
|
1 |
|
if (isset($note['content'])) { |
54
|
1 |
|
return $note['content']; |
55
|
|
|
} |
56
|
|
|
} |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $state |
61
|
|
|
*/ |
62
|
|
|
public function setState($state) |
63
|
|
|
{ |
64
|
|
|
$this->removeAllInCategory('state'); |
65
|
|
|
$this->addCategory('state', $state); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string|null |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function getDesc() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$notes = $this->getAllInCategory('desc'); |
74
|
|
|
foreach ($notes as $note) { |
75
|
|
|
if (isset($note['content'])) { |
76
|
|
|
return $note['content']; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the extracted translation if any. |
85
|
|
|
* |
86
|
|
|
* @return string|null |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function getTranslation() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$notes = $this->getAllInCategory('translation'); |
91
|
|
|
foreach ($notes as $note) { |
92
|
|
|
if (isset($note['content'])) { |
93
|
|
|
return $note['content']; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
1 |
View Code Duplication |
public function isApproved() |
|
|
|
|
104
|
|
|
{ |
105
|
1 |
|
$notes = $this->getAllInCategory('approved'); |
106
|
1 |
|
foreach ($notes as $note) { |
107
|
1 |
|
if (isset($note['content'])) { |
108
|
1 |
|
return 'true' === $note['content']; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param bool $bool |
117
|
|
|
*/ |
118
|
|
|
public function setApproved($bool) |
119
|
|
|
{ |
120
|
|
|
$this->removeAllInCategory('approved'); |
121
|
|
|
$this->addCategory('approved', $bool ? 'true' : 'false'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function getSourceLocations() |
128
|
|
|
{ |
129
|
|
|
$sources = []; |
130
|
|
|
$notes = $this->getAllInCategory('file-source'); |
131
|
|
|
foreach ($notes as $note) { |
132
|
|
|
if (!isset($note['content'])) { |
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
list($path, $line) = \explode(':', $note['content'], 2); |
136
|
|
|
$sources[] = ['path' => $path, 'line' => $line]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $sources; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Add metadata. |
144
|
|
|
* |
145
|
|
|
* @param string $name |
146
|
|
|
* @param string $content |
147
|
|
|
*/ |
148
|
|
|
public function addCategory($name, $content, $priority = 1) |
149
|
|
|
{ |
150
|
|
|
$this->notes[] = ['category' => $name, 'content' => $content, 'priority' => $priority]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function toArray() |
157
|
|
|
{ |
158
|
|
|
$metadata = $this->metadata; |
159
|
|
|
$metadata['notes'] = $this->notes; |
160
|
|
|
|
161
|
|
|
return $metadata; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Return all notes for one category. It will also order data according to priority. |
166
|
|
|
* |
167
|
|
|
* @param string $category |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
1 |
|
public function getAllInCategory($category) |
172
|
|
|
{ |
173
|
1 |
|
$data = []; |
174
|
1 |
|
foreach ($this->notes as $note) { |
175
|
1 |
|
if (!isset($note['category'])) { |
176
|
|
|
continue; |
177
|
|
|
} |
178
|
1 |
|
if ($note['category'] === $category) { |
179
|
1 |
|
if (!isset($note['priority'])) { |
180
|
1 |
|
$note['priority'] = '1'; |
181
|
|
|
} |
182
|
1 |
|
$data[] = $note; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
\usort($data, function (array $a, array $b) { |
187
|
|
|
return (int) $a['priority'] - (int) $b['priority']; |
188
|
1 |
|
}); |
189
|
|
|
|
190
|
1 |
|
return $data; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Remove all metadata in category. |
195
|
|
|
* |
196
|
|
|
* @param string $category |
197
|
|
|
*/ |
198
|
|
|
public function removeAllInCategory($category) |
199
|
|
|
{ |
200
|
|
|
foreach ($this->notes as $i => $note) { |
201
|
|
|
if (isset($note['category']) && $note['category'] === $category) { |
202
|
|
|
unset($this->notes[$i]); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.