Skip to content

Implement Countable for DomNodeList and DOMNamedNodeMap (Request #74837) #2618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ext/dom/dom_fe.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions ext/dom/namednodemap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
/* }}} */

/*
Expand All @@ -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
};
/* }}} */
Expand Down Expand Up @@ -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

/*
Expand Down
27 changes: 27 additions & 0 deletions ext/dom/nodelist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -170,6 +196,7 @@ PHP_FUNCTION(dom_nodelist_item)
}
/* }}} end dom_nodelist_item */


#endif

/*
Expand Down
4 changes: 2 additions & 2 deletions ext/dom/php_dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions ext/dom/tests/DOMNamedNodeMap_count.phpt
Original file line number Diff line number Diff line change
@@ -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)
28 changes: 28 additions & 0 deletions ext/dom/tests/DomNodeList_count.phpt
Original file line number Diff line number Diff line change
@@ -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)