forked from KnpLabs/KnpMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved the creation from array and from node outside the item factory
They are now handled by loaders
- Loading branch information
Showing
8 changed files
with
275 additions
and
92 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
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,61 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Loader; | ||
|
||
use Knp\Menu\FactoryInterface; | ||
use Knp\Menu\ItemInterface; | ||
|
||
/** | ||
* Loader importing a menu tree from an array. | ||
* | ||
* The array should match the output of MenuManipulator::toArray | ||
*/ | ||
class ArrayLoader implements LoaderInterface | ||
{ | ||
private $factory; | ||
|
||
public function __construct(FactoryInterface $factory) | ||
{ | ||
$this->factory = $factory; | ||
} | ||
|
||
public function load($data) | ||
{ | ||
if (!$this->supports($data)) { | ||
throw new \InvalidArgumentException(sprintf('Unsupported data. Expected an array but got ', is_object($data) ? get_class($data) : gettype($data))); | ||
} | ||
|
||
return $this->fromArray($data); | ||
} | ||
|
||
public function supports($data) | ||
{ | ||
return is_array($data); | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @param string|null $name (the name of the item, used only if there is no name in the data themselves) | ||
* | ||
* @return ItemInterface | ||
*/ | ||
private function fromArray(array $data, $name = null) | ||
{ | ||
$name = isset($data['name']) ? $data['name'] : $name; | ||
|
||
if (isset($data['children'])) { | ||
$children = $data['children']; | ||
unset($data['children']); | ||
} else { | ||
$children = array(); | ||
} | ||
|
||
$item = $this->factory->createItem($name, $data); | ||
|
||
foreach ($children as $name => $child) { | ||
$item->addChild($this->fromArray($child, $name)); | ||
} | ||
|
||
return $item; | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Loader; | ||
|
||
use Knp\Menu\ItemInterface; | ||
|
||
interface LoaderInterface | ||
{ | ||
/** | ||
* Loads the data into a menu item | ||
* | ||
* @param mixed $data | ||
* | ||
* @return ItemInterface | ||
*/ | ||
public function load($data); | ||
|
||
/** | ||
* Checks whether the loader can load these data | ||
* | ||
* @param mixed $data | ||
* | ||
* @return boolean | ||
*/ | ||
public function supports($data); | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Loader; | ||
|
||
use Knp\Menu\FactoryInterface; | ||
use Knp\Menu\NodeInterface; | ||
|
||
class NodeLoader implements LoaderInterface | ||
{ | ||
private $factory; | ||
|
||
public function __construct(FactoryInterface $factory) | ||
{ | ||
$this->factory = $factory; | ||
} | ||
|
||
public function load($data) | ||
{ | ||
if (!$data instanceof NodeInterface) { | ||
throw new \InvalidArgumentException(sprintf('Unsupported data. Expected Knp\Menu\NodeInterface but got ', is_object($data) ? get_class($data) : gettype($data))); | ||
} | ||
|
||
$item = $this->factory->createItem($data->getName(), $data->getOptions()); | ||
|
||
foreach ($data->getChildren() as $childNode) { | ||
$item->addChild($this->load($childNode)); | ||
} | ||
|
||
return $item; | ||
} | ||
|
||
public function supports($data) | ||
{ | ||
return $data instanceof NodeInterface; | ||
} | ||
} |
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,107 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Tests\Loader; | ||
|
||
use Knp\Menu\Loader\ArrayLoader; | ||
use Knp\Menu\MenuFactory; | ||
|
||
class ArrayLoaderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testLoadWithoutChildren() | ||
{ | ||
$array = array( | ||
'name' => 'joe', | ||
'uri' => '/foobar', | ||
'display' => false, | ||
); | ||
|
||
$loader = new ArrayLoader(new MenuFactory()); | ||
$item = $loader->load($array); | ||
|
||
$this->assertEquals('joe', $item->getName()); | ||
$this->assertEquals('/foobar', $item->getUri()); | ||
$this->assertFalse($item->isDisplayed()); | ||
$this->assertEmpty($item->getAttributes()); | ||
$this->assertEmpty($item->getChildren()); | ||
} | ||
|
||
public function testLoadWithChildren() | ||
{ | ||
$array = array( | ||
'name' => 'joe', | ||
'children' => array( | ||
'jack' => array( | ||
'name' => 'jack', | ||
'label' => 'Jack', | ||
), | ||
array( | ||
'name' => 'john' | ||
) | ||
), | ||
); | ||
|
||
$loader = new ArrayLoader(new MenuFactory()); | ||
$item = $loader->load($array); | ||
|
||
$this->assertEquals('joe', $item->getName()); | ||
$this->assertEmpty($item->getAttributes()); | ||
$this->assertCount(2, $item); | ||
$this->assertTrue(isset($item['john'])); | ||
} | ||
|
||
public function testLoadWithChildrenOmittingName() | ||
{ | ||
$array = array( | ||
'name' => 'joe', | ||
'children' => array( | ||
'jack' => array( | ||
'label' => 'Jack', | ||
), | ||
'john' => array( | ||
'label' => 'John' | ||
) | ||
), | ||
); | ||
|
||
$loader = new ArrayLoader(new MenuFactory()); | ||
$item = $loader->load($array); | ||
|
||
$this->assertEquals('joe', $item->getName()); | ||
$this->assertEmpty($item->getAttributes()); | ||
$this->assertCount(2, $item); | ||
$this->assertTrue(isset($item['john'])); | ||
$this->assertTrue(isset($item['jack'])); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
*/ | ||
public function testLoadInvalidData() | ||
{ | ||
$loader = new ArrayLoader(new MenuFactory()); | ||
|
||
$loader->load(new \stdClass()); | ||
} | ||
|
||
/** | ||
* @dataProvider provideSupportingData | ||
*/ | ||
public function testSupports($data, $expected) | ||
{ | ||
$loader = new ArrayLoader(new MenuFactory()); | ||
|
||
$this->assertSame($expected, $loader->supports($data)); | ||
} | ||
|
||
public function provideSupportingData() | ||
{ | ||
return array( | ||
array(array(), true), | ||
array(null, false), | ||
array('foobar', false), | ||
array(new \stdClass(), false), | ||
array(53, false), | ||
array(true, false), | ||
); | ||
} | ||
} |
Oops, something went wrong.