]> granicus.if.org Git - php/commitdiff
Implement Countable for DomNodeList and DOMNamedNodeMap (Request #74837)
authorAndreas Treichel <gmblar+github@gmail.com>
Sun, 9 Jul 2017 00:15:38 +0000 (02:15 +0200)
committerRemi Collet <remi@php.net>
Mon, 10 Jul 2017 06:48:10 +0000 (08:48 +0200)
ext/dom/dom_fe.h
ext/dom/namednodemap.c
ext/dom/nodelist.c
ext/dom/php_dom.c
ext/dom/tests/DOMNamedNodeMap_count.phpt [new file with mode: 0644]
ext/dom/tests/DomNodeList_count.phpt [new file with mode: 0644]

index 231d8402d89e1017002d30448bec24bbcbb39fb9..6f1035a57731664b7e1d682bd77a470ed11cfa12 100644 (file)
@@ -172,6 +172,7 @@ PHP_METHOD(domnode, getLineNo);
 
 /* domnodelist methods */
 PHP_FUNCTION(dom_nodelist_item);
+PHP_FUNCTION(dom_nodelist_count);
 
 /* domnamednodemap methods */
 PHP_FUNCTION(dom_namednodemap_get_named_item);
@@ -181,6 +182,7 @@ PHP_FUNCTION(dom_namednodemap_item);
 PHP_FUNCTION(dom_namednodemap_get_named_item_ns);
 PHP_FUNCTION(dom_namednodemap_set_named_item_ns);
 PHP_FUNCTION(dom_namednodemap_remove_named_item_ns);
+PHP_FUNCTION(dom_namednodemap_count);
 
 /* domcharacterdata methods */
 PHP_FUNCTION(dom_characterdata_substring_data);
index 4dea7be28a91bd324ad5a3c275fb5164fab4a90b..3ed426cb8e741f4b93555594e6ec6cfe67390a13 100644 (file)
@@ -57,6 +57,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_namednodemap_remove_named_item_ns, 0, 0, 0)
        ZEND_ARG_INFO(0, namespaceURI)
        ZEND_ARG_INFO(0, localName)
 ZEND_END_ARG_INFO();
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_namednodemap_count, 0, 0, 0)
+ZEND_END_ARG_INFO();
 /* }}} */
 
 /*
@@ -74,6 +77,7 @@ const zend_function_entry php_dom_namednodemap_class_functions[] = { /* {{{ */
        PHP_FALIAS(getNamedItemNS, dom_namednodemap_get_named_item_ns, arginfo_dom_namednodemap_get_named_item_ns)
        PHP_FALIAS(setNamedItemNS, dom_namednodemap_set_named_item_ns, arginfo_dom_namednodemap_set_named_item_ns)
        PHP_FALIAS(removeNamedItemNS, dom_namednodemap_remove_named_item_ns, arginfo_dom_namednodemap_remove_named_item_ns)
+       PHP_FALIAS(count, dom_namednodemap_count, arginfo_dom_namednodemap_count)
        PHP_FE_END
 };
 /* }}} */
@@ -332,6 +336,24 @@ PHP_FUNCTION(dom_namednodemap_remove_named_item_ns)
 }
 /* }}} end dom_namednodemap_remove_named_item_ns */
 
+/* {{{ proto int|bool dom_namednodemap_count();
+*/
+PHP_FUNCTION(dom_namednodemap_count)
+{
+       zval *id;
+       dom_object *intern;
+
+       if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &id, dom_namednodemap_class_entry) == FAILURE) {
+               return;
+       }
+
+       intern = Z_DOMOBJ_P(id);
+       if(dom_namednodemap_length_read(intern, return_value) == FAILURE) {
+               RETURN_FALSE;
+       }
+}
+/* }}} end dom_namednodemap_count */
+
 #endif
 
 /*
index d469467f48a95b1af3198c9140caa30fa8a41bde..c7fbc76fd1c863353e90f60678cede79c97385bf 100644 (file)
@@ -34,6 +34,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_nodelist_item, 0, 0, 1)
 ZEND_END_ARG_INFO();
 /* }}} */
 
+/* {{{ arginfo */
+ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_nodelist_count, 0, 0, 0)
+ZEND_END_ARG_INFO();
+/* }}} */
+
 /*
 * class DOMNodeList
 *
@@ -43,9 +48,11 @@ ZEND_END_ARG_INFO();
 
 const zend_function_entry php_dom_nodelist_class_functions[] = {
        PHP_FALIAS(item, dom_nodelist_item, arginfo_dom_nodelist_item)
+       PHP_FALIAS(count, dom_nodelist_count, arginfo_dom_nodelist_count)
        PHP_FE_END
 };
 
+
 /* {{{ length  int
 readonly=yes
 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-203510337
@@ -96,6 +103,25 @@ int dom_nodelist_length_read(dom_object *obj, zval *retval)
        return SUCCESS;
 }
 
+
+/* {{{ proto int|bool dom_nodelist_count();
+*/
+PHP_FUNCTION(dom_nodelist_count)
+{
+       zval *id;
+       dom_object *intern;
+
+       if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &id, dom_nodelist_class_entry) == FAILURE) {
+               return;
+       }
+
+       intern = Z_DOMOBJ_P(id);
+       if(dom_nodelist_length_read(intern, return_value) == FAILURE) {
+               RETURN_FALSE;
+       }
+}
+/* }}} end dom_nodelist_count */
+
 /* }}} */
 
 /* {{{ proto DOMNode dom_nodelist_item(int index);
@@ -170,6 +196,7 @@ PHP_FUNCTION(dom_nodelist_item)
 }
 /* }}} end dom_nodelist_item */
 
+
 #endif
 
 /*
index 6b844ab33304de8155caec3a6e182bcd5738af9a..77f918ae9bf9bd6efe844185dbbfc3dd210b7ae9 100644 (file)
@@ -706,7 +706,7 @@ PHP_MINIT_FUNCTION(dom)
        ce.create_object = dom_nnodemap_objects_new;
        dom_nodelist_class_entry = zend_register_internal_class_ex(&ce, NULL);
        dom_nodelist_class_entry->get_iterator = php_dom_get_iterator;
-       zend_class_implements(dom_nodelist_class_entry, 1, zend_ce_traversable);
+       zend_class_implements(dom_nodelist_class_entry, 2, zend_ce_traversable, zend_ce_countable);
 
        zend_hash_init(&dom_nodelist_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
        dom_register_prop_handler(&dom_nodelist_prop_handlers, "length", sizeof("length")-1, dom_nodelist_length_read, NULL);
@@ -716,7 +716,7 @@ PHP_MINIT_FUNCTION(dom)
        ce.create_object = dom_nnodemap_objects_new;
        dom_namednodemap_class_entry = zend_register_internal_class_ex(&ce, NULL);
        dom_namednodemap_class_entry->get_iterator = php_dom_get_iterator;
-       zend_class_implements(dom_namednodemap_class_entry, 1, zend_ce_traversable);
+       zend_class_implements(dom_namednodemap_class_entry, 2, zend_ce_traversable, zend_ce_countable);
 
        zend_hash_init(&dom_namednodemap_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
        dom_register_prop_handler(&dom_namednodemap_prop_handlers, "length", sizeof("length")-1, dom_namednodemap_length_read, NULL);
diff --git a/ext/dom/tests/DOMNamedNodeMap_count.phpt b/ext/dom/tests/DOMNamedNodeMap_count.phpt
new file mode 100644 (file)
index 0000000..656dd48
--- /dev/null
@@ -0,0 +1,28 @@
+--TEST--
+Test count nodes in DOMNamedNodeMap
+--CREDITS--
+Andreas Treichel <gmblar+github@gmail.com>
+--SKIPIF--
+<?php require_once('skipif.inc'); ?>
+--FILE--
+<?php
+
+$document = new DomDocument();
+$root = $document->createElement('root');
+$document->appendChild($root);
+for($i = 0; $i < 5; $i++) {
+    $root->setAttribute('attribute-' . $i, 'value-' . $i);
+}
+for($i = 0; $i < 7; $i++) {
+    $item = $document->createElement('item');
+    $root->appendChild($item);
+}
+var_dump($root->attributes->length);
+var_dump($root->attributes->count());
+var_dump(count($root->attributes));
+
+?>
+--EXPECTF--
+int(5)
+int(5)
+int(5)
diff --git a/ext/dom/tests/DomNodeList_count.phpt b/ext/dom/tests/DomNodeList_count.phpt
new file mode 100644 (file)
index 0000000..3ebf4ae
--- /dev/null
@@ -0,0 +1,28 @@
+--TEST--
+Test count nodes in DOMNodeList
+--CREDITS--
+Andreas Treichel <gmblar+github@gmail.com>
+--SKIPIF--
+<?php require_once('skipif.inc'); ?>
+--FILE--
+<?php
+
+$document = new DomDocument();
+$root = $document->createElement('root');
+$document->appendChild($root);
+for($i = 0; $i < 5; $i++) {
+    $root->setAttribute('attribute-' . $i, 'value-' . $i);
+}
+for($i = 0; $i < 7; $i++) {
+    $item = $document->createElement('item');
+    $root->appendChild($item);
+}
+var_dump($root->childNodes->length);
+var_dump($root->childNodes->count());
+var_dump(count($root->childNodes));
+
+?>
+--EXPECTF--
+int(7)
+int(7)
+int(7)