Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -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 %s', \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 = [];
}
$item = $this->factory->createItem($name, $data);
foreach ($children as $name => $child) {
$item->addChild($this->fromArray($child, $name));
}
return $item;
}
}

View File

@@ -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 bool
*/
public function supports($data);
}

View File

@@ -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 %s', \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;
}
}