1: <?php
2:
3: class HtmlDocument {
4:
5: /**
6: *
7: * @var DOMDocument
8: */
9: private $document;
10:
11: /**
12: *
13: * @var DOMNode
14: */
15: private $contextNode;
16:
17: public static function createFromContent($content) {
18: $domDocument = new DOMDocument;
19:
20: if (!@$domDocument->loadHTML($content)) {
21: throw new Exception("Falha ao tentar carregar conteúdo.");
22: }
23:
24: return new HtmlDocument($domDocument);
25: }
26:
27: /**
28: *
29: * @param string $url
30: * @return \HtmlDocument
31: * @throws Exception
32: */
33: public static function createFromUrl($url) {
34: $domDocument = new DOMDocument;
35:
36: if (!@$domDocument->loadHTMLFile($url)) {
37: throw new Exception("Falha ao tentar carregar \"$url\".");
38: }
39:
40: return new HtmlDocument($domDocument);
41: }
42:
43: /**
44: *
45: * @param DOMDocument $domDocument
46: * @param DOMNode $contextNode
47: */
48: private function __construct(DOMDocument $domDocument, DOMNode $contextNode = null) {
49: $this->document = $domDocument;
50: $this->contextNode = $contextNode;
51: }
52:
53: /**
54: *
55: * @param DOMNode $node
56: * @return \HtmlDocument
57: */
58: public function createFromNode(DOMNode $node) {
59: return new HtmlDocument($this->document, $node);
60: }
61:
62: /**
63: *
64: * @param string $xpathQuery
65: * @return DOMNodeList
66: * @throws Exception
67: */
68: public function queryNodes($xpathQuery) {
69: $xpath = new DOMXPath($this->document);
70: $xpathQuery = str_replace('x:', '', $xpathQuery);
71: $result = $xpath->query($xpathQuery, $this->contextNode);
72:
73: if (!$result) {
74: throw new Exception("Fail on XPath query \"$xpathQuery\".");
75: }
76:
77: return $result;
78: }
79:
80: /**
81: *
82: * @param string $xpathQuery
83: * @return \DOMNode
84: */
85: public function queryUniqueNode($xpathQuery) {
86: foreach ($this->queryNodes($xpathQuery) as $node) {
87: return $node;
88: }
89:
90: return null;
91: }
92:
93: /**
94: *
95: * @param string $xpathQuery
96: * @return array
97: */
98: public function queryValues($xpathQuery) {
99: $values = array();
100: foreach ($this->queryNodes($xpathQuery) as $node) {
101: $values[] = $node->nodeValue;
102: }
103:
104: return $values;
105: }
106:
107: /**
108: *
109: * @param string $xpathQuery
110: * @return string
111: */
112: public function queryUniqueValue($xpathQuery) {
113: foreach ($this->queryValues($xpathQuery) as $value) {
114: return $value;
115: }
116:
117: return null;
118: }
119:
120: /**
121: *
122: * @return DOMDocument
123: */
124: public function getDocument() {
125: return $this->document;
126: }
127:
128: /**
129: *
130: * @return DOMNode
131: */
132: public function getContextNode() {
133: return $this->contextNode;
134: }
135:
136: }
137: