1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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 : /* $Id: sxe.c 276986 2009-03-10 23:40:06Z helly $ */
20 :
21 : #ifdef HAVE_CONFIG_H
22 : # include "config.h"
23 : #endif
24 :
25 : #include "php.h"
26 : #include "php_ini.h"
27 : #include "ext/standard/info.h"
28 : #include "zend_interfaces.h"
29 :
30 : #include "ext/spl/php_spl.h"
31 : #include "ext/spl/spl_iterators.h"
32 : #include "sxe.h"
33 :
34 : zend_class_entry *ce_SimpleXMLIterator = NULL;
35 : zend_class_entry *ce_SimpleXMLElement;
36 :
37 : #include "php_simplexml_exports.h"
38 :
39 : /* {{{ proto void SimpleXMLIterator::rewind() U
40 : Rewind to first element */
41 : PHP_METHOD(ce_SimpleXMLIterator, rewind)
42 6 : {
43 : php_sxe_iterator iter;
44 :
45 6 : iter.sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
46 6 : ce_SimpleXMLElement->iterator_funcs.funcs->rewind((zend_object_iterator*)&iter TSRMLS_CC);
47 6 : }
48 : /* }}} */
49 :
50 : /* {{{ proto bool SimpleXMLIterator::valid() U
51 : Check whether iteration is valid */
52 : PHP_METHOD(ce_SimpleXMLIterator, valid)
53 21 : {
54 21 : php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
55 :
56 21 : RETURN_BOOL(sxe->iter.data);
57 : }
58 : /* }}} */
59 :
60 : /* {{{ proto SimpleXMLIterator SimpleXMLIterator::current() U
61 : Get current element */
62 : PHP_METHOD(ce_SimpleXMLIterator, current)
63 8 : {
64 8 : php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
65 :
66 8 : if (!sxe->iter.data) {
67 0 : return; /* return NULL */
68 : }
69 :
70 8 : RETURN_ZVAL(sxe->iter.data, 1, 0);
71 : }
72 : /* }}} */
73 :
74 : /* {{{ proto string SimpleXMLIterator::key() U
75 : Get name of current child element */
76 : PHP_METHOD(ce_SimpleXMLIterator, key)
77 2 : {
78 : xmlNodePtr curnode;
79 : php_sxe_object *intern;
80 2 : php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
81 :
82 2 : if (!sxe->iter.data) {
83 0 : RETURN_FALSE;
84 : }
85 :
86 2 : intern = (php_sxe_object *)zend_object_store_get_object(sxe->iter.data TSRMLS_CC);
87 2 : if (intern != NULL && intern->node != NULL) {
88 2 : curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->node)->node;
89 2 : RETURN_U_STRINGL(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), (char*)curnode->name, xmlStrlen(curnode->name), 1);
90 : }
91 :
92 0 : RETURN_FALSE;
93 : }
94 : /* }}} */
95 :
96 : /* {{{ proto void SimpleXMLIterator::next() U
97 : Move to next element */
98 : PHP_METHOD(ce_SimpleXMLIterator, next)
99 8 : {
100 : php_sxe_iterator iter;
101 :
102 8 : iter.sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
103 8 : ce_SimpleXMLElement->iterator_funcs.funcs->move_forward((zend_object_iterator*)&iter TSRMLS_CC);
104 8 : }
105 : /* }}} */
106 :
107 : /* {{{ proto bool SimpleXMLIterator::hasChildren() U
108 : Check whether element has children (elements) */
109 : PHP_METHOD(ce_SimpleXMLIterator, hasChildren)
110 24 : {
111 24 : php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
112 : php_sxe_object *child;
113 : xmlNodePtr node;
114 :
115 24 : if (!sxe->iter.data || sxe->iter.type == SXE_ITER_ATTRLIST) {
116 0 : RETURN_FALSE;
117 : }
118 24 : child = php_sxe_fetch_object(sxe->iter.data TSRMLS_CC);
119 :
120 24 : GET_NODE(child, node);
121 24 : if (node) {
122 24 : node = node->children;
123 : }
124 71 : while (node && node->type != XML_ELEMENT_NODE) {
125 23 : node = node->next;
126 : }
127 24 : RETURN_BOOL(node ? 1 : 0);
128 : }
129 : /* }}} */
130 :
131 : /* {{{ proto SimpleXMLIterator SimpleXMLIterator::getChildren() U
132 : Get child element iterator */
133 : PHP_METHOD(ce_SimpleXMLIterator, getChildren)
134 14 : {
135 14 : php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
136 :
137 14 : if (!sxe->iter.data || sxe->iter.type == SXE_ITER_ATTRLIST) {
138 1 : return; /* return NULL */
139 : }
140 13 : RETURN_ZVAL(sxe->iter.data, 1, 0);
141 : }
142 :
143 : static const zend_function_entry funcs_SimpleXMLIterator[] = {
144 : PHP_ME(ce_SimpleXMLIterator, rewind, NULL, ZEND_ACC_PUBLIC)
145 : PHP_ME(ce_SimpleXMLIterator, valid, NULL, ZEND_ACC_PUBLIC)
146 : PHP_ME(ce_SimpleXMLIterator, current, NULL, ZEND_ACC_PUBLIC)
147 : PHP_ME(ce_SimpleXMLIterator, key, NULL, ZEND_ACC_PUBLIC)
148 : PHP_ME(ce_SimpleXMLIterator, next, NULL, ZEND_ACC_PUBLIC)
149 : PHP_ME(ce_SimpleXMLIterator, hasChildren, NULL, ZEND_ACC_PUBLIC)
150 : PHP_ME(ce_SimpleXMLIterator, getChildren, NULL, ZEND_ACC_PUBLIC)
151 : {NULL, NULL, NULL}
152 : };
153 : /* }}} */
154 :
155 : PHP_MINIT_FUNCTION(sxe) /* {{{ */
156 17007 : {
157 : zend_class_entry **pce;
158 : zend_class_entry sxi;
159 :
160 17007 : if (zend_hash_find(CG(class_table), "simplexmlelement", sizeof("SimpleXMLElement"), (void **) &pce) == FAILURE) {
161 0 : ce_SimpleXMLElement = NULL;
162 0 : ce_SimpleXMLIterator = NULL;
163 0 : return SUCCESS; /* SimpleXML must be initialized before */
164 : }
165 :
166 17007 : ce_SimpleXMLElement = *pce;
167 :
168 17007 : INIT_CLASS_ENTRY_EX(sxi, "SimpleXMLIterator", strlen("SimpleXMLIterator"), funcs_SimpleXMLIterator);
169 17007 : ce_SimpleXMLIterator = zend_register_internal_class_ex(&sxi, ce_SimpleXMLElement, NULL TSRMLS_CC);
170 17007 : ce_SimpleXMLIterator->create_object = ce_SimpleXMLElement->create_object;
171 :
172 17007 : zend_class_implements(ce_SimpleXMLIterator TSRMLS_CC, 1, spl_ce_RecursiveIterator);
173 17007 : zend_class_implements(ce_SimpleXMLIterator TSRMLS_CC, 1, spl_ce_Countable);
174 :
175 17007 : return SUCCESS;
176 : }
177 : /* }}} */
178 :
179 : /*
180 : * Local variables:
181 : * tab-width: 4
182 : * c-basic-offset: 4
183 : * End:
184 : * vim600: fdm=marker
185 : * vim: noet sw=4 ts=4
186 : */
|