Bug #66411 [Opn->Nab]: RecursiveIterator and Countable don't work together
| From: | requinix@php.net | Date: | Sun, 05 Jan 2014 00:20:09 +0000 |
| Subject: | Bug #66411 [Opn->Nab]: RecursiveIterator and Countable don't work together | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-183593@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66411&edit=1
ID: 66411
Updated by: requinix@php.net
Reported by: webmaster at johnatan dot ru
Summary: RecursiveIterator and Countable don't work together
-Status: Open
+Status: Not a bug
Type: Bug
Package: SPL related
Operating System: Debian 3.2.51-1
PHP Version: 5.4.23
Block user comment: N
Private report: N
New Comment:
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
return count($this->children[$this->position]->getChildren()) > 0;
is wrong. It should be
return count($this->children[$this->position]) > 0;
Previous Comments:
------------------------------------------------------------------------
[2014-01-04 18:15:34] webmaster at johnatan dot ru
Description:
------------
---
From manual page: http://www.php.net/countable.count
---
If I implement Countable interface in class RecursiveIteratorIterator show only nodes with
count(node) > 0. If I remove Countable interface show all nodes.
Test script:
---------------
<?php
class TreeModel implements RecursiveIterator, Countable
{
public $id;
public $parentId;
public $name;
private $children = array();
private $position = 0;
private $counter = 0;
public function addChildren(TreeModel $child)
{
$this->children[] = $child;
$this->counter++;
}
public final function getChildren()
{
return $this->valid() ? $this->children[$this->position] : null;
}
public function hasChildren()
{
if (!$this->valid()) {
return false;
}
return count($this->children[$this->position]->getChildren()) > 0;
}
public function current()
{
return $this->children[$this->position];
}
public function next()
{
$this->position++;
}
public function key()
{
return $this->position;
}
public function valid()
{
return isset($this->children[$this->position]);
}
public function rewind()
{
$this->position = 0;
}
public function count()
{
return iterator_count(new RecursiveIteratorIterator($this,
RecursiveIteratorIterator::SELF_FIRST));
}
}
$data = array(
array('parentId' => 0, 'id' => 1, 'name' => 'Node
1'),
array('parentId' => 0, 'id' => 2, 'name' => 'Node
2'),
array('parentId' => 0, 'id' => 3, 'name' => 'Node
3'),
array('parentId' => 1, 'id' => 4, 'name' => 'Node
1.1'),
array('parentId' => 1, 'id' => 5, 'name' => 'Node
1.2'),
array('parentId' => 1, 'id' => 6, 'name' => 'Node
1.3'),
array('parentId' => 2, 'id' => 7, 'name' => 'Node
2.1'),
array('parentId' => 3, 'id' => 10, 'name' => 'Node
3.1'),
array('parentId' => 7, 'id' => 8, 'name' => 'Node
2.1.1'),
array('parentId' => 8, 'id' => 9, 'name' => 'Node
2.1.1.1')
);
$root = new TreeModel();
$root->id = 0;
$root->parentId = null;
$root->name = 'ROOT';
$tempList = array($root);
foreach($data as $treeNode) {
$treeItem = new TreeModel();
$treeItem->id = $treeNode['id'];
$treeItem->parentId = $treeNode['parentId'];
$treeItem->name = $treeNode['name'];
$tempList[$treeNode['id']] = $treeItem;
$tempList[$treeNode['parentId']]->addChildren($treeItem);
}
unset($tempList);
$ritit = new RecursiveIteratorIterator($root, RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD);
foreach($ritit as $index => $treeItem) {
echo $index, ': ', $treeItem->name, ": ", count($treeItem), "\n";
}
echo 'Total: ', count($root), "\n";
Expected result:
----------------
0: Node 1: 3
0: Node 1.1: 0
1: Node 1.2: 0
2: Node 1.3: 0
1: Node 2: 1
0: Node 2.1: 1
0: Node 2.1.1: 1
0: Node 2.1.1.1: 0
2: Node 3: 1
0: Node 3.1: 0
Total: 10
Actual result:
--------------
0: Node 1: 3
1: Node 2: 1
0: Node 2.1: 1
2: Node 3: 1
Total: 3
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66411&edit=1