| 148 |
lars |
1 |
<?php declare(strict_types = 1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of PharIo\Manifest.
|
|
|
4 |
*
|
|
|
5 |
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
namespace PharIo\Manifest;
|
|
|
11 |
|
|
|
12 |
use DOMElement;
|
|
|
13 |
use DOMNodeList;
|
|
|
14 |
|
|
|
15 |
class ManifestElement {
|
|
|
16 |
public const XMLNS = 'https://phar.io/xml/manifest/1.0';
|
|
|
17 |
|
|
|
18 |
/** @var DOMElement */
|
|
|
19 |
private $element;
|
|
|
20 |
|
|
|
21 |
public function __construct(DOMElement $element) {
|
|
|
22 |
$this->element = $element;
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
protected function getAttributeValue(string $name): string {
|
|
|
26 |
if (!$this->element->hasAttribute($name)) {
|
|
|
27 |
throw new ManifestElementException(
|
|
|
28 |
\sprintf(
|
|
|
29 |
'Attribute %s not set on element %s',
|
|
|
30 |
$name,
|
|
|
31 |
$this->element->localName
|
|
|
32 |
)
|
|
|
33 |
);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
return $this->element->getAttribute($name);
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
protected function getChildByName(string $elementName): DOMElement {
|
|
|
40 |
$element = $this->element->getElementsByTagNameNS(self::XMLNS, $elementName)->item(0);
|
|
|
41 |
|
|
|
42 |
if (!$element instanceof DOMElement) {
|
|
|
43 |
throw new ManifestElementException(
|
|
|
44 |
\sprintf('Element %s missing', $elementName)
|
|
|
45 |
);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
return $element;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
protected function getChildrenByName(string $elementName): DOMNodeList {
|
|
|
52 |
$elementList = $this->element->getElementsByTagNameNS(self::XMLNS, $elementName);
|
|
|
53 |
|
|
|
54 |
if ($elementList->length === 0) {
|
|
|
55 |
throw new ManifestElementException(
|
|
|
56 |
\sprintf('Element(s) %s missing', $elementName)
|
|
|
57 |
);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
return $elementList;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
protected function hasChild(string $elementName): bool {
|
|
|
64 |
return $this->element->getElementsByTagNameNS(self::XMLNS, $elementName)->length !== 0;
|
|
|
65 |
}
|
|
|
66 |
}
|