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|null $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
|
1 |
|
public function getState(): ?string |
47
|
|
|
{ |
48
|
1 |
|
$notes = $this->getAllInCategory('state'); |
49
|
1 |
|
foreach ($notes as $note) { |
50
|
1 |
|
if (isset($note['content'])) { |
51
|
1 |
|
return $note['content']; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setState(string $state): void |
59
|
|
|
{ |
60
|
|
|
$this->removeAllInCategory('state'); |
61
|
|
|
$this->addCategory('state', $state); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getDesc(): ?string |
65
|
|
|
{ |
66
|
|
|
$notes = $this->getAllInCategory('desc'); |
67
|
|
|
foreach ($notes as $note) { |
68
|
|
|
if (isset($note['content'])) { |
69
|
|
|
return $note['content']; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the extracted translation if any. |
78
|
|
|
*/ |
79
|
|
|
public function getTranslation(): ?string |
80
|
|
|
{ |
81
|
|
|
$notes = $this->getAllInCategory('translation'); |
82
|
|
|
foreach ($notes as $note) { |
83
|
|
|
if (isset($note['content'])) { |
84
|
|
|
return $note['content']; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function isApproved(): bool |
92
|
|
|
{ |
93
|
1 |
|
$notes = $this->getAllInCategory('approved'); |
94
|
1 |
|
foreach ($notes as $note) { |
95
|
1 |
|
if (isset($note['content'])) { |
96
|
1 |
|
return 'true' === $note['content']; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function setApproved(bool $bool): void |
104
|
|
|
{ |
105
|
|
|
$this->removeAllInCategory('approved'); |
106
|
|
|
$this->addCategory('approved', $bool ? 'true' : 'false'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getSourceLocations(): array |
110
|
|
|
{ |
111
|
|
|
$sources = []; |
112
|
|
|
$notes = $this->getAllInCategory('file-source'); |
113
|
|
|
foreach ($notes as $note) { |
114
|
|
|
if (!isset($note['content'])) { |
115
|
|
|
continue; |
116
|
|
|
} |
117
|
|
|
list($path, $line) = \explode(':', $note['content'], 2); |
118
|
|
|
$sources[] = ['path' => $path, 'line' => $line]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $sources; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Add metadata. |
126
|
|
|
*/ |
127
|
|
|
public function addCategory(string $name, string $content, int $priority = 1): void |
128
|
|
|
{ |
129
|
|
|
$this->notes[] = ['category' => $name, 'content' => $content, 'priority' => $priority]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function toArray(): array |
133
|
|
|
{ |
134
|
|
|
$metadata = $this->metadata; |
135
|
|
|
$metadata['notes'] = $this->notes; |
136
|
|
|
|
137
|
|
|
return $metadata; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Return all notes for one category. It will also order data according to priority. |
142
|
|
|
*/ |
143
|
1 |
|
public function getAllInCategory(string $category): array |
144
|
|
|
{ |
145
|
1 |
|
$data = []; |
146
|
1 |
|
foreach ($this->notes as $note) { |
147
|
1 |
|
if (!isset($note['category'])) { |
148
|
|
|
continue; |
149
|
|
|
} |
150
|
1 |
|
if ($note['category'] === $category) { |
151
|
1 |
|
if (!isset($note['priority'])) { |
152
|
1 |
|
$note['priority'] = '1'; |
153
|
|
|
} |
154
|
1 |
|
$data[] = $note; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
\usort($data, static function (array $a, array $b) { |
159
|
|
|
return (int) $a['priority'] - (int) $b['priority']; |
160
|
1 |
|
}); |
161
|
|
|
|
162
|
1 |
|
return $data; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Remove all metadata in category. |
167
|
|
|
*/ |
168
|
|
|
public function removeAllInCategory(string $category): void |
169
|
|
|
{ |
170
|
|
|
foreach ($this->notes as $i => $note) { |
171
|
|
|
if (isset($note['category']) && $note['category'] === $category) { |
172
|
|
|
unset($this->notes[$i]); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|