colder          Wed Jul  9 21:27:10 2008 UTC

  Added files:                 
    /php-src/ext/simplexml/tests        036.phpt 

  Modified files:              
    /php-src/ext/simplexml      php_simplexml.h simplexml.c 
  Log:
  Move SXI::count to SXE::count and make it user-friendly
  
http://cvs.php.net/viewvc.cgi/php-src/ext/simplexml/php_simplexml.h?r1=1.29&r2=1.30&diff_format=u
Index: php-src/ext/simplexml/php_simplexml.h
diff -u php-src/ext/simplexml/php_simplexml.h:1.29 
php-src/ext/simplexml/php_simplexml.h:1.30
--- php-src/ext/simplexml/php_simplexml.h:1.29  Thu Jan  3 16:18:29 2008
+++ php-src/ext/simplexml/php_simplexml.h       Wed Jul  9 21:27:10 2008
@@ -16,7 +16,7 @@
   +----------------------------------------------------------------------+
 */
 
-/* $Id: php_simplexml.h,v 1.29 2008/01/03 16:18:29 nlopess Exp $ */
+/* $Id: php_simplexml.h,v 1.30 2008/07/09 21:27:10 colder Exp $ */
 
 #ifndef PHP_SIMPLEXML_H
 #define PHP_SIMPLEXML_H
@@ -68,6 +68,7 @@
                zval                  *data;
        } iter;
        zval *tmp;
+       zend_function *fptr_count;
 } php_sxe_object;
 
 #ifdef ZTS
http://cvs.php.net/viewvc.cgi/php-src/ext/simplexml/simplexml.c?r1=1.256&r2=1.257&diff_format=u
Index: php-src/ext/simplexml/simplexml.c
diff -u php-src/ext/simplexml/simplexml.c:1.256 
php-src/ext/simplexml/simplexml.c:1.257
--- php-src/ext/simplexml/simplexml.c:1.256     Fri Jun 27 15:47:21 2008
+++ php-src/ext/simplexml/simplexml.c   Wed Jul  9 21:27:10 2008
@@ -18,7 +18,7 @@
   +----------------------------------------------------------------------+
 */
 
-/* $Id: simplexml.c,v 1.256 2008/06/27 15:47:21 felipe Exp $ */
+/* $Id: simplexml.c,v 1.257 2008/07/09 21:27:10 colder Exp $ */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -1868,14 +1868,12 @@
 }
 /* }}} */
 
-static int sxe_count_elements(zval *object, long *count TSRMLS_DC) /* {{{ */
+static int php_sxe_count_elements_helper(php_sxe_object *sxe, long *count 
TSRMLS_DC) /* {{{ */
 {
-       php_sxe_object  *sxe;
        xmlNodePtr       node;
        zval            *data;
 
        *count = 0;
-       sxe = php_sxe_fetch_object(object TSRMLS_CC);
 
        data = sxe->iter.data;
        sxe->iter.data = NULL;
@@ -1897,6 +1895,42 @@
 }
 /* }}} */
 
+static int sxe_count_elements(zval *object, long *count TSRMLS_DC) /* {{{ */
+{
+       php_sxe_object  *intern;
+       intern = php_sxe_fetch_object(object TSRMLS_CC);
+       if (intern->fptr_count) {
+               zval *rv;
+               zend_call_method_with_0_params(&object, intern->zo.ce, 
&intern->fptr_count, "count", &rv);
+               if (rv) {
+                       if (intern->tmp) {
+                               zval_ptr_dtor(&intern->tmp);
+                       }
+                       MAKE_STD_ZVAL(intern->tmp);
+                       ZVAL_ZVAL(intern->tmp, rv, 1, 1);
+                       convert_to_long(intern->tmp);
+                       *count = (long) Z_LVAL_P(intern->tmp);
+                       return SUCCESS;
+               }
+               return FAILURE;
+       }
+       return php_sxe_count_elements_helper(intern, count TSRMLS_CC);
+}
+/* }}} */
+
+/* {{{ proto int SimpleXMLIterator::count()
+ Get number of child elements */
+SXE_METHOD(count)
+{
+       long count = 0;
+       php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
+
+       php_sxe_count_elements_helper(sxe, &count TSRMLS_CC);
+       
+       RETURN_LONG(count);
+}
+/* }}} */
+
 static zval *sxe_get_value(zval *z TSRMLS_DC) /* {{{ */
 {
        zval *retval;
@@ -2035,15 +2069,34 @@
 static php_sxe_object* php_sxe_object_new(zend_class_entry *ce TSRMLS_DC)
 {
        php_sxe_object *intern;
+       zend_class_entry     *parent = ce;
+       int inherited = 0;
 
        intern = ecalloc(1, sizeof(php_sxe_object));
 
        intern->iter.type = SXE_ITER_NONE;
        intern->iter.nsprefix = NULL;
        intern->iter.name = NULL;
+       intern->fptr_count = NULL;
 
        zend_object_std_init(&intern->zo, ce TSRMLS_CC);
 
+       while (parent) {
+               if (parent == sxe_class_entry) {
+                       break;
+               }
+
+               parent = parent->parent;
+               inherited = 1;
+       }
+
+       if (inherited) {
+               zend_hash_find(&ce->function_table, "count", 
sizeof("count"),(void **) &intern->fptr_count);
+               if (intern->fptr_count->common.scope == parent) {
+                       intern->fptr_count = NULL;
+               }
+       }
+
        return intern;
 }
 /* }}} */
@@ -2538,6 +2591,7 @@
        SXE_ME(addChild,               NULL, ZEND_ACC_PUBLIC)
        SXE_ME(addAttribute,           NULL, ZEND_ACC_PUBLIC)
        SXE_ME(__toString,             NULL, ZEND_ACC_PUBLIC)
+       SXE_ME(count,                  NULL, ZEND_ACC_PUBLIC)
        {NULL, NULL, NULL}
 };
 /* }}} */
@@ -2586,7 +2640,7 @@
 {
        php_info_print_table_start();
        php_info_print_table_header(2, "Simplexml support", "enabled");
-       php_info_print_table_row(2, "Revision", "$Revision: 1.256 $");
+       php_info_print_table_row(2, "Revision", "$Revision: 1.257 $");
        php_info_print_table_row(2, "Schema support",
 #ifdef LIBXML_SCHEMAS_ENABLED
                "enabled");

http://cvs.php.net/viewvc.cgi/php-src/ext/simplexml/tests/036.phpt?view=markup&rev=1.1
Index: php-src/ext/simplexml/tests/036.phpt
+++ php-src/ext/simplexml/tests/036.phpt
--TEST--
SimpleXML: overriden count() method
--SKIPIF--
<?php if (!extension_loaded("simplexml")) print "skip"; ?>
--FILE--
<?php
class SXE extends SimpleXmlElement {
    public function count() {
        echo "Called Count!\n";
        return parent::count();
    }
}

$str = '<xml><c>asdf</c><c>ghjk</c></xml>';
$sxe = new SXE($str);
var_dump(count($sxe));
?>
==Done==
--EXPECT--
Called 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