summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xext/spl/spl.php6
-rwxr-xr-xext/spl/spl_sxe.c12
-rwxr-xr-xext/spl/tests/sxe_005.phpt46
3 files changed, 63 insertions, 1 deletions
diff --git a/ext/spl/spl.php b/ext/spl/spl.php
index 4841174224..86374d00f5 100755
--- a/ext/spl/spl.php
+++ b/ext/spl/spl.php
@@ -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
diff --git a/ext/spl/spl_sxe.c b/ext/spl/spl_sxe.c
index ee136361b0..dc446610e8 100755
--- a/ext/spl/spl_sxe.c
+++ b/ext/spl/spl_sxe.c
@@ -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
index 0000000000..2efd0a6bee
--- /dev/null
+++ b/ext/spl/tests/sxe_005.phpt
@@ -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===