From 12970b3afb011fcd02ee6da0b3fb110c2a89347b Mon Sep 17 00:00:00 2001 From: Igor Nikolaev Date: Tue, 24 Nov 2020 13:24:00 +0300 Subject: [PATCH] Extract JSON renderer data collector service. --- CHANGELOG.md | 2 ++ Renderer/Json/DataCollector.php | 33 ++++++++++++++++++++++++ Renderer/Json/DataCollectorInterface.php | 27 +++++++++++++++++++ Renderer/Json/JsonRenderer.php | 30 +++++++-------------- Resources/config/services/renderer.yaml | 12 ++++++--- 5 files changed, 81 insertions(+), 23 deletions(-) create mode 100644 Renderer/Json/DataCollector.php create mode 100644 Renderer/Json/DataCollectorInterface.php diff --git a/CHANGELOG.md b/CHANGELOG.md index cabb28d..1d9a7a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -107,3 +107,5 @@ darvin_menu: 7.0.23: Initialize menu item translations in switch menu event subscriber. 7.0.24: Make private JsonRenderer::toArray() protected. + +7.0.25: Extract JSON renderer data collector service. diff --git a/Renderer/Json/DataCollector.php b/Renderer/Json/DataCollector.php new file mode 100644 index 0000000..f70d89c --- /dev/null +++ b/Renderer/Json/DataCollector.php @@ -0,0 +1,33 @@ + + * @copyright Copyright (c) 2020, Darvin Studio + * @link https://www.darvin-studio.ru + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Darvin\MenuBundle\Renderer\Json; + +use Knp\Menu\ItemInterface; + +/** + * JSON renderer data collector + */ +class DataCollector implements DataCollectorInterface +{ + /** + * {@inheritDoc} + */ + public function getData(ItemInterface $item, array $ids): array + { + return [ + 'id' => $ids[$item->getName()], + 'name' => $item->getLabel(), + 'href' => $item->getUri(), + 'hasChild' => $item->hasChildren(), + 'parentId' => null !== $item->getParent() && !$item->getParent()->isRoot() ? $ids[$item->getParent()->getName()] : null, + ]; + } +} diff --git a/Renderer/Json/DataCollectorInterface.php b/Renderer/Json/DataCollectorInterface.php new file mode 100644 index 0000000..5600ed5 --- /dev/null +++ b/Renderer/Json/DataCollectorInterface.php @@ -0,0 +1,27 @@ + + * @copyright Copyright (c) 2020, Darvin Studio + * @link https://www.darvin-studio.ru + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Darvin\MenuBundle\Renderer\Json; + +use Knp\Menu\ItemInterface; + +/** + * JSON renderer data collector + */ +interface DataCollectorInterface +{ + /** + * @param \Knp\Menu\ItemInterface $item Menu item + * @param array $ids IDs + * + * @return array + */ + public function getData(ItemInterface $item, array $ids): array; +} diff --git a/Renderer/Json/JsonRenderer.php b/Renderer/Json/JsonRenderer.php index 8c3c087..d54eed8 100644 --- a/Renderer/Json/JsonRenderer.php +++ b/Renderer/Json/JsonRenderer.php @@ -19,16 +19,23 @@ */ class JsonRenderer implements RendererInterface { + /** + * @var \Darvin\MenuBundle\Renderer\Json\DataCollectorInterface + */ + private $dataCollector; + /** * @var \Darvin\Utils\Json\JsonEncoderInterface */ private $encoder; /** - * @param \Darvin\Utils\Json\JsonEncoderInterface $encoder JSON encoder + * @param \Darvin\MenuBundle\Renderer\Json\DataCollectorInterface $dataCollector Data collector + * @param \Darvin\Utils\Json\JsonEncoderInterface $encoder JSON encoder */ - public function __construct(JsonEncoderInterface $encoder) + public function __construct(DataCollectorInterface $dataCollector, JsonEncoderInterface $encoder) { + $this->dataCollector = $dataCollector; $this->encoder = $encoder; } @@ -42,23 +49,6 @@ public function render(ItemInterface $item, array $options = []): string return $this->encoder->encode($this->buildArray($item, $ids)); } - /** - * @param \Knp\Menu\ItemInterface $item Menu item - * @param array $ids IDs - * - * @return array - */ - protected function toArray(ItemInterface $item, array $ids): array - { - return [ - 'id' => $ids[$item->getName()], - 'name' => $item->getLabel(), - 'href' => $item->getUri(), - 'hasChild' => $item->hasChildren(), - 'parentId' => null !== $item->getParent() && !$item->getParent()->isRoot() ? $ids[$item->getParent()->getName()] : null, - ]; - } - /** * @param \Knp\Menu\ItemInterface $item Menu item * @param string $prefix ID prefix @@ -102,7 +92,7 @@ private function buildArray(ItemInterface $item, array $ids): array continue; } - $array[] = array_filter($this->toArray($child, $ids), function ($value): bool { + $array[] = array_filter($this->dataCollector->getData($child, $ids), function ($value): bool { return null !== $value; }); diff --git a/Resources/config/services/renderer.yaml b/Resources/config/services/renderer.yaml index 83d8eb4..902650a 100644 --- a/Resources/config/services/renderer.yaml +++ b/Resources/config/services/renderer.yaml @@ -1,18 +1,24 @@ parameters: darvin_menu.renderer.json.class: Darvin\MenuBundle\Renderer\Json\JsonRenderer - darvin_menu.renderer.json.twig_extension.class: Darvin\MenuBundle\Twig\Extension\Renderer\JsonRendererExtension + darvin_menu.renderer.json.data.collector.class: Darvin\MenuBundle\Renderer\Json\DataCollector + + darvin_menu.renderer.json.twig.extension.class: Darvin\MenuBundle\Twig\Extension\Renderer\JsonRendererExtension services: darvin_menu.renderer.json: class: '%darvin_menu.renderer.json.class%' arguments: + - '@darvin_menu.renderer.json.data.collector' - '@darvin_utils.json.encoder' tags: - { name: knp_menu.renderer, alias: json } - darvin_menu.renderer.json.twig_extension: - class: '%darvin_menu.renderer.json.twig_extension.class%' + darvin_menu.renderer.json.data.collector: + class: '%darvin_menu.renderer.json.data.collector.class%' + + darvin_menu.renderer.json.twig.extension: + class: '%darvin_menu.renderer.json.twig.extension.class%' arguments: - '@darvin_menu.renderer.json' tags: