Completed
Push — master ( d702e6...5bf9b6 )
by Sebastian
02:14
created

Menu::viewIfCan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
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::toUrl($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::toAction($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::toRoute($name, $text, $parameters, $absolute, $route));
73
    }
74
75
    /**
76
     * @param string $name
77
     * @param array $data
78
     *
79
     * @return $this
80
     */
81
    public function view(string $name, array $data = [])
82
    {
83
        return $this->add(View::create($name, $data));
84
    }
85
86
    /**
87
     * @param bool $condition
88
     * @param string $path
89
     * @param string $text
90
     * @param array $parameters
91
     * @param bool|null $secure
92
     *
93
     * @return $this
94
     */
95
    public function urlIf($condition, string $path, string $text, array $parameters = [], $secure = null)
96
    {
97
        return $this->addIf($condition, Link::toUrl($path, $text, $parameters, $secure));
98
    }
99
100
    /**
101
     * @param bool $condition
102
     * @param string $action
103
     * @param string $text
104
     * @param array $parameters
105
     * @param bool $absolute
106
     *
107
     * @return $this
108
     */
109
    public function actionIf($condition, string $action, string $text, array $parameters = [], bool $absolute = true)
110
    {
111
        return $this->addIf($condition, Link::toAction($action, $text, $parameters, $absolute));
112
    }
113
114
    /**
115
     * @param bool $condition
116
     * @param string $name
117
     * @param string $text
118
     * @param array $parameters
119
     * @param bool $absolute
120
     * @param \Illuminate\Routing\Route|null $route
121
     *
122
     * @return $this
123
     */
124
    public function routeIf($condition, string $name, string $text, array $parameters = [], bool $absolute = true, $route = null)
125
    {
126
        return $this->addIf($condition, Link::toRoute($name, $text, $parameters, $absolute, $route));
127
    }
128
129
    /**
130
     * @param $condition
131
     * @param string $name
132
     * @param array $data
133
     *
134
     * @return $this
135
     */
136
    public function viewIf($condition, string $name, array $data)
137
    {
138
        return $this->addIf($condition, View::create($name, $data));
139
    }
140
141
    /**
142
     * @param string|array $authorization
143
     * @param \Spatie\Menu\Item $item
144
     *
145
     * @return $this
146
     */
147
    public function addIfCan($authorization, Item $item)
148
    {
149
        $ablityArguments = is_array($authorization) ? $authorization : [$authorization];
150
        $ability = array_shift($ablityArguments);
151
152
        return $this->addIf(app(Gate::class)->allows($ability, $ablityArguments), $item);
153
    }
154
155
    /**
156
     * @param string|array $authorization
157
     * @param string $url
158
     * @param string $text
159
     *
160
     * @return $this
161
     */
162
    public function linkIfCan($authorization, string $url, string $text)
163
    {
164
        return $this->addIfCan($authorization, Link::to($url, $text));
165
    }
166
167
    /**
168
     * @param string|array $authorization
169
     * @param string $html
170
     *
171
     * @return \Spatie\Menu\Laravel\Menu
172
     */
173
    public function htmlIfCan($authorization, string $html)
174
    {
175
        return $this->addIfCan($authorization, Html::raw($html));
176
    }
177
178
    /**
179
     * @param string|array $authorization
180
     * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header
181
     * @param callable|\Spatie\Menu\Menu|null $menu
182
     *
183
     * @return $this
184
     */
185
    public function submenuIfCan($authorization, $header, $menu = null)
0 ignored issues
show
Unused Code introduced by
The parameter $authorization is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $header is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $menu is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
186
    {
187
        list($authorization, $header, $menu) = $this->parseSubmenuIfCanArgs(...func_get_args());
188
189
        $menu = $this->createSubmenuMenu($menu);
190
        $header = $this->createSubmenuHeader($header);
191
192
        return $this->addIfCan($authorization, $menu->prependIf($header, $header));
0 ignored issues
show
Documentation introduced by
$header is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
193
    }
194
195
    protected function parseSubmenuIfCanArgs($authorization, ...$args): array
196
    {
197
        return array_merge([$authorization], $this->parseSubmenuArgs($args));
198
    }
199
200
    /**
201
     * @param string|array $authorization
202
     * @param string $path
203
     * @param string $text
204
     * @param array $parameters
205
     * @param bool|null $secure
206
     *
207
     * @return $this
208
     */
209
    public function urlIfCan($authorization, string $path, string $text, array $parameters = [], $secure = null)
210
    {
211
        return $this->addIfCan($authorization, Link::toUrl($path, $text, $parameters, $secure));
212
    }
213
214
    /**
215
     * @param string|array $authorization
216
     * @param string $action
217
     * @param string $text
218
     * @param array $parameters
219
     * @param bool $absolute
220
     *
221
     * @return $this
222
     */
223
    public function actionIfCan($authorization, string $action, string $text, array $parameters = [], bool $absolute = true)
224
    {
225
        return $this->addIfCan($authorization, Link::toAction($action, $text, $parameters, $absolute));
226
    }
227
228
    /**
229
     * @param string|array $authorization
230
     * @param string $name
231
     * @param string $text
232
     * @param array $parameters
233
     * @param bool $absolute
234
     * @param \Illuminate\Routing\Route|null $route
235
     *
236
     * @return $this
237
     */
238
    public function routeIfCan($authorization, string $name, string $text, array $parameters = [], bool $absolute = true, $route = null)
239
    {
240
        return $this->addIfCan($authorization, Link::toRoute($name, $text, $parameters, $absolute, $route));
241
    }
242
243
    /**
244
     * @param $condition
245
     * @param string $name
246
     * @param array $data
247
     *
248
     * @return $this
249
     */
250
    public function viewIfCan($authorization, string $name, array $data)
251
    {
252
        return $this->addIfCan($authorization, View::create($name, $data));
253
    }
254
255
    /**
256
     * @return string
257
     */
258
    public function toHtml() : string
259
    {
260
        return $this->render();
261
    }
262
}
263