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