Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

Kunstmaan/MenuBundle/Twig/MenuTwigExtension.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\MenuBundle\Twig;
4
5
use Kunstmaan\MenuBundle\Entity\MenuItem;
6
use Kunstmaan\MenuBundle\Repository\MenuItemRepositoryInterface;
7
use Kunstmaan\MenuBundle\Service\RenderService;
8
use Twig\Environment;
9
use Twig\Extension\AbstractExtension;
10
use Twig\TwigFunction;
11
12
/**
13
 * @final since 5.4
14
 */
15
class MenuTwigExtension extends AbstractExtension
16
{
17
    /**
18
     * @var RenderService
19
     */
20
    private $renderService;
21
22
    /**
23
     * @var MenuItemRepositoryInterface
24
     */
25
    private $repository;
26
27
    /**
28
     * @param MenuItemRepositoryInterface $repository
29
     * @param RenderService               $renderService
30
     */
31
    public function __construct(MenuItemRepositoryInterface $repository, RenderService $renderService)
32
    {
33
        $this->renderService = $renderService;
34
        $this->repository = $repository;
35
    }
36
37
    /**
38
     * Returns a list of functions to add to the existing list.
39
     *
40
     * @return array An array of functions
0 ignored issues
show
Consider making the return type a bit more specific; maybe use TwigFunction[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
41
     */
42
    public function getFunctions()
43
    {
44
        return array(
45
            new TwigFunction(
46
                'get_menu',
47
                array($this, 'getMenu'),
48
                array(
49
                    'is_safe' => array('html'),
50
                    'needs_environment' => true,
51
                )
52
            ),
53
            new TwigFunction('get_menu_items', array($this, 'getMenuItems')),
54
        );
55
    }
56
57
    /**
58
     * Get a html representation of a menu.
59
     *
60
     * @param string $name
61
     * @param string $lang
62
     * @param array  $options
63
     *
64
     * @return string
65
     */
66
    public function getMenu(Environment $environment, $name, $lang, $options = array())
67
    {
68
        $options = array_merge($this->getDefaultOptions(), $options);
69
70
        $renderService = $this->renderService;
71
        $options['nodeDecorator'] = function ($node) use ($environment, $renderService, $options) {
72
            return $renderService->renderMenuItemTemplate($environment, $node, $options);
73
        };
74
75
        $arrayResult = $this->getMenuItems($name, $lang);
76
77
        return $this->repository->buildTree($arrayResult, $options);
78
    }
79
80
    /**
81
     * Get an array with menu items of a menu.
82
     *
83
     * @param string $name
84
     * @param string $lang
85
     *
86
     * @return array
87
     */
88
    public function getMenuItems($name, $lang)
89
    {
90
        /** @var MenuItem $menuRepo */
91
        $arrayResult = $this->repository->getMenuItemsForLanguage($name, $lang);
92
93
        // Make sure the parent item is not offline
94
        $foundIds = array();
95
        foreach ($arrayResult as $array) {
96
            $foundIds[] = $array['id'];
97
        }
98
        foreach ($arrayResult as $key => $array) {
99
            if (!\is_null($array['parent']) && !\in_array($array['parent']['id'], $foundIds)) {
100
                unset($arrayResult[$key]);
101
            }
102
        }
103
104
        return $arrayResult;
105
    }
106
107
    /**
108
     * Get the default options to render the html.
109
     *
110
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string,boolean|string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
111
     */
112
    private function getDefaultOptions()
113
    {
114
        return array(
115
            'decorate' => true,
116
            'rootOpen' => '<ul>',
117
            'rootClose' => '</ul>',
118
            'childOpen' => '<li>',
119
            'childClose' => '</li>',
120
        );
121
    }
122
}
123