com php-src: Implement Countable for DomNodeList and DOMNamedNodeMap (Request #74837): ext/dom/dom_fe.h ext/dom/namednodemap .c ext/dom/nodelist.c
ext/dom/php_dom.c ext/dom/t ests/DOMNamedNodeMap_count.phpt ext/dom/tests/DomNodeList _count.phpt
| From: | Remi Collet | Date: | Mon, 10 Jul 2017 06:48:10 +0000 |
| Subject: | com php-src: Implement Countable for DomNodeList and DOMNamedNodeMap (Request #74837): ext/dom/dom_fe.h ext/dom/namednodemap .c ext/dom/nodelist.c ext/dom/php_dom.c ext/dom/t ests/DOMNamedNodeMap_count.phpt ext/dom/tests/DomNodeList _count.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-98483@lists.php.net to get a copy of this message | ||
Commit: f725d9b1b9c71091ece0c73d6168a13bfb0e44d1
Author: Andreas Treichel <gmblar+github@gmail.com> Sun, 9 Jul 2017 02:15:38 +0200
Committer: Remi Collet <remi@php.net> Mon, 10 Jul 2017 08:48:10 +0200
Parents: 7cb5bdf64a95bd70623d33d6ea122c13b01113bd
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=f725d9b1b9c71091ece0c73d6168a13bfb0e44d1
Log:
Implement Countable for DomNodeList and DOMNamedNodeMap (Request #74837)
Bugs:
https://bugs.php.net/74837
Changed paths:
M ext/dom/dom_fe.h
M ext/dom/namednodemap.c
M ext/dom/nodelist.c
M ext/dom/php_dom.c
A ext/dom/tests/DOMNamedNodeMap_count.phpt
A ext/dom/tests/DomNodeList_count..phpt
Diff:
diff --git a/ext/dom/dom_fe.h b/ext/dom/dom_fe.h
index 231d840..6f1035a 100644
--- a/ext/dom/dom_fe.h
+++ b/ext/dom/dom_fe..h
@@ -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);
diff --git a/ext/dom/namednodemap.c b/ext/dom/namednodemap.c
index 4dea7be2..3ed426c 100644
--- a/ext/dom/namednodemap.c
+++ b/ext/dom/namednodemap.c
@@ -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
/*
diff --git a/ext/dom/nodelist.c b/ext/dom/nodelist.c
index d469467..c7fbc76 100644
--- a/ext/dom/nodelist.c
+++ b/ext/dom/nodelist.c
@@ -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
/*
diff --git a/ext/dom/php_dom.c b/ext/dom/php_dom.c
index 6b844ab..77f918a 100644
--- a/ext/dom/php_dom.c
+++ b/ext/dom/php_dom.c
@@ -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
index 0000000..656dd48
--- /dev/null
+++ b/ext/dom/tests/DOMNamedNodeMap_count.phpt
@@ -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
index 0000000..3ebf4ae
--- /dev/null
+++ b/ext/dom/tests/DomNodeList_count.phpt
@@ -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)