]> granicus.if.org Git - php/commitdiff
- Make SimpleXMLIterator implement Countable
authorMarcus Boerger <helly@php.net>
Mon, 31 Oct 2005 20:29:34 +0000 (20:29 +0000)
committerMarcus Boerger <helly@php.net>
Mon, 31 Oct 2005 20:29:34 +0000 (20:29 +0000)
ext/spl/spl.php
ext/spl/spl_sxe.c
ext/spl/tests/sxe_005.phpt [new file with mode: 0755]

index 484117422454914daebb3c811cddfae02ab7c5ad..86374d00f5013e1053eb413fa0a3ea7b14f1701d 100755 (executable)
@@ -932,7 +932,7 @@ class RecursiveDirectoryIterator extends DirectoryIterator implements RecursiveI
  * has subelements, hasChildren() returns true.  This will trigger a call to
  * getChildren() which returns the iterator for that sub element.
  */
-class SimpleXMLIterator extends SimpleXMLElement implements RecursiveIterator
+class SimpleXMLIterator extends SimpleXMLElement implements RecursiveIterator, Countable
 {
        /** @return whether the current node has sub nodes.
         */
@@ -941,6 +941,10 @@ class SimpleXMLIterator extends SimpleXMLElement implements RecursiveIterator
        /** @return a SimpleXMLIterator for the current node.
         */
        function getChildren(); 
+
+       /** @return number of elements/attributes seen with foreach()
+        */
+       function count();
 }
 
 /** @ingroup SPL
index ee136361b002d126e82dcb3af1a8a17b2a2f7103..dc446610e8dcc98f68daea0304e7245b8ee45e42 100755 (executable)
@@ -32,6 +32,7 @@
 #include "spl_engine.h"
 #include "spl_iterators.h"
 #include "spl_sxe.h"
+#include "spl_array.h"
 
 zend_class_entry *spl_ce_SimpleXMLIterator = NULL;
 zend_class_entry *spl_ce_SimpleXMLElement;
@@ -135,6 +136,15 @@ SPL_METHOD(SimpleXMLIterator, getChildren)
        return_value->value.obj = zend_objects_store_clone_obj(sxe->iter.data TSRMLS_CC);
 }
 
+SPL_METHOD(SimpleXMLIterator, count) /* {{{ */
+{
+       long count = 0;
+
+       Z_OBJ_HANDLER_P(getThis(), count_elements)(getThis(), &count TSRMLS_CC);
+       
+       RETURN_LONG(count);
+}
+
 static zend_function_entry spl_funcs_SimpleXMLIterator[] = {
        SPL_ME(SimpleXMLIterator, rewind,                 NULL, ZEND_ACC_PUBLIC)
        SPL_ME(SimpleXMLIterator, valid,                  NULL, ZEND_ACC_PUBLIC)
@@ -143,6 +153,7 @@ static zend_function_entry spl_funcs_SimpleXMLIterator[] = {
        SPL_ME(SimpleXMLIterator, next,                   NULL, ZEND_ACC_PUBLIC)
        SPL_ME(SimpleXMLIterator, hasChildren,            NULL, ZEND_ACC_PUBLIC)
        SPL_ME(SimpleXMLIterator, getChildren,            NULL, ZEND_ACC_PUBLIC)
+       SPL_ME(SimpleXMLIterator, count,                  NULL, ZEND_ACC_PUBLIC)
        {NULL, NULL, NULL}
 };
 /* }}} */
@@ -161,6 +172,7 @@ SPL_API PHP_MINIT_FUNCTION(spl_sxe) /* {{{ */
 
        REGISTER_SPL_SUB_CLASS_EX(SimpleXMLIterator, SimpleXMLElement, spl_ce_SimpleXMLElement->create_object, spl_funcs_SimpleXMLIterator);
        REGISTER_SPL_IMPLEMENTS(SimpleXMLIterator, RecursiveIterator);
+       REGISTER_SPL_IMPLEMENTS(SimpleXMLIterator, Countable);
 
        return SUCCESS;
 }
diff --git a/ext/spl/tests/sxe_005.phpt b/ext/spl/tests/sxe_005.phpt
new file mode 100755 (executable)
index 0000000..2efd0a6
--- /dev/null
@@ -0,0 +1,46 @@
+--TEST--
+SPL: SimpleXMLIterator and getChildren()
+--SKIPIF--
+<?php 
+if (!extension_loaded("spl")) print "skip";
+if (!extension_loaded('simplexml')) print 'skip';
+if (!extension_loaded("libxml")) print "skip LibXML not present";
+if (!class_exists('RecursiveIteratorIterator')) print 'skip RecursiveIteratorIterator not available';
+?>
+--FILE--
+<?php 
+
+$xml =<<<EOF
+<?xml version='1.0'?>
+<sxe>
+ <elem1/>
+ <elem2/>
+ <elem2/>
+</sxe>
+EOF;
+
+class SXETest extends SimpleXMLIterator
+{
+       function count()
+       {
+               echo __METHOD__ . "\n";
+               return parent::count();
+       }
+}
+
+$sxe = new SXETest($xml);
+
+var_dump(count($sxe));
+var_dump(count($sxe->elem1));
+var_dump(count($sxe->elem2));
+
+?>
+===DONE===
+--EXPECT--
+SXETest::count
+int(3)
+SXETest::count
+int(1)
+SXETest::count
+int(2)
+===DONE===