NavigationNodeHelper::nodeMatch()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 20
rs 9.2222
c 0
b 0
f 0
cc 6
nc 6
nop 2
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\CoreBundle\Helper\Assets;
13
14
use Symfony\Component\HttpFoundation\ParameterBag;
15
use Symfony\Component\HttpFoundation\Request;
16
use WBW\Library\Symfony\Assets\AbstractNavigationNode;
17
use WBW\Library\Symfony\Assets\Navigation\BreadcrumbNode;
18
use WBW\Library\Symfony\Assets\Navigation\NavigationNode;
19
use WBW\Library\Symfony\Assets\Navigation\NavigationTree;
20
use WBW\Library\Symfony\Assets\NavigationNodeInterface;
21
22
/**
23
 * Navigation node helper.
24
 *
25
 * @author webeweb <https://github.com/webeweb>
26
 * @package WBW\Bundle\CoreBundle\Helper\Assets
27
 */
28
class NavigationNodeHelper {
29
30
    /**
31
     * Active the navigation nodes.
32
     *
33
     * @param Request $request The request.
34
     * @param AbstractNavigationNode[] $nodes The navigation nodes.
35
     * @param int $level The node level.
36
     * @return bool Returns true in case of success, false otherwise.
37
     */
38
    protected static function activeNodes(Request $request, array $nodes = [], int $level = 0): bool {
39
40
        $result = false;
41
42
        foreach ($nodes as $n) {
43
44
            if (true === static::nodeMatch($n, $request)) {
45
                $current = true;
46
            } else {
47
                $current = static::activeNodes($request, $n->getNodes(), $level + 1);
48
            }
49
50
            if (false === $current) {
51
                continue;
52
            }
53
54
            $result = $n->setActive(true)->getActive();
55
        }
56
57
        return $result;
58
    }
59
60
    /**
61
     * Active a tree.
62
     *
63
     * @param NavigationTree $tree The tree.
64
     * @param Request $request The request.
65
     * @return void
66
     */
67
    public static function activeTree(NavigationTree $tree, Request $request): void {
68
        static::activeNodes($request, $tree->getNodes());
69
    }
70
71
    /**
72
     * Get the breadcrumbs.
73
     *
74
     * @param AbstractNavigationNode $node The navigation node.
75
     * @return AbstractNavigationNode[] Returns the breadcrumbs.
76
     */
77
    public static function getBreadcrumbs(AbstractNavigationNode $node): array {
78
79
        $breadcrumbs = [];
80
81
        if (true === ($node instanceof NavigationNode || $node instanceof BreadcrumbNode) && true === $node->getActive()) {
82
            $breadcrumbs[] = $node;
83
        }
84
85
        foreach ($node->getNodes() as $current) {
86
            $breadcrumbs = array_merge($breadcrumbs, static::getBreadcrumbs($current));
87
        }
88
89
        return $breadcrumbs;
90
    }
91
92
    /**
93
     * Determine if a navigation node match a URL.
94
     *
95
     * @param AbstractNavigationNode $node The navigation node.
96
     * @param Request $request The request.
97
     * @return bool Returns true in case of success, false otherwise.
98
     */
99
    protected static function nodeMatch(AbstractNavigationNode $node, Request $request): bool {
100
101
        $result = false;
102
103
        switch ($node->getMatcher()) {
104
105
            case NavigationNodeInterface::MATCHER_REGEXP:
106
                $result = preg_match("/" . $node->getUri() . "/", $request->getUri());
107
                break;
108
109
            case NavigationNodeInterface::MATCHER_ROUTER:
110
                $result = $request->get("_route") === $node->getUri() || $request->get("_forwarded", new ParameterBag())->get("_route") === $node->getUri();
111
                break;
112
113
            case NavigationNodeInterface::MATCHER_URL:
114
                $result = $request->getUri() === $node->getUri() || $request->getRequestUri() === $node->getUri();
115
                break;
116
        }
117
118
        return boolval($result);
119
    }
120
}
121