helly           Mon Oct 31 15:29:34 2005 EDT

  Added files:                 
    /php-src/ext/spl/tests      sxe_005.phpt 

  Modified files:              
    /php-src/ext/spl    spl.php spl_sxe.c 
  Log:
  - Make SimpleXMLIterator implement Countable
  
  
http://cvs.php.net/diff.php/php-src/ext/spl/spl.php?r1=1.61&r2=1.62&ty=u
Index: php-src/ext/spl/spl.php
diff -u php-src/ext/spl/spl.php:1.61 php-src/ext/spl/spl.php:1.62
--- php-src/ext/spl/spl.php:1.61        Mon Oct 10 17:06:54 2005
+++ php-src/ext/spl/spl.php     Mon Oct 31 15:29:29 2005
@@ -932,7 +932,7 @@
  * 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 @@
        /** @return a SimpleXMLIterator for the current node.
         */
        function getChildren(); 
+
+       /** @return number of elements/attributes seen with foreach()
+        */
+       function count();
 }
 
 /** @ingroup SPL
http://cvs.php.net/diff.php/php-src/ext/spl/spl_sxe.c?r1=1.14&r2=1.15&ty=u
Index: php-src/ext/spl/spl_sxe.c
diff -u php-src/ext/spl/spl_sxe.c:1.14 php-src/ext/spl/spl_sxe.c:1.15
--- php-src/ext/spl/spl_sxe.c:1.14      Sat Oct 29 16:37:59 2005
+++ php-src/ext/spl/spl_sxe.c   Mon Oct 31 15:29:29 2005
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: spl_sxe.c,v 1.14 2005/10/29 20:37:59 helly Exp $ */
+/* $Id: spl_sxe.c,v 1.15 2005/10/31 20:29:29 helly Exp $ */
 
 #ifdef HAVE_CONFIG_H
 # include "config.h"
@@ -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 @@
        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 @@
        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 @@
 
        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;
 }

http://cvs.php.net/co.php/php-src/ext/spl/tests/sxe_005.phpt?r=1.1&p=1
Index: php-src/ext/spl/tests/sxe_005.phpt
+++ php-src/ext/spl/tests/sxe_005.phpt
--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===

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to