1: <?php
2: /**
3: * This file is part of the FastFeed package.
4: *
5: * (c) Daniel González <daniel@desarrolla2.com>
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 FastFeed\Parser;
11:
12: use DOMElement;
13: use DOMDocument;
14:
15: /**
16: * AbstractDomParser
17: */
18: abstract class AbstractDomParser
19: {
20:
21: /**
22: * @param $content
23: *
24: * @return DOMDocument
25: * @throws \FastFeed\Exception\RuntimeException
26: */
27: protected function createDocumentFromXML($content)
28: {
29: $previousValue = libxml_use_internal_errors(true);
30:
31: $document = new DOMDocument();
32: $document->strictErrorChecking = false;
33: $document->loadXML(trim($content));
34:
35: libxml_use_internal_errors($previousValue);
36:
37: return $document;
38: }
39:
40: /**
41: * @param $content
42: *
43: * @return DOMDocument
44: * @throws \FastFeed\Exception\RuntimeException
45: */
46: protected function createDocumentFromHTML($content)
47: {
48: $previousValue = libxml_use_internal_errors(true);
49:
50: $document = new DOMDocument();
51: $document->strictErrorChecking = false;
52: $document->loadHTML(trim($content));
53:
54: libxml_use_internal_errors($previousValue);
55:
56: return $document;
57: }
58:
59: /**
60: * @param DOMElement $node
61: * @param $tagName
62: *
63: * @return bool|string
64: * @throws \FastFeed\Exception\RuntimeException
65: */
66: protected function getNodeValueByTagName(DOMElement $node, $tagName)
67: {
68: $results = $node->getElementsByTagName($tagName);
69: for ($i = 0; $i < $results->length; $i++) {
70: $result = $results->item($i);
71: if (!$result->nodeValue) {
72: continue;
73: }
74:
75: return $result->nodeValue;
76: }
77:
78: return false;
79: }
80:
81: /**
82: * @param DOMElement $node
83: * @param $namespace
84: * @param $tagName
85: *
86: * @return bool|string
87: * @throws \FastFeed\Exception\RuntimeException
88: */
89: protected function getNodeValueByTagNameNS(DOMElement $node, $namespace, $tagName)
90: {
91: $results = $node->getElementsByTagNameNS($namespace, $tagName);
92: for ($i = 0; $i < $results->length; $i++) {
93: $result = $results->item($i);
94: if (!$result->nodeValue) {
95: continue;
96: }
97:
98: return $result->nodeValue;
99: }
100:
101: return false;
102: }
103:
104: /**
105: * @param DOMElement $node
106: * @param $tagName
107: *
108: * @return array
109: * @throws \FastFeed\Exception\RuntimeException
110: */
111: protected function getNodeValuesByTagName(DOMElement $node, $tagName)
112: {
113: $values = array();
114: $results = $node->getElementsByTagName($tagName);
115: if ($results->length) {
116: foreach ($results as $result) {
117: if ($result->nodeValue) {
118: $values[] = $result->nodeValue;
119: }
120: }
121: }
122:
123: return $values;
124: }
125:
126: /**
127: * @param DOMElement $node
128: * @param $tagName
129: * @param $propertyName
130: *
131: * @return array
132: * @throws \FastFeed\Exception\RuntimeException
133: */
134: protected function getNodePropertyByTagName(\DOMElement $node, $tagName, $propertyName)
135: {
136: $values = array();
137: $results = $node->getElementsByTagName($tagName);
138: if ($results->length) {
139: foreach ($results as $result) {
140: if ($result->getAttribute($propertyName)) {
141: $values[] = $result->getAttribute($propertyName);
142: }
143: }
144: }
145:
146: return $values;
147: }
148: }
149: