Completed
Push — master ( e8977d...7ee9fe )
by Sebastian
04:26
created

Menu::toHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Menu\Laravel;
4
5
use Illuminate\Contracts\Support\Htmlable;
6
use Illuminate\Support\Traits\Macroable;
7
use Illuminate\Contracts\Auth\Access\Gate;
8
use Spatie\Menu\Item;
9
use Spatie\Menu\Menu as BaseMenu;
10
11
class Menu extends BaseMenu implements Htmlable
12
{
13
    use Macroable;
14
15
    /**
16
     * Set all relevant children active based on the current request's URL.
17
     *
18
     * /, /about, /contact => request to /about will set the about link active.
19
     *
20
     * /en, /en/about, /en/contact => request to /en won't set /en active if the
21
     *                                request root is set to /en.
22
     *
23
     * @param string $requestRoot If the link's URL is an exact match with the
24
     *                            request root, the link won't be set active.
25
     *                            This behavior is to avoid having home links
26
     *                            active on every request.
27
     *
28
     * @return $this
29
     */
30
    public function setActiveFromRequest(string $requestRoot = '')
31
    {
32
        return $this->setActive(app('request')->url(), $requestRoot);
33
    }
34
35
    /**
36
     * @param string $path
37
     * @param string $text
38
     * @param array $parameters
39
     * @param bool|null $secure
40
     *
41
     * @return $this
42
     */
43
    public function url(string $path, string $text, array $parameters = [], $secure = null)
44
    {
45
        return $this->add(Link::url($path, $text, $parameters, $secure));
46
    }
47
48
    /**
49
     * @param string $action
50
     * @param string $text
51
     * @param array $parameters
52
     * @param bool $absolute
53
     *
54
     * @return $this
55
     */
56
    public function action(string $action, string $text, array $parameters = [], bool $absolute = true)
57
    {
58
        return $this->add(Link::action($action, $text, $parameters, $absolute));
59
    }
60
61
    /**
62
     * @param string $name
63
     * @param string $text
64
     * @param array $parameters
65
     * @param bool $absolute
66
     * @param \Illuminate\Routing\Route|null $route
67
     *
68
     * @return $this
69
     */
70
    public function route(string $name, string $text, array $parameters = [], bool $absolute = true, $route = null)
71
    {
72
        return $this->add(Link::route($name, $text, $parameters, $absolute, $route));
73
    }
74
75
    /**
76
     * @param bool $condition
77
     * @param string $path
78
     * @param string $text
79
     * @param array $parameters
80
     * @param bool|null $secure
81
     *
82
     * @return $this
83
     */
84
    public function urlIf($condition, string $path, string $text, array $parameters = [], $secure = null)
85
    {
86
        return $this->addIf($condition, Link::url($path, $text, $parameters, $secure));
87
    }
88
89
    /**
90
     * @param bool $condition
91
     * @param string $action
92
     * @param string $text
93
     * @param array $parameters
94
     * @param bool $absolute
95
     *
96
     * @return $this
97
     */
98
    public function actionIf($condition, string $action, string $text, array $parameters = [], bool $absolute = true)
99
    {
100
        return $this->addIf($condition, Link::action($action, $text, $parameters, $absolute));
101
    }
102
103
    /**
104
     * @param bool $condition
105
     * @param string $name
106
     * @param string $text
107
     * @param array $parameters
108
     * @param bool $absolute
109
     * @param \Illuminate\Routing\Route|null $route
110
     *
111
     * @return $this
112
     */
113
    public function routeIf($condition, string $name, string $text, array $parameters = [], bool $absolute = true, $route = null)
114
    {
115
        return $this->addIf($condition, Link::route($name, $text, $parameters, $absolute, $route));
116
    }
117
118
    /**
119
     * @param string|array $authorization
120
     * @param \Spatie\Menu\Item $item
121
     *
122
     * @return $this
123
     */
124
    public function addIfCan($authorization, Item $item)
125
    {
126
        $ablityArguments = is_array($authorization) ? $authorization : [$authorization];
127
        $ability = array_shift($ablityArguments);
128
129
        return $this->addIf(app(Gate::class)->allows($ability, $ablityArguments), $item);
130
    }
131
132
    /**
133
     * @param string|array $authorization
134
     * @param string $url
135
     * @param string $text
136
     *
137
     * @return $this
138
     */
139
    public function linkIfCan($authorization, string $url, string $text)
140
    {
141
        return $this->addIfCan($authorization, Link::to($url, $text));
142
    }
143
144
    /**
145
     * @param string|array $authorization
146
     * @param string $html
147
     *
148
     * @return \Spatie\Menu\Laravel\Menu
149
     */
150
    public function htmlIfCan($authorization, string $html)
151
    {
152
        return $this->addIfCan($authorization, Html::raw($html));
153
    }
154
155
    /**
156
     * @param string|array $authorization
157
     * @param string $path
158
     * @param string $text
159
     * @param array $parameters
160
     * @param bool|null $secure
161
     *
162
     * @return $this
163
     */
164
    public function urlIfCan($authorization, string $path, string $text, array $parameters = [], $secure = null)
165
    {
166
        return $this->addIfCan($authorization, Link::url($path, $text, $parameters, $secure));
167
    }
168
169
    /**
170
     * @param string|array $authorization
171
     * @param string $action
172
     * @param string $text
173
     * @param array $parameters
174
     * @param bool $absolute
175
     *
176
     * @return $this
177
     */
178
    public function actionIfCan($authorization, string $action, string $text, array $parameters = [], bool $absolute = true)
179
    {
180
        return $this->addIfCan($authorization, Link::action($action, $text, $parameters, $absolute));
181
    }
182
183
    /**
184
     * @param string|array $authorization
185
     * @param string $name
186
     * @param string $text
187
     * @param array $parameters
188
     * @param bool $absolute
189
     * @param \Illuminate\Routing\Route|null $route
190
     *
191
     * @return $this
192
     */
193
    public function routeIfCan($authorization, string $name, string $text, array $parameters = [], bool $absolute = true, $route = null)
194
    {
195
        return $this->addIfCan($authorization, Link::route($name, $text, $parameters, $absolute, $route));
196
    }
197
198
    /**
199
     * @return string
200
     */
201
    public function toHtml() : string
202
    {
203
        return $this->render();
204
    }
205
}
206