Bug #66411 [NEW]: RecursiveIterator and Countable don't work together
| From: | webmaster at johnatan dot ru | Date: | Sat, 04 Jan 2014 18:15:35 +0000 |
| Subject: | Bug #66411 [NEW]: RecursiveIterator and Countable don't work together | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-183589@lists.php.net to get a copy of this message | ||
From: webmaster at johnatan dot ru
Operating system: Debian 3.2.51-1
PHP version: 5.4.23
Package: SPL related
Bug Type: Bug
Bug description:RecursiveIterator and Countable don't work together
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 bug report at https://bugs.php.net/bug.php?id=66411&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=66411&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=66411&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=66411&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=66411&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=66411&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=66411&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=66411&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=66411&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=66411&r=support
Expected behavior: https://bugs.php.net/fix.php?id=66411&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=66411&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=66411&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=66411&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66411&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=66411&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=66411&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=66411&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66411&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=66411&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=66411&r=mysqlcfg