1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CHMLib\TOCIndex; |
4
|
|
|
|
5
|
|
|
use CHMLib\CHM; |
6
|
|
|
use CHMLib\Map; |
7
|
|
|
use DOMElement; |
8
|
|
|
use Exception; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A list of items in the TOC or in the Index of an CHM file. |
12
|
|
|
*/ |
13
|
|
|
class Item |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The parent CHM instance. |
17
|
|
|
* |
18
|
|
|
* @var CHM |
19
|
|
|
*/ |
20
|
|
|
protected $chm; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The name of the tree item. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The keyword of the tree item. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $keyword; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The value of the "See Also" parameter. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $seeAlso; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The local path to the tree item. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $local; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The URL to the tree item. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $url; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The path to an entry in another CHM file. |
59
|
|
|
* |
60
|
|
|
* @var array|null If not null, it's an array with two keys: 'chm' and 'entry'. |
61
|
|
|
*/ |
62
|
|
|
protected $merge; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The image number attribute. |
66
|
|
|
* |
67
|
|
|
* @var int|null |
68
|
|
|
*/ |
69
|
|
|
protected $imageNumber; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The sub-elements of this Item. |
73
|
|
|
* |
74
|
|
|
* @var Tree |
75
|
|
|
*/ |
76
|
|
|
protected $children; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Initializes the instance. |
80
|
|
|
* |
81
|
|
|
* @param CHM $chm The parent CHM instance. |
82
|
|
|
* @param DOMElement $object The OBJECT element. |
83
|
|
|
* |
84
|
|
|
* @throws Exception Throw an Exception in case of errors. |
85
|
|
|
* |
86
|
|
|
* @return static |
|
|
|
|
87
|
|
|
*/ |
88
|
|
|
public function __construct(CHM $chm, DOMElement $object) |
89
|
|
|
{ |
90
|
|
|
$this->chm = $chm; |
91
|
|
|
$this->name = ''; |
92
|
|
|
$this->keyword = ''; |
93
|
|
|
$this->seeAlso = ''; |
94
|
|
|
$this->local = ''; |
95
|
|
|
$this->url = ''; |
96
|
|
|
$this->merge = null; |
97
|
|
|
$this->imageNumber = null; |
98
|
|
|
$this->children = new Tree(); |
99
|
|
|
foreach ($object->childNodes as $p) { |
100
|
|
|
if ($p instanceof DOMElement && strcasecmp($p->tagName, 'param') === 0) { |
101
|
|
|
$name = trim((string) $p->getAttribute('name')); |
102
|
|
|
$value = trim((string) $p->getAttribute('value')); |
103
|
|
|
switch (strtolower($name)) { |
104
|
|
|
case 'name': |
105
|
|
|
// Multiple values are allowed: we keep only the last one |
106
|
|
|
$this->name = $value; |
107
|
|
|
break; |
108
|
|
|
case 'keyword': |
109
|
|
|
$this->keyword = $value; |
110
|
|
|
break; |
111
|
|
|
case 'see also': |
112
|
|
|
$this->seeAlso = $value; |
113
|
|
|
break; |
114
|
|
|
case 'local': |
115
|
|
|
$this->local = '/'.str_replace('\\', '/', $value); |
116
|
|
|
break; |
117
|
|
|
case 'url': |
118
|
|
|
$this->url = $value; |
119
|
|
|
break; |
120
|
|
|
case 'merge': |
121
|
|
|
if (!preg_match('%^([^:\\\\/]+)::(.+\.hh[ck])$%i', $value, $matches)) { |
122
|
|
|
throw new Exception("Invalid value of the '$name' attribute: $value"); |
123
|
|
|
} |
124
|
|
|
$this->merge = array('chm' => $matches[1], 'entry' => '/'.ltrim(str_replace('\\', '/', $matches[2]), '/')); |
125
|
|
|
break; |
126
|
|
|
case 'imagenumber': |
127
|
|
|
if (is_numeric($value)) { |
128
|
|
|
$this->imageNumber = (int) $value; |
129
|
|
|
} elseif ($value !== '') { |
130
|
|
|
throw new Exception("Invalid value of the '$name' attribute: $value"); |
131
|
|
|
} |
132
|
|
|
break; |
133
|
|
|
default: |
134
|
|
|
throw new Exception("Unknown parameter name '$name' of a tree item (value: '$value')"); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the name of the tree item. |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getName() |
146
|
|
|
{ |
147
|
|
|
return $this->name; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the keyword of the tree item. |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public function getKeyword() |
156
|
|
|
{ |
157
|
|
|
return $this->keyword; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the value of the "See Also" parameter. |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getSeeAlso() |
166
|
|
|
{ |
167
|
|
|
return $this->seeAlso; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the local path to the tree item. |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
public function getLocal() |
176
|
|
|
{ |
177
|
|
|
return $this->local; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get the URL to the tree item. |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public function getURL() |
186
|
|
|
{ |
187
|
|
|
return $this->url; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get the path to an entry in another CHM file. |
192
|
|
|
* |
193
|
|
|
* @var array|null If not null, it's an array with two keys: 'chm' and 'entry'. |
194
|
|
|
*/ |
195
|
|
|
public function getMerge() |
196
|
|
|
{ |
197
|
|
|
return $this->merge; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the image number attribute. |
202
|
|
|
* |
203
|
|
|
* @return int|null |
204
|
|
|
*/ |
205
|
|
|
public function getImageNumber() |
206
|
|
|
{ |
207
|
|
|
return $this->imageNumber; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get the sub-elements of this Item. |
212
|
|
|
* |
213
|
|
|
* @return Tree |
214
|
|
|
*/ |
215
|
|
|
public function getChildren() |
216
|
|
|
{ |
217
|
|
|
return $this->children; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Resolve the items contained in other CHM files. |
222
|
|
|
* |
223
|
|
|
* @param Map $map |
224
|
|
|
* |
225
|
|
|
* @throws Exception Throw an Exception in case of errors. |
226
|
|
|
* |
227
|
|
|
* @return static[] |
228
|
|
|
*/ |
229
|
|
|
public function resolve(Map $map) |
230
|
|
|
{ |
231
|
|
|
if ($this->merge === null) { |
232
|
|
|
$result = array($this); |
233
|
|
|
} else { |
234
|
|
|
$chm = $map->get($this->merge['chm']); |
235
|
|
|
if ($chm === null) { |
236
|
|
|
throw new Exception("Missing CHM reference from map: {$this->merge['chm']}"); |
237
|
|
|
} |
238
|
|
|
$entry = $chm->getEntryByPath($this->merge['entry']); |
239
|
|
|
if ($entry === null) { |
240
|
|
|
throw new Exception("Missing entry '{$this->merge['entry']}' in CHM file {$this->merge['chm']}"); |
241
|
|
|
} |
242
|
|
|
$tree = Tree::fromString($chm, $entry->getContents()); |
243
|
|
|
$tree->resolve($map); |
244
|
|
|
$result = $tree->getItems(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return $result; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.