1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppLocalize; |
4
|
|
|
|
5
|
|
|
use AppLocalize\Parser\Text; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Container for a single string hash: collects all instances |
9
|
|
|
* of the same string hash accross all places where the same |
10
|
|
|
* string has been found. |
11
|
|
|
* |
12
|
|
|
* @package Localization |
13
|
|
|
* @subpackage Scanner |
14
|
|
|
* @author Sebastian Mordziol <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Localization_Scanner_StringHash |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Localization_Scanner_StringsCollection |
20
|
|
|
*/ |
21
|
|
|
protected $collection; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $hash; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Localization_Scanner_StringInfo[] |
30
|
|
|
*/ |
31
|
|
|
protected $strings = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array<string,bool> |
35
|
|
|
*/ |
36
|
|
|
protected $sourceIDs = array(); |
37
|
|
|
|
38
|
|
|
public function __construct(Localization_Scanner_StringsCollection $collection, string $hash) |
39
|
|
|
{ |
40
|
|
|
$this->collection = $collection; |
41
|
|
|
$this->hash = $hash; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function addString(Localization_Scanner_StringInfo $string) : Localization_Scanner_StringHash |
45
|
|
|
{ |
46
|
|
|
$this->strings[] = $string; |
47
|
|
|
$this->sourceIDs[$string->getSourceID()] = true; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function toArray() : array |
53
|
|
|
{ |
54
|
|
|
$entries = array(); |
55
|
|
|
|
56
|
|
|
foreach($this->strings as $string) { |
57
|
|
|
$entries[] = $string->toArray(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $entries; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Retrieves all individual string locations where this text was found. |
65
|
|
|
* @return Localization_Scanner_StringInfo[] |
66
|
|
|
*/ |
67
|
|
|
public function getStrings() : array |
68
|
|
|
{ |
69
|
|
|
return $this->strings; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function countFiles() : int |
73
|
|
|
{ |
74
|
|
|
$amount = 0; |
75
|
|
|
|
76
|
|
|
foreach($this->strings as $string) { |
77
|
|
|
if($string->isFile()) { |
78
|
|
|
$amount++; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $amount; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function hasSourceID(string $id) : bool |
86
|
|
|
{ |
87
|
|
|
return isset($this->sourceIDs[$id]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function hasLanguageType(string $type) : bool |
91
|
|
|
{ |
92
|
|
|
foreach($this->strings as $string) { |
93
|
|
|
if($string->getLanguageType() == $type) { |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getText() : ?Text |
102
|
|
|
{ |
103
|
|
|
if(isset($this->strings[0])) { |
104
|
|
|
return $this->strings[0]->getText(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getHash() : string |
111
|
|
|
{ |
112
|
|
|
return $this->hash; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function isTranslated() : bool |
116
|
|
|
{ |
117
|
|
|
$translator = Localization::getTranslator(); |
118
|
|
|
return $translator->hashExists($this->getHash()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function countStrings() : int |
122
|
|
|
{ |
123
|
|
|
return count($this->strings); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Retrieves the translated text, if any. |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getTranslatedText() : string |
131
|
|
|
{ |
132
|
|
|
$translator = Localization::getTranslator(); |
133
|
|
|
$text = $translator->getHashTranslation($this->getHash()); |
134
|
|
|
|
135
|
|
|
if($text !== null) { |
136
|
|
|
return $text; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return ''; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Retrieves a list of all file names, with relative paths. |
144
|
|
|
* @return string[] |
145
|
|
|
*/ |
146
|
|
|
public function getFiles() : array |
147
|
|
|
{ |
148
|
|
|
$files = array(); |
149
|
|
|
|
150
|
|
|
foreach($this->strings as $string) |
151
|
|
|
{ |
152
|
|
|
if(!$string->isFile()) { |
153
|
|
|
continue; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$file = $string->getSourceFile(); |
157
|
|
|
if(!in_array($file, $files)) { |
158
|
|
|
$files[] = $file; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
sort($files); |
163
|
|
|
|
164
|
|
|
return $files; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Retrieves a list of all file names this string is used in. |
169
|
|
|
* @return string[] |
170
|
|
|
*/ |
171
|
|
|
public function getFileNames() : array |
172
|
|
|
{ |
173
|
|
|
$files = $this->getFiles(); |
174
|
|
|
$result = array(); |
175
|
|
|
|
176
|
|
|
foreach($files as $path) { |
177
|
|
|
$result[] = basename($path); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// some files may have the same name, there's no |
181
|
|
|
// sense in using duplicates in this context. |
182
|
|
|
return array_unique($result); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Retrieves a text comprised of all strings that are relevant |
187
|
|
|
* for a full text search, imploded together. Used in the search |
188
|
|
|
* function to find matching strings. |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function getSearchString() : string |
193
|
|
|
{ |
194
|
|
|
$parts = array($this->getTranslatedText(), $this->getTextAsString()); |
195
|
|
|
|
196
|
|
|
$parts = array_merge($parts, $this->getFiles()); |
197
|
|
|
|
198
|
|
|
return implode(' ', $parts); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function getTextAsString() : string |
202
|
|
|
{ |
203
|
|
|
$text = $this->getText(); |
204
|
|
|
|
205
|
|
|
if($text !== null) |
206
|
|
|
{ |
207
|
|
|
return $text->getText(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return ''; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|