1+ <?php
2+ /**
3+ * This file is part of PHPOffice Common
4+ *
5+ * PHPOffice Common is free software distributed under the terms of the GNU Lesser
6+ * General Public License version 3 as published by the Free Software Foundation.
7+ *
8+ * For the full copyright and license information, please read the LICENSE
9+ * file that was distributed with this source code. For the full list of
10+ * contributors, visit https://github.com/PHPOffice/Common/contributors.
11+ *
12+ * @link https://github.com/PHPOffice/Common
13+ * @copyright 2009-2014 PHPOffice Common contributors
14+ * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
15+ */
16+
17+ namespace PhpOffice \Common ;
18+
19+ /**
20+ * XML Reader wrapper
21+ *
22+ * @since 0.2.1
23+ */
24+ class XMLReader
25+ {
26+ /**
27+ * DOMDocument object
28+ *
29+ * @var \DOMDocument
30+ */
31+ private $ dom = null ;
32+
33+ /**
34+ * DOMXpath object
35+ *
36+ * @var \DOMXpath
37+ */
38+ private $ xpath = null ;
39+
40+ /**
41+ * Get DOMDocument from ZipArchive
42+ *
43+ * @param string $zipFile
44+ * @param string $xmlFile
45+ * @return \DOMDocument|false
46+ * @throws \Exception
47+ */
48+ public function getDomFromZip ($ zipFile , $ xmlFile )
49+ {
50+ if (file_exists ($ zipFile ) === false ) {
51+ throw new \Exception ('Cannot find archive file. ' );
52+ }
53+
54+ $ zip = new ZipArchive ();
55+ $ zip ->open ($ zipFile );
56+ $ content = $ zip ->getFromName ($ xmlFile );
57+ $ zip ->close ();
58+
59+ if ($ content === false ) {
60+ return false ;
61+ } else {
62+ return $ this ->getDomFromString ($ content );
63+ }
64+ }
65+
66+ /**
67+ * Get DOMDocument from content string
68+ *
69+ * @param string $content
70+ * @return \DOMDocument
71+ */
72+ public function getDomFromString ($ content )
73+ {
74+ $ this ->dom = new \DOMDocument ();
75+ $ this ->dom ->loadXML ($ content );
76+
77+ return $ this ->dom ;
78+ }
79+
80+ /**
81+ * Get elements
82+ *
83+ * @param string $path
84+ * @param \DOMElement $contextNode
85+ * @return \DOMNodeList
86+ */
87+ public function getElements ($ path , \DOMElement $ contextNode = null )
88+ {
89+ if ($ this ->dom === null ) {
90+ return array ();
91+ }
92+ if ($ this ->xpath === null ) {
93+ $ this ->xpath = new \DOMXpath ($ this ->dom );
94+ }
95+
96+ if (is_null ($ contextNode )) {
97+ return $ this ->xpath ->query ($ path );
98+ } else {
99+ return $ this ->xpath ->query ($ path , $ contextNode );
100+ }
101+ }
102+
103+ /**
104+ * Get element
105+ *
106+ * @param string $path
107+ * @param \DOMElement $contextNode
108+ * @return \DOMElement|null
109+ */
110+ public function getElement ($ path , \DOMElement $ contextNode = null )
111+ {
112+ $ elements = $ this ->getElements ($ path , $ contextNode );
113+ if ($ elements ->length > 0 ) {
114+ return $ elements ->item (0 );
115+ } else {
116+ return null ;
117+ }
118+ }
119+
120+ /**
121+ * Get element attribute
122+ *
123+ * @param string $attribute
124+ * @param \DOMElement $contextNode
125+ * @param string $path
126+ * @return string|null
127+ */
128+ public function getAttribute ($ attribute , \DOMElement $ contextNode = null , $ path = null )
129+ {
130+ $ return = null ;
131+ if ($ path !== null ) {
132+ $ elements = $ this ->getElements ($ path , $ contextNode );
133+ if ($ elements ->length > 0 ) {
134+ /** @var \DOMElement $node Type hint */
135+ $ node = $ elements ->item (0 );
136+ $ return = $ node ->getAttribute ($ attribute );
137+ }
138+ } else {
139+ if ($ contextNode !== null ) {
140+ $ return = $ contextNode ->getAttribute ($ attribute );
141+ }
142+ }
143+
144+ return ($ return == '' ) ? null : $ return ;
145+ }
146+
147+ /**
148+ * Get element value
149+ *
150+ * @param string $path
151+ * @param \DOMElement $contextNode
152+ * @return string|null
153+ */
154+ public function getValue ($ path , \DOMElement $ contextNode = null )
155+ {
156+ $ elements = $ this ->getElements ($ path , $ contextNode );
157+ if ($ elements ->length > 0 ) {
158+ return $ elements ->item (0 )->nodeValue ;
159+ } else {
160+ return null ;
161+ }
162+ }
163+
164+ /**
165+ * Count elements
166+ *
167+ * @param string $path
168+ * @param \DOMElement $contextNode
169+ * @return integer
170+ */
171+ public function countElements ($ path , \DOMElement $ contextNode = null )
172+ {
173+ $ elements = $ this ->getElements ($ path , $ contextNode );
174+
175+ return $ elements ->length ;
176+ }
177+
178+ /**
179+ * Element exists
180+ *
181+ * @param string $path
182+ * @param \DOMElement $contextNode
183+ * @return boolean
184+ */
185+ public function elementExists ($ path , \DOMElement $ contextNode = null )
186+ {
187+ return $ this ->getElements ($ path , $ contextNode )->length > 0 ;
188+ }
189+ }
0 commit comments