Completed
Push — 1.x ( a3ba31...8bb1d5 )
by Mikaël
33:28
created

AbstractDomDocumentHandler::initRootElement()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4
Metric Value
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 8
nc 4
nop 0
crap 4
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\DomHandler;
4
5
abstract class AbstractDomDocumentHandler
6
{
7
    /**
8
     * @var \DOMDocument
9
     */
10
    protected $domDocument;
11
    /**
12
     * @var ElementHandler
13
     */
14
    protected $rootElement;
15
    /**
16
     * @param \DOMDocument $domDocument
17
     */
18 540
    public function __construct(\DOMDocument $domDocument)
19
    {
20 540
        $this->domDocument = $domDocument;
21 540
        $this->initRootElement();
22 532
    }
23
    /**
24
     * Find valid root node (not a comment, at least a DOMElement node)
25
     * @throws \InvalidArgumentException
26
     */
27 540
    protected function initRootElement()
28
    {
29 540
        if ($this->domDocument->hasChildNodes()) {
30 532
            foreach ($this->domDocument->childNodes as $node) {
31 532
                if ($node instanceof \DOMElement) {
32 532
                    $this->rootElement = $this->getElementHandler($node, $this);
33 532
                    break;
34
                }
35 532
            }
36 532
        } else {
37 8
            throw new \InvalidArgumentException('Document seems to be invalid', __LINE__);
38
        }
39 532
    }
40
    /**
41
     * Return the matching node handler based on current \DomNode type
42
     * @param \DOMNode|\DOMNameSpaceNode $node
43
     * @param int $index
44
     * @return NodeHandler|ElementHandler|AttributeHandler|NameSpaceHandler
45
     */
46 660
    public function getHandler($node, $index = -1)
47
    {
48 660
        if ($node instanceof \DOMElement) {
49 624
            return $this->getElementHandler($node, $this, $index);
50 620
        } elseif ($node instanceof \DOMAttr) {
51 592
            return $this->getAttributeHandler($node, $this, $index);
52 88
        } elseif ($node instanceof \DOMNameSpaceNode) {
53 72
            return new NameSpaceHandler($node, $this, $index);
54
        }
55 16
        return $this->getNodeHandler($node, $this, $index);
56
    }
57
    /**
58
     * @param \DOMNode $node
59
     * @param AbstractDomDocumentHandler $domDocument
60
     * @param int $index
61
     * @return NodeHandler
62
     */
63
    abstract protected function getNodeHandler(\DOMNode $node, AbstractDomDocumentHandler $domDocument, $index = -1);
64
    /**
65
     * @param \DOMElement $element
66
     * @param AbstractDomDocumentHandler $domDocument
67
     * @param int $index
68
     * @return ElementHandler
69
     */
70
    abstract protected function getElementHandler(\DOMElement $element, AbstractDomDocumentHandler $domDocument, $index = -1);
71
    /**
72
     * @param \DOMAttr $attribute
73
     * @param AbstractDomDocumentHandler $domDocument
74
     * @param int $index
75
     * @return AttributeHandler
76
     */
77
    abstract protected function getAttributeHandler(\DOMAttr $attribute, AbstractDomDocumentHandler $domDocument, $index = -1);
78
    /**
79
     * @param string $name
80
     * @return NodeHandler
81
     */
82 108
    public function getNodeByName($name)
83
    {
84 108
        return $this->domDocument->getElementsByTagName($name)->length > 0 ? $this->getNodeHandler($this->domDocument->getElementsByTagName($name)->item(0), $this) : null;
85
    }
86
    /**
87
     * @param string $name
88
     * @return ElementHandler
89
     */
90 76
    public function getElementByName($name)
91
    {
92 76
        $node = $this->getNodeByName($name);
93 76
        if ($node instanceof AbstractNodeHandler && $node->getNode() instanceof \DOMElement) {
94 76
            return $this->getElementHandler($node->getNode(), $this);
95
        }
96 4
        return null;
97
    }
98
    /**
99
     * @param string $name
100
     * @param string $checkInstance
101
     * @return NodeHandler[]
102
     */
103 600
    public function getNodesByName($name, $checkInstance = null)
104
    {
105 600
        $nodes = array();
106 600
        if ($this->domDocument->getElementsByTagName($name)->length > 0) {
107 600
            foreach ($this->domDocument->getElementsByTagName($name) as $node) {
108 600
                if ($checkInstance === null || $node instanceof $checkInstance) {
109 600
                    $nodes[] = $this->getHandler($node, count($nodes));
110 600
                }
111 600
            }
112 600
        }
113 600
        return $nodes;
114
    }
115
    /**
116
     * @param string $name
117
     * @return ElementHandler[]
118
     */
119 596
    public function getElementsByName($name)
120
    {
121 596
        return $this->getNodesByName($name, 'DOMElement');
122
    }
123
    /**
124
     * @param string $name
125
     * @param array $attributes
126
     * @param \DOMNode $node
127
     * @return ElementHandler[]
128
     */
129 200
    public function getElementsByNameAndAttributes($name, array $attributes, \DOMNode $node = null)
130
    {
131 200
        $matchingElements = $this->getElementsByName($name);
132 200
        if ((!empty($attributes) || $node instanceof \DOMNode) && !empty($matchingElements)) {
133 200
            $nodes = $this->searchTagsByXpath($name, $attributes, $node);
134 200
            if (!empty($nodes)) {
135 200
                $matchingElements = $this->getElementsHandlers($nodes);
136 200
            }
137 200
        }
138 200
        return $matchingElements;
139
    }
140
    /**
141
     * @param \DOMNodeList $nodeList
142
     * @return ElementHandler[]
143
     */
144 288
    public function getElementsHandlers(\DOMNodeList $nodeList)
145
    {
146 288
        $nodes = array();
147 288
        if (!empty($nodeList)) {
148 288
            $index = 0;
149 288
            foreach ($nodeList as $node) {
150 288
                if ($node instanceof \DOMElement) {
151 288
                    $nodes[] = $this->getElementHandler($node, $this, $index);
152 288
                    $index++;
153 288
                }
154 288
            }
155 288
        }
156 288
        return $nodes;
157
    }
158
    /**
159
     * @param string $name
160
     * @param array $attributes
161
     * @param \DOMNode $node
162
     * @return \DOMNodeList
163
     */
164 200
    public function searchTagsByXpath($name, array $attributes, \DOMNode $node = null)
165
    {
166 200
        $xpath = new \DOMXPath($this->domDocument);
167 200
        $xQuery = sprintf("%s//*[local-name() = '%s']", $node instanceof \DOMNode ? '.' : '', $name);
168 200
        foreach ($attributes as $attributeName=>$attributeValue) {
169 200
            $xQuery .= sprintf("[@%s='%s']", $attributeName, $attributeValue);
170 200
        }
171 200
        return $xpath->query($xQuery, $node);
172
    }
173
    /**
174
     * @param string $name
175
     * @param array $attributes
176
     * @return null|ElementHandler
177
     */
178 184
    public function getElementByNameAndAttributes($name, array $attributes)
179
    {
180 184
        $elements = $this->getElementsByNameAndAttributes($name, $attributes);
181 184
        return empty($elements) ? null : array_shift($elements);
182
    }
183
}
184