1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppLocalize; |
4
|
|
|
|
5
|
|
|
use AppLocalize\Parser\Text; |
6
|
|
|
|
7
|
|
|
class Localization_Scanner_StringsCollection |
8
|
|
|
{ |
9
|
|
|
const ERROR_UNKNOWN_STRING_HASH = 39201; |
10
|
|
|
|
11
|
|
|
const SOURCE_FILE = 'file'; |
12
|
|
|
|
13
|
|
|
const STORAGE_FORMAT_VERSION = 2; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Localization_Scanner |
17
|
|
|
*/ |
18
|
|
|
protected $scanner; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Localization_Scanner_StringHash[] |
22
|
|
|
*/ |
23
|
|
|
protected $hashes = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $warnings = array(); |
29
|
|
|
|
30
|
|
|
public function __construct(Localization_Scanner $scanner) |
31
|
|
|
{ |
32
|
|
|
$this->scanner = $scanner; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function addFromFile(string $sourceID, string $relativePath, string $languageType, Text $text) : void |
36
|
|
|
{ |
37
|
|
|
$string = $this->createString($sourceID, self::SOURCE_FILE, $text); |
38
|
|
|
|
39
|
|
|
$string->setProperty('languageType', $languageType); |
40
|
|
|
$string->setProperty('relativePath', $relativePath); |
41
|
|
|
|
42
|
|
|
$this->add($string); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function addWarning(Localization_Parser_Warning $warning) : void |
46
|
|
|
{ |
47
|
|
|
$this->warnings[] = $warning->toArray(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function createString(string $sourceID, string $sourceType, Text $text) : Localization_Scanner_StringInfo |
51
|
|
|
{ |
52
|
|
|
return new Localization_Scanner_StringInfo($this, $sourceID, $sourceType, $text); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Adds a single translatable string. |
57
|
|
|
* |
58
|
|
|
* @param Localization_Scanner_StringInfo $string |
59
|
|
|
* @return Localization_Scanner_StringsCollection |
60
|
|
|
*/ |
61
|
|
|
protected function add(Localization_Scanner_StringInfo $string) : Localization_Scanner_StringsCollection |
62
|
|
|
{ |
63
|
|
|
$hash = $string->getHash(); |
64
|
|
|
|
65
|
|
|
if(!isset($this->hashes[$hash])) { |
66
|
|
|
$this->hashes[$hash] = new Localization_Scanner_StringHash($this, $hash); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->hashes[$hash]->addString($string); |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Retrieves all available translatable strings, |
75
|
|
|
* grouped by their hash to identify unique strings. |
76
|
|
|
* |
77
|
|
|
* @return Localization_Scanner_StringHash[] |
78
|
|
|
*/ |
79
|
|
|
public function getHashes() : array |
80
|
|
|
{ |
81
|
|
|
return array_values($this->hashes); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function hashExists(string $hash) : bool |
85
|
|
|
{ |
86
|
|
|
return isset($this->hashes[$hash]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $hash |
91
|
|
|
* @return Localization_Scanner_StringHash |
92
|
|
|
* @throws Localization_Exception |
93
|
|
|
*/ |
94
|
|
|
public function getHash(string $hash) : Localization_Scanner_StringHash |
95
|
|
|
{ |
96
|
|
|
if(isset($this->hashes[$hash])) { |
97
|
|
|
return $this->hashes[$hash]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
throw new Localization_Exception( |
101
|
|
|
'Unknown string hash', |
102
|
|
|
sprintf('Could not find string by hash [%s].', $hash), |
103
|
|
|
self::ERROR_UNKNOWN_STRING_HASH |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array<string,mixed> |
109
|
|
|
*/ |
110
|
|
|
public function toArray() : array |
111
|
|
|
{ |
112
|
|
|
$data = array( |
113
|
|
|
'formatVersion' => self::STORAGE_FORMAT_VERSION, |
114
|
|
|
'hashes' => array(), |
115
|
|
|
'warnings' => array() |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
foreach($this->hashes as $hash) |
119
|
|
|
{ |
120
|
|
|
$data['hashes'] = array_merge($data['hashes'], $hash->toArray()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$data['warnings'] = $this->warnings; |
124
|
|
|
|
125
|
|
|
return $data; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param array<int|string,mixed> $array |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
|
|
public function fromArray(array $array) : bool |
133
|
|
|
{ |
134
|
|
|
if(!isset($array['formatVersion']) || $array['formatVersion'] != self::STORAGE_FORMAT_VERSION) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
foreach($array['hashes'] as $entry) |
139
|
|
|
{ |
140
|
|
|
$string = Localization_Scanner_StringInfo::fromArray($this, $entry); |
141
|
|
|
$this->add($string); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$this->warnings = $array['warnings']; |
145
|
|
|
|
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Whether the parser reported warnings during the |
151
|
|
|
* search for translatable texts. |
152
|
|
|
* |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public function hasWarnings() : bool |
156
|
|
|
{ |
157
|
|
|
return !empty($this->warnings); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Retrieves the amount of warnings. |
162
|
|
|
* @return int |
163
|
|
|
*/ |
164
|
|
|
public function countWarnings() : int |
165
|
|
|
{ |
166
|
|
|
return count($this->warnings); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Retrieves all warning messages that were added |
171
|
|
|
* during the search for translatable texts, if any. |
172
|
|
|
* |
173
|
|
|
* @return Localization_Scanner_StringsCollection_Warning[] |
174
|
|
|
*/ |
175
|
|
|
public function getWarnings() : array |
176
|
|
|
{ |
177
|
|
|
$result = array(); |
178
|
|
|
|
179
|
|
|
foreach($this->warnings as $def) { |
180
|
|
|
$result[] = new Localization_Scanner_StringsCollection_Warning($def); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $result; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function countHashes() : int |
187
|
|
|
{ |
188
|
|
|
return count($this->hashes); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function countFiles() : int |
192
|
|
|
{ |
193
|
|
|
$amount = 0; |
194
|
|
|
foreach($this->hashes as $hash) { |
195
|
|
|
$amount = $amount + $hash->countFiles(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $amount; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Retrieves all string hashed for the specified source. |
203
|
|
|
* |
204
|
|
|
* @param string $id |
205
|
|
|
* @return Localization_Scanner_StringHash[] |
206
|
|
|
*/ |
207
|
|
|
public function getHashesBySourceID(string $id) : array |
208
|
|
|
{ |
209
|
|
|
$hashes = array(); |
210
|
|
|
|
211
|
|
|
foreach($this->hashes as $hash) { |
212
|
|
|
if($hash->hasSourceID($id)) { |
213
|
|
|
$hashes[] = $hash; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $hashes; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Retrieves all hashes for the specified language ID. |
222
|
|
|
* |
223
|
|
|
* @param string $languageID The language ID, e.g. "PHP" |
224
|
|
|
* @return Localization_Scanner_StringHash[] |
225
|
|
|
*/ |
226
|
|
|
public function getHashesByLanguageID(string $languageID) : array |
227
|
|
|
{ |
228
|
|
|
$hashes = array(); |
229
|
|
|
|
230
|
|
|
foreach($this->hashes as $hash) { |
231
|
|
|
if($hash->hasLanguageType($languageID)) { |
232
|
|
|
$hashes[] = $hash; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $hashes; |
237
|
|
|
} |
238
|
|
|
} |