xref: /PHP-7.4/ext/simplexml/sxe.c (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Marcus Boerger <helly@php.net>                              |
16    +----------------------------------------------------------------------+
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22 
23 #include "php.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26 #include "zend_interfaces.h"
27 
28 #include "php_simplexml.h"
29 #include "ext/spl/php_spl.h"
30 #include "ext/spl/spl_iterators.h"
31 #include "sxe.h"
32 
33 PHP_SXE_API zend_class_entry *ce_SimpleXMLIterator = NULL;
34 PHP_SXE_API zend_class_entry *ce_SimpleXMLElement;
35 
36 #include "php_simplexml_exports.h"
37 
38 /* {{{ proto void SimpleXMLIterator::rewind()
39  Rewind to first element */
PHP_METHOD(ce_SimpleXMLIterator,rewind)40 PHP_METHOD(ce_SimpleXMLIterator, rewind)
41 {
42 	if (zend_parse_parameters_none() == FAILURE) {
43 		return;
44 	}
45 
46 	php_sxe_rewind_iterator(Z_SXEOBJ_P(ZEND_THIS));
47 }
48 /* }}} */
49 
50 /* {{{ proto bool SimpleXMLIterator::valid()
51  Check whether iteration is valid */
PHP_METHOD(ce_SimpleXMLIterator,valid)52 PHP_METHOD(ce_SimpleXMLIterator, valid)
53 {
54 	php_sxe_object *sxe = Z_SXEOBJ_P(ZEND_THIS);
55 
56 	if (zend_parse_parameters_none() == FAILURE) {
57 		return;
58 	}
59 
60 	RETURN_BOOL(!Z_ISUNDEF(sxe->iter.data));
61 }
62 /* }}} */
63 
64 /* {{{ proto SimpleXMLIterator SimpleXMLIterator::current()
65  Get current element */
PHP_METHOD(ce_SimpleXMLIterator,current)66 PHP_METHOD(ce_SimpleXMLIterator, current)
67 {
68 	php_sxe_object *sxe = Z_SXEOBJ_P(ZEND_THIS);
69 	zval *data;
70 
71 	if (zend_parse_parameters_none() == FAILURE) {
72 		return;
73 	}
74 
75 	if (Z_ISUNDEF(sxe->iter.data)) {
76 		return; /* return NULL */
77 	}
78 
79 	data = &sxe->iter.data;
80 	ZVAL_COPY_DEREF(return_value, data);
81 }
82 /* }}} */
83 
84 /* {{{ proto string SimpleXMLIterator::key()
85  Get name of current child element */
PHP_METHOD(ce_SimpleXMLIterator,key)86 PHP_METHOD(ce_SimpleXMLIterator, key)
87 {
88 	xmlNodePtr curnode;
89 	php_sxe_object *intern;
90 	php_sxe_object *sxe = Z_SXEOBJ_P(ZEND_THIS);
91 
92 	if (zend_parse_parameters_none() == FAILURE) {
93 		return;
94 	}
95 
96 	if (Z_ISUNDEF(sxe->iter.data)) {
97 		RETURN_FALSE;
98 	}
99 
100 	intern = Z_SXEOBJ_P(&sxe->iter.data);
101 	if (intern != NULL && intern->node != NULL) {
102 		curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->node)->node;
103 		RETURN_STRINGL((char*)curnode->name, xmlStrlen(curnode->name));
104 	}
105 
106 	RETURN_FALSE;
107 }
108 /* }}} */
109 
110 /* {{{ proto void SimpleXMLIterator::next()
111  Move to next element */
PHP_METHOD(ce_SimpleXMLIterator,next)112 PHP_METHOD(ce_SimpleXMLIterator, next)
113 {
114 	if (zend_parse_parameters_none() == FAILURE) {
115 		return;
116 	}
117 
118 	php_sxe_move_forward_iterator(Z_SXEOBJ_P(ZEND_THIS));
119 }
120 /* }}} */
121 
122 /* {{{ proto bool SimpleXMLIterator::hasChildren()
123  Check whether element has children (elements) */
PHP_METHOD(ce_SimpleXMLIterator,hasChildren)124 PHP_METHOD(ce_SimpleXMLIterator, hasChildren)
125 {
126 	php_sxe_object *sxe = Z_SXEOBJ_P(ZEND_THIS);
127 	php_sxe_object *child;
128 	xmlNodePtr      node;
129 
130 	if (zend_parse_parameters_none() == FAILURE) {
131 		return;
132 	}
133 
134 	if (Z_ISUNDEF(sxe->iter.data) || sxe->iter.type == SXE_ITER_ATTRLIST) {
135 		RETURN_FALSE;
136 	}
137 	child = Z_SXEOBJ_P(&sxe->iter.data);
138 
139 	GET_NODE(child, node);
140 	if (node) {
141 		node = node->children;
142 	}
143 	while (node && node->type != XML_ELEMENT_NODE) {
144 		node = node->next;
145 	}
146 	RETURN_BOOL(node ? 1 : 0);
147 }
148 /* }}} */
149 
150 /* {{{ proto SimpleXMLIterator SimpleXMLIterator::getChildren()
151  Get child element iterator */
PHP_METHOD(ce_SimpleXMLIterator,getChildren)152 PHP_METHOD(ce_SimpleXMLIterator, getChildren)
153 {
154 	php_sxe_object *sxe = Z_SXEOBJ_P(ZEND_THIS);
155 	zval *data;
156 
157 	if (zend_parse_parameters_none() == FAILURE) {
158 		return;
159 	}
160 
161 	if (Z_ISUNDEF(sxe->iter.data) || sxe->iter.type == SXE_ITER_ATTRLIST) {
162 		return; /* return NULL */
163 	}
164 
165 	data = &sxe->iter.data;
166 	ZVAL_COPY_DEREF(return_value, data);
167 }
168 
169 /* {{{ arginfo */
170 ZEND_BEGIN_ARG_INFO(arginfo_simplexmliterator__void, 0)
171 ZEND_END_ARG_INFO()
172 /* }}} */
173 
174 static const zend_function_entry funcs_SimpleXMLIterator[] = {
175 	PHP_ME(ce_SimpleXMLIterator, rewind,                 arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
176 	PHP_ME(ce_SimpleXMLIterator, valid,                  arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
177 	PHP_ME(ce_SimpleXMLIterator, current,                arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
178 	PHP_ME(ce_SimpleXMLIterator, key,                    arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
179 	PHP_ME(ce_SimpleXMLIterator, next,                   arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
180 	PHP_ME(ce_SimpleXMLIterator, hasChildren,            arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
181 	PHP_ME(ce_SimpleXMLIterator, getChildren,            arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
182 	PHP_FE_END
183 };
184 /* }}} */
185 
PHP_MINIT_FUNCTION(sxe)186 PHP_MINIT_FUNCTION(sxe) /* {{{ */
187 {
188 	zend_class_entry *pce;
189 	zend_class_entry sxi;
190 
191 	if ((pce = zend_hash_str_find_ptr(CG(class_table), "simplexmlelement", sizeof("SimpleXMLElement") - 1)) == NULL) {
192 		ce_SimpleXMLElement  = NULL;
193 		ce_SimpleXMLIterator = NULL;
194 		return SUCCESS; /* SimpleXML must be initialized before */
195 	}
196 
197 	ce_SimpleXMLElement = pce;
198 
199 	INIT_CLASS_ENTRY_EX(sxi, "SimpleXMLIterator", sizeof("SimpleXMLIterator") - 1, funcs_SimpleXMLIterator);
200 	ce_SimpleXMLIterator = zend_register_internal_class_ex(&sxi, ce_SimpleXMLElement);
201 	ce_SimpleXMLIterator->create_object = ce_SimpleXMLElement->create_object;
202 
203 	zend_class_implements(ce_SimpleXMLIterator, 1, spl_ce_RecursiveIterator);
204 	zend_class_implements(ce_SimpleXMLIterator, 1, zend_ce_countable);
205 
206 	return SUCCESS;
207 }
208 /* }}} */
209