Completed
Push — master ( 9157af...94c184 )
by Michele
04:14
created

Item::getWindowName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
     * Is this item marked as new?
31
     *
32
     * @var bool
33
     */
34
    protected $isNew;
35
36
    /**
37
     * The comment of the tree item.
38
     *
39
     * @var string
40
     */
41
    protected $comment;
42
43
    /**
44
     * The keyword of the tree item.
45
     *
46
     * @var string
47
     */
48
    protected $keyword;
49
50
    /**
51
     * The value of the "See Also" parameter.
52
     *
53
     * @var string
54
     */
55
    protected $seeAlso;
56
57
    /**
58
     * The local path to the tree item.
59
     *
60
     * @var string
61
     */
62
    protected $local;
63
64
    /**
65
     * The URL to the tree item.
66
     *
67
     * @var string
68
     */
69
    protected $url;
70
71
    /**
72
     * The frame name for this item.
73
     *
74
     * @var string;
75
     */
76
    protected $frameName;
77
78
    /**
79
     * The window name for this item.
80
     *
81
     * @var string;
82
     */
83
    protected $windowName;
84
85
    /**
86
     * The path to an entry in another CHM file.
87
     *
88
     * @var string|array|null If it's an array, it has two keys: 'chm' and 'entry'.
89
     */
90
    protected $merge;
91
92
    /**
93
     * The image number attribute.
94
     *
95
     * @var int|null
96
     */
97
    protected $imageNumber;
98
99
    /**
100
     * The sub-elements of this Item.
101
     *
102
     * @var Tree
103
     */
104
    protected $children;
105
106
    /**
107
     * Initializes the instance.
108
     *
109
     * @param CHM $chm The parent CHM instance.
110
     * @param DOMElement $object The OBJECT element.
111
     *
112
     * @throws Exception Throw an Exception in case of errors.
113
     *
114
     * @return static
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

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.

Loading history...
115
     */
116 1
    public function __construct(CHM $chm, DOMElement $object)
117
    {
118 1
        $this->chm = $chm;
119 1
        $this->name = '';
120 1
        $this->isNew = false;
121 1
        $this->comment = '';
122 1
        $this->keyword = '';
123 1
        $this->seeAlso = '';
124 1
        $this->local = '';
125 1
        $this->url = '';
126 1
        $this->frameName = '';
127 1
        $this->windowName = '';
128 1
        $this->merge = null;
129 1
        $this->imageNumber = null;
130 1
        $this->children = new Tree();
131 1
        foreach ($object->childNodes as $p) {
132 1
            if ($p instanceof DOMElement && strcasecmp($p->tagName, 'param') === 0) {
133 1
                $name = trim((string) $p->getAttribute('name'));
134 1
                $value = trim((string) $p->getAttribute('value'));
135 1
                switch (strtolower($name)) {
136 1
                    case 'name':
137
                        // Multiple values are allowed: we keep only the last one
138 1
                        $this->name = $value;
139 1
                        break;
140 1
                    case 'new':
141
                        $this->isNew = !empty($value);
142
                        break;
143 1
                    case 'comment':
144
                        $this->comment = $value;
145
                        break;
146 1
                    case 'keyword':
147
                        $this->keyword = $value;
148
                        break;
149 1
                    case 'see also':
150
                        $this->seeAlso = $value;
151
                        break;
152 1
                    case 'local':
153 1
                        $this->local = '/'.str_replace('\\', '/', str_replace('%20', ' ', $value));
154 1
                        break;
155 1
                    case 'url':
156
                        $this->url = $value;
157
                        break;
158 1
                    case 'framename':
159
                        $this->frameName = $value;
160
                        break;
161 1
                    case 'windowname':
162
                        $this->windowName = $value;
163
                        break;
164 1
                    case 'merge':
165 1
                        if ($value !== '') {
166 1
                            if (preg_match('%^([^:\\\\/]+.chm)::(.+)$%i', $value, $matches)) {
167 1
                                $this->merge = array('chm' => $matches[1], 'entry' => '/'.ltrim(str_replace('\\', '/', $matches[2]), '/'));
168
                            } else {
169
                                $this->merge = $value;
170
                            }
171
                        }
172 1
                        break;
173
                    case 'imagenumber':
174
                        if (is_numeric($value)) {
175
                            $this->imageNumber = (int) $value;
176
                        } elseif ($value !== '') {
177
                            throw new Exception("Invalid value of the '$name' attribute: $value");
178
                        }
179
                        break;
180
                    default:
181 1
                        throw new Exception("Unknown parameter name '$name' of a tree item (value: '$value')");
182
                }
183
            }
184
        }
185 1
    }
186
187
    /**
188
     * Get the name of the tree item.
189
     *
190
     * @return string
191
     */
192 1
    public function getName()
193
    {
194 1
        return $this->name;
195
    }
196
197
    /**
198
     * Is this item marked as new?
199
     *
200
     * @return bool
201
     */
202
    public function isNew()
203
    {
204
        return $this->isNew;
205
    }
206
    
207
    /**
208
     * Get the comment of the tree item.
209
     *
210
     * @return string
211
     */
212
    public function getComment()
213
    {
214
        return $this->comment;
215
    }
216
217
    /**
218
     * Get the keyword of the tree item.
219
     *
220
     * @return string
221
     */
222
    public function getKeyword()
223
    {
224
        return $this->keyword;
225
    }
226
227
    /**
228
     * Get the value of the "See Also" parameter.
229
     *
230
     * @return string
231
     */
232
    public function getSeeAlso()
233
    {
234
        return $this->seeAlso;
235
    }
236
237
    /**
238
     * Get the local path to the tree item.
239
     *
240
     * @return string
241
     */
242
    public function getLocal()
243
    {
244
        return $this->local;
245
    }
246
247
    /**
248
     * Get the URL to the tree item.
249
     *
250
     * @return string
251
     */
252 1
    public function getURL()
253
    {
254 1
        return $this->url;
255
    }
256
257
    /**
258
     * Get the frame name for this item.
259
     *
260
     * @return string
261
     */
262
    public function getFrameName()
263
    {
264
        return $this->frameName;
265
    }
266
267
    /**
268
     * Get the window name for this item.
269
     *
270
     * @return string
271
     */
272
    public function getWindowName()
273
    {
274
        return $this->frameName;
275
    }
276
277
    /**
278
     * Get the path to an entry in another CHM file.
279
     *
280
     * @var array|null If not null, it's an array with two keys: 'chm' and 'entry'.
281
     */
282 1
    public function getMerge()
283
    {
284 1
        return $this->merge;
285
    }
286
287
    /**
288
     * Get the image number attribute.
289
     *
290
     * @return int|null
291
     */
292
    public function getImageNumber()
293
    {
294
        return $this->imageNumber;
295
    }
296
297
    /**
298
     * Get the sub-elements of this Item.
299
     *
300
     * @return Tree
301
     */
302 1
    public function getChildren()
303
    {
304 1
        return $this->children;
305
    }
306
307
    /**
308
     * Resolve the items contained in other CHM files.
309
     *
310
     * @param Map $map
311
     *
312
     * @throws Exception Throw an Exception in case of errors.
313
     *
314
     * @return static[]
315
     */
316 1
    public function resolve(Map $map)
317
    {
318 1
        if (is_array($this->merge)) {
319 1
            $chm = $map->get($this->merge['chm']);
320 1
            if ($chm === null) {
321
                throw new Exception("Missing CHM reference from map: {$this->merge['chm']}");
322
            }
323 1
            $entry = $chm->getEntryByPath($this->merge['entry']);
324 1
            if ($entry === null) {
325
                throw new Exception("Missing entry '{$this->merge['entry']}' in CHM file {$this->merge['chm']}");
326
            }
327 1
            $tree = Tree::fromString($chm, $entry->getContents());
328 1
            $tree->resolve($map);
329 1
            $result = $tree->getItems();
330
        } else {
331 1
            $result = array($this);
332
        }
333 1
        foreach ($result as $newItem) {
334 1
            $newItem->children->resolve($map);
335
        }
336
337 1
        return $result;
338
    }
339
}
340