Skip to content

Commit

Permalink
Extract JSON renderer data collector service.
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Nikolaev committed Nov 24, 2020
1 parent c076979 commit 12970b3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
33 changes: 33 additions & 0 deletions Renderer/Json/DataCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);
/**
* @author Igor Nikolaev <igor.sv.n@gmail.com>
* @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,
];
}
}
27 changes: 27 additions & 0 deletions Renderer/Json/DataCollectorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);
/**
* @author Igor Nikolaev <igor.sv.n@gmail.com>
* @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;
}
30 changes: 10 additions & 20 deletions Renderer/Json/JsonRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
Expand Down Expand Up @@ -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;
});

Expand Down
12 changes: 9 additions & 3 deletions Resources/config/services/renderer.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 12970b3

Please sign in to comment.