-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from KnpLabs/iterator
Iterator
- Loading branch information
Showing
7 changed files
with
199 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Iterator; | ||
|
||
/** | ||
* Filter iterator keeping only curent items | ||
*/ | ||
class CurrentItemFilterIterator extends \FilterIterator | ||
{ | ||
public function accept() | ||
{ | ||
return $this->current()->isCurrent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Iterator; | ||
|
||
/** | ||
* Recursive iterator iterating on an item | ||
*/ | ||
class ItemIterator extends \ArrayIterator implements \RecursiveIterator | ||
{ | ||
public function hasChildren() | ||
{ | ||
$current = $this->current(); | ||
|
||
return $current->getIterator() instanceof \RecursiveIterator && 0 < count($current); | ||
} | ||
|
||
public function getChildren() | ||
{ | ||
return $this->current()->getIterator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Tests\Iterator; | ||
|
||
use Knp\Menu\Iterator\CurrentItemFilterIterator; | ||
use Knp\Menu\Tests\TestCase; | ||
|
||
class IteratorTest extends TestCase | ||
{ | ||
public function testIterator() | ||
{ | ||
$count = 0; | ||
foreach ($this->pt1 as $key => $value) { | ||
$count++; | ||
$this->assertEquals('Child '.$count, $key); | ||
$this->assertEquals('Child '.$count, $value->getLabel()); | ||
} | ||
} | ||
|
||
public function testRecursiveIterator() | ||
{ | ||
// Adding an item which does not provide a RecursiveIterator to be sure it works properly. | ||
$child = $this->getMock('Knp\Menu\ItemInterface'); | ||
$child->expects($this->any()) | ||
->method('getName') | ||
->will($this->returnValue('Foo')); | ||
$child->expects($this->any()) | ||
->method('getIterator') | ||
->will($this->returnValue(new \EmptyIterator())); | ||
$this->menu->addChild($child); | ||
|
||
$names = array(); | ||
foreach (new \RecursiveIteratorIterator($this->menu, \RecursiveIteratorIterator::SELF_FIRST) as $value) { | ||
$names[] = $value->getName(); | ||
} | ||
|
||
$this->assertEquals(array('Parent 1', 'Child 1', 'Child 2', 'Child 3', 'Parent 2', 'Child 4', 'Grandchild 1', 'Foo'), $names); | ||
} | ||
|
||
public function testRecursiveIteratorLeavesOnly() | ||
{ | ||
$names = array(); | ||
foreach (new \RecursiveIteratorIterator($this->menu, \RecursiveIteratorIterator::LEAVES_ONLY) as $value) { | ||
$names[] = $value->getName(); | ||
} | ||
|
||
$this->assertEquals(array('Child 1', 'Child 2', 'Child 3', 'Grandchild 1'), $names); | ||
} | ||
|
||
public function testFilterIterator() | ||
{ | ||
$this->pt1->setCurrent(true); | ||
$this->ch2->setCurrent(true); | ||
$this->gc1->setCurrent(true); | ||
|
||
$names = array(); | ||
$iterator = new CurrentItemFilterIterator( | ||
new \RecursiveIteratorIterator($this->menu, \RecursiveIteratorIterator::SELF_FIRST) | ||
); | ||
foreach ($iterator as $value) { | ||
$names[] = $value->getName(); | ||
} | ||
|
||
$this->assertEquals(array('Parent 1', 'Child 2', 'Grandchild 1'), $names); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Tests; | ||
|
||
use Knp\Menu\Iterator\CurrentItemFilterIterator; | ||
use Knp\Menu\MenuItem; | ||
use Knp\Menu\MenuFactory; | ||
|
||
abstract class TestCase extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var \Knp\Menu\MenuItem | ||
*/ | ||
protected $menu; | ||
|
||
/** | ||
* @var \Knp\Menu\MenuItem | ||
*/ | ||
protected $pt1; | ||
|
||
/** | ||
* @var \Knp\Menu\MenuItem | ||
*/ | ||
protected $ch1; | ||
|
||
/** | ||
* @var \Knp\Menu\MenuItem | ||
*/ | ||
protected $ch2; | ||
|
||
/** | ||
* @var \Knp\Menu\MenuItem | ||
*/ | ||
protected $ch3; | ||
|
||
/** | ||
* @var \Knp\Menu\MenuItem | ||
*/ | ||
protected $pt2; | ||
|
||
/** | ||
* @var \Knp\Menu\MenuItem | ||
*/ | ||
protected $ch4; | ||
|
||
/** | ||
* @var \Knp\Menu\MenuItem | ||
*/ | ||
protected $gc1; | ||
|
||
protected function setUp() | ||
{ | ||
$factory = new MenuFactory(); | ||
$this->menu = $factory->createItem('Root li', array('attributes' => array('class' => 'root'))); | ||
$this->pt1 = $this->menu->addChild('Parent 1'); | ||
$this->ch1 = $this->pt1->addChild('Child 1'); | ||
$this->ch2 = $this->pt1->addChild('Child 2'); | ||
|
||
// add the 3rd child via addChild with an object | ||
$this->ch3 = new MenuItem('Child 3', $factory); | ||
$this->pt1->addChild($this->ch3); | ||
|
||
$this->pt2 = $this->menu->addChild('Parent 2'); | ||
$this->ch4 = $this->pt2->addChild('Child 4'); | ||
$this->gc1 = $this->ch4->addChild('Grandchild 1'); | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
$this->menu = null; | ||
$this->pt1 = null; | ||
$this->ch1 = null; | ||
$this->ch2 = null; | ||
$this->ch3 = null; | ||
$this->pt2 = null; | ||
$this->ch4 = null; | ||
$this->gc1 = null; | ||
} | ||
|
||
// prints a visual representation of our basic testing tree | ||
protected function printTestTree() | ||
{ | ||
print(' Menu Structure '."\n"); | ||
print(' rt '."\n"); | ||
print(' / \ '."\n"); | ||
print(' pt1 pt2 '."\n"); | ||
print(' / | \ | '."\n"); | ||
print(' ch1 ch2 ch3 ch4 '."\n"); | ||
print(' | '."\n"); | ||
print(' gc1 '."\n"); | ||
} | ||
} |