Back to index

php5  5.3.10
sxe.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Marcus Boerger <helly@php.net>                              |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: sxe.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 # include "config.h"
00023 #endif
00024 
00025 #include "php.h"
00026 #include "php_ini.h"
00027 #include "ext/standard/info.h"
00028 #include "zend_interfaces.h"
00029 
00030 #include "ext/spl/php_spl.h"
00031 #include "ext/spl/spl_iterators.h"
00032 #include "sxe.h"
00033 
00034 zend_class_entry *ce_SimpleXMLIterator = NULL;
00035 zend_class_entry *ce_SimpleXMLElement;
00036 
00037 #include "php_simplexml_exports.h"
00038 
00039 /* {{{ proto void SimpleXMLIterator::rewind()
00040  Rewind to first element */
00041 PHP_METHOD(ce_SimpleXMLIterator, rewind)
00042 {
00043        php_sxe_iterator iter;
00044        
00045        if (zend_parse_parameters_none() == FAILURE) {
00046               return;
00047        }
00048 
00049        iter.sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
00050        ce_SimpleXMLElement->iterator_funcs.funcs->rewind((zend_object_iterator*)&iter TSRMLS_CC);
00051 }
00052 /* }}} */
00053 
00054 /* {{{ proto bool SimpleXMLIterator::valid()
00055  Check whether iteration is valid */
00056 PHP_METHOD(ce_SimpleXMLIterator, valid)
00057 {
00058        php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
00059        
00060        if (zend_parse_parameters_none() == FAILURE) {
00061               return;
00062        }
00063 
00064        RETURN_BOOL(sxe->iter.data);
00065 }
00066 /* }}} */
00067 
00068 /* {{{ proto SimpleXMLIterator SimpleXMLIterator::current()
00069  Get current element */
00070 PHP_METHOD(ce_SimpleXMLIterator, current)
00071 {
00072        php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
00073        
00074        if (zend_parse_parameters_none() == FAILURE) {
00075               return;
00076        }
00077 
00078        if (!sxe->iter.data) {
00079               return; /* return NULL */
00080        }
00081 
00082        RETURN_ZVAL(sxe->iter.data, 1, 0);
00083 }
00084 /* }}} */
00085 
00086 /* {{{ proto string SimpleXMLIterator::key()
00087  Get name of current child element */
00088 PHP_METHOD(ce_SimpleXMLIterator, key)
00089 {
00090        xmlNodePtr curnode;
00091        php_sxe_object *intern;
00092        php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
00093        
00094        if (zend_parse_parameters_none() == FAILURE) {
00095               return;
00096        }
00097 
00098        if (!sxe->iter.data) {
00099               RETURN_FALSE;
00100        }
00101 
00102        intern = (php_sxe_object *)zend_object_store_get_object(sxe->iter.data TSRMLS_CC);
00103        if (intern != NULL && intern->node != NULL) {
00104               curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->node)->node;
00105               RETURN_STRINGL((char*)curnode->name, xmlStrlen(curnode->name), 1);
00106        }
00107     
00108        RETURN_FALSE;
00109 }
00110 /* }}} */
00111 
00112 /* {{{ proto void SimpleXMLIterator::next()
00113  Move to next element */
00114 PHP_METHOD(ce_SimpleXMLIterator, next)
00115 {
00116        php_sxe_iterator iter;
00117        
00118        if (zend_parse_parameters_none() == FAILURE) {
00119               return;
00120        }
00121 
00122        iter.sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
00123        ce_SimpleXMLElement->iterator_funcs.funcs->move_forward((zend_object_iterator*)&iter TSRMLS_CC);
00124 }
00125 /* }}} */
00126 
00127 /* {{{ proto bool SimpleXMLIterator::hasChildren()
00128  Check whether element has children (elements) */ 
00129 PHP_METHOD(ce_SimpleXMLIterator, hasChildren)
00130 {
00131        php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
00132        php_sxe_object *child;
00133        xmlNodePtr      node;
00134        
00135        if (zend_parse_parameters_none() == FAILURE) {
00136               return;
00137        }
00138 
00139        if (!sxe->iter.data || sxe->iter.type == SXE_ITER_ATTRLIST) {
00140               RETURN_FALSE;
00141        }
00142        child = php_sxe_fetch_object(sxe->iter.data TSRMLS_CC);
00143 
00144        GET_NODE(child, node);
00145        if (node) {
00146               node = node->children;
00147        }
00148        while (node && node->type != XML_ELEMENT_NODE) {
00149               node = node->next;
00150        }
00151        RETURN_BOOL(node ? 1 : 0);
00152 }
00153 /* }}} */
00154 
00155 /* {{{ proto SimpleXMLIterator SimpleXMLIterator::getChildren()
00156  Get child element iterator */ 
00157 PHP_METHOD(ce_SimpleXMLIterator, getChildren)
00158 {
00159        php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
00160        
00161        if (zend_parse_parameters_none() == FAILURE) {
00162               return;
00163        }
00164 
00165        if (!sxe->iter.data || sxe->iter.type == SXE_ITER_ATTRLIST) {
00166               return; /* return NULL */
00167        }
00168        RETURN_ZVAL(sxe->iter.data, 1, 0);
00169 }
00170 
00171 /* {{{ arginfo */
00172 ZEND_BEGIN_ARG_INFO(arginfo_simplexmliterator__void, 0)
00173 ZEND_END_ARG_INFO()
00174 /* }}} */
00175 
00176 static const zend_function_entry funcs_SimpleXMLIterator[] = {
00177        PHP_ME(ce_SimpleXMLIterator, rewind,                 arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
00178        PHP_ME(ce_SimpleXMLIterator, valid,                  arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
00179        PHP_ME(ce_SimpleXMLIterator, current,                arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
00180        PHP_ME(ce_SimpleXMLIterator, key,                    arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
00181        PHP_ME(ce_SimpleXMLIterator, next,                   arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
00182        PHP_ME(ce_SimpleXMLIterator, hasChildren,            arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
00183        PHP_ME(ce_SimpleXMLIterator, getChildren,            arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
00184        {NULL, NULL, NULL}
00185 };
00186 /* }}} */
00187 
00188 PHP_MINIT_FUNCTION(sxe) /* {{{ */
00189 {
00190        zend_class_entry **pce;
00191        zend_class_entry sxi;
00192 
00193        if (zend_hash_find(CG(class_table), "simplexmlelement", sizeof("SimpleXMLElement"), (void **) &pce) == FAILURE) {
00194               ce_SimpleXMLElement  = NULL;
00195               ce_SimpleXMLIterator = NULL;
00196               return SUCCESS; /* SimpleXML must be initialized before */
00197        }
00198 
00199        ce_SimpleXMLElement = *pce;
00200 
00201        INIT_CLASS_ENTRY_EX(sxi, "SimpleXMLIterator", strlen("SimpleXMLIterator"), funcs_SimpleXMLIterator);
00202        ce_SimpleXMLIterator = zend_register_internal_class_ex(&sxi, ce_SimpleXMLElement, NULL TSRMLS_CC);
00203        ce_SimpleXMLIterator->create_object = ce_SimpleXMLElement->create_object;
00204 
00205        zend_class_implements(ce_SimpleXMLIterator TSRMLS_CC, 1, spl_ce_RecursiveIterator);
00206        zend_class_implements(ce_SimpleXMLIterator TSRMLS_CC, 1, spl_ce_Countable);
00207 
00208        return SUCCESS;
00209 }
00210 /* }}} */
00211 
00212 /*
00213  * Local variables:
00214  * tab-width: 4
00215  * c-basic-offset: 4
00216  * End:
00217  * vim600: fdm=marker
00218  * vim: noet sw=4 ts=4
00219  */