Completed
Push — master ( 166b30...68e592 )
by WEBEWEB
01:45
created

NavigationTreeHelper::activeTree()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-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\BootstrapBundle\Navigation;
13
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * Navigation tree helper.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Bundle\BootstrapBundle\Navigation
21
 */
22
class NavigationTreeHelper {
23
24
    /**
25
     * Actives the nodes.
26
     *
27
     * @param Request $request The request.
28
     * @param array $nodes The nodes.
29
     * @param int $level The node level.
30
     * @return boolean Returns true in case of success, false othewise.
31
     */
32
    protected static function activeNodes(Request $request, array $nodes = [], $level = 0) {
33
34
        // Initialize the result.
35
        $result = false;
36
37
        // Handle each node.
38
        foreach ($nodes as $n) {
39
40
            // Check the node.
41
            if (false === ($n instanceOf AbstractNavigationNode)) {
42
                continue;
43
            }
44
45
            // Init.
46
            $current = false;
47
            $delete  = false;
48
49
            // Determines if the current node matches the URL.
50
            if (true === self::nodeMatch($n, $request)) {
51
                $current = true;
52
                $delete  = true;
53
            } else {
54
                $current = self::activeNodes($request, $n->getNodes(), $level + 1);
55
            }
56
57
            // Handle the next node.
58
            if (false === $current) {
59
                continue;
60
            }
61
62
            // Mark the node as active.
63
            $n->setActive(true);
64
65
            // Remove the icon only on the last level.
66
            if (true === $delete && 0 < $level) {
67
                $n->setIcon(null);
68
            }
69
70
            // Set the result.
71
            $result = true;
72
        }
73
74
        // Return the result.
75
        return $result;
76
    }
77
78
    /**
79
     * Active the tree.
80
     *
81
     * @param NavigationTree $tree The tree.
82
     * @param Request $request The request.
83
     * @return void
84
     */
85
    public static function activeTree(NavigationTree $tree, Request $request) {
86
        self::activeNodes($request, $tree->getNodes());
87
    }
88
89
    /**
90
     * Get the breadcrumbs.
91
     *
92
     * @param AbstractNavigationNode $node The tree.
93
     * @return AsbtractNavigationNode[] Returns the breadcrumbs.
94
     */
95
    public static function getBreadcrumbs(AbstractNavigationNode $node) {
96
97
        // Create the breadcrumbs.
98
        $breadcrumbs = [];
99
100
        // Check the instance.
101
        if (true === ($node instanceof NavigationNode || $node instanceof BreadcrumbNode) && true === $node->getActive()) {
102
            $breadcrumbs[] = $node;
103
        }
104
105
        // Handle each node.
106
        foreach ($node->getNodes() as $current) {
107
            if (false === ($current instanceof AbstractNavigationNode)) {
108
                continue;
109
            }
110
            $breadcrumbs = array_merge($breadcrumbs, self::getBreadcrumbs($current));
111
        }
112
113
        // Return the breadcrumbs.
114
        return $breadcrumbs;
115
    }
116
117
    /**
118
     * Determines if a node match an URL.
119
     *
120
     * @param AbstractNavigationNode $node The node.
121
     * @param Request $request The request.
122
     * @return boolean Returns true in case of success, false otherwise.
123
     */
124
    protected static function nodeMatch(AbstractNavigationNode $node, Request $request) {
125
126
        // Initialize the result.
127
        $result = false;
128
129
        //
130
        switch ($node->getMatcher()) {
131
132
            case NavigationInterface::NAVIGATION_MATCHER_REGEXP:
133
                $result = preg_match("/" . $node->getRoute() . "/", $request->getUri());
134
                break;
135
136
            case NavigationInterface::NAVIGATION_MATCHER_ROUTER:
137
                $result = $request->get("_route") === $node->getRoute();
138
                break;
139
140
            case NavigationInterface::NAVIGATION_MATCHER_URL:
141
                $result = $request->getUri() === $node->getRoute() || $request->getRequestUri() === $node->getRoute();
142
                break;
143
        }
144
145
        // Return the result.
146
        return boolval($result);
147
    }
148
149
}
150