Completed
Push — master ( 587667...bbfed8 )
by Sebastian
07:41
created

Menu::actionIfCan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 5
1
<?php
2
3
namespace Spatie\Menu\Laravel;
4
5
use Illuminate\Support\Traits\Macroable;
6
use Gate;
7
use Spatie\Menu\Menu as BaseMenu;
8
9
class Menu extends BaseMenu
10
{
11
    use Macroable;
12
13
    /**
14
     * Set all relevant children active based on the current request's URL.
15
     *
16
     * /, /about, /contact => request to /about will set the about link active.
17
     *
18
     * /en, /en/about, /en/contact => request to /en won't set /en active if the
19
     *                                request root is set to /en.
20
     *
21
     * @param string $requestRoot If the link's URL is an exact match with the
22
     *                            request root, the link won't be set active.
23
     *                            This behavior is to avoid having home links
24
     *                            active on every request.
25
     *
26
     * @return $this
27
     */
28
    public function setActiveFromRequest(string $requestRoot = '')
29
    {
30
        return $this->setActive(app('request')->url(), $requestRoot);
31
    }
32
33
    /**
34
     * @param string $path
35
     * @param string $text
36
     * @param array $parameters
37
     * @param bool|null $secure
38
     *
39
     * @return $this
40
     */
41
    public function url(string $path, string $text, array $parameters = [], $secure = null)
42
    {
43
        return $this->add(Link::url($path, $text, $parameters, $secure));
44
    }
45
46
    /**
47
     * @param string $action
48
     * @param string $text
49
     * @param array $parameters
50
     * @param bool $absolute
51
     *
52
     * @return $this
53
     */
54
    public function action(string $action, string $text, array $parameters = [], bool $absolute = true)
55
    {
56
        return $this->add(Link::action($action, $text, $parameters, $absolute));
57
    }
58
59
    /**
60
     * @param string $name
61
     * @param string $text
62
     * @param array $parameters
63
     * @param bool $absolute
64
     * @param \Illuminate\Routing\Route|null $route
65
     *
66
     * @return $this
67
     */
68
    public function route(string $name, string $text, array $parameters = [], bool $absolute = true, $route = null)
69
    {
70
        return $this->add(Link::route($name, $text, $parameters, $absolute, $route));
71
    }
72
73
    /**
74
     * @param bool $condition
75
     * @param string $path
76
     * @param string $text
77
     * @param array $parameters
78
     * @param bool|null $secure
79
     *
80
     * @return $this
81
     */
82
    public function urlIf($condition, string $path, string $text, array $parameters = [], $secure = null)
83
    {
84
        return $this->addIf($condition, Link::url($path, $text, $parameters, $secure));
85
    }
86
87
    /**
88
     * @param bool $condition
89
     * @param string $action
90
     * @param string $text
91
     * @param array $parameters
92
     * @param bool $absolute
93
     *
94
     * @return $this
95
     */
96
    public function actionIf($condition, string $action, string $text, array $parameters = [], bool $absolute = true)
97
    {
98
        return $this->addIf($condition, Link::action($action, $text, $parameters, $absolute));
99
    }
100
101
    /**
102
     * @param bool $condition
103
     * @param string $name
104
     * @param string $text
105
     * @param array $parameters
106
     * @param bool $absolute
107
     * @param \Illuminate\Routing\Route|null $route
108
     *
109
     * @return $this
110
     */
111
    public function routeIf($condition, string $name, string $text, array $parameters = [], bool $absolute = true, $route = null)
112
    {
113
        return $this->addIf($condition, Link::route($name, $text, $parameters, $absolute, $route));
114
    }
115
116
    /**
117
     * @param string|array $authorization
118
     * @param \Spatie\Menu\Laravel\Item $item
119
     *
120
     * @return $this
121
     */
122
    public function addIfCan($authorization, Item $item)
123
    {
124
        $arguments = is_array($authorization) ? $authorization : [$authorization];
125
        $ability = array_shift($ablityArguments);
126
127
        return $this->addIf(Gate::allows($ability, $arguments), $item);
128
    }
129
130
    /**
131
     * @param string|array $authorization
132
     * @param string $url
133
     * @param string $text
134
     *
135
     * @return $this
136
     */
137
    public function linkIfCan($authorization, string $url, string $text)
138
    {
139
        return $this->addIfCan($authorization, Link::to($url, $text));
0 ignored issues
show
Documentation introduced by
\Spatie\Menu\Laravel\Link::to($url, $text) is of type object<Spatie\Menu\Laravel\Link>, but the function expects a object<Spatie\Menu\Laravel\Item>.

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...
140
    }
141
142
    /**
143
     * @param string|array $authorization
144
     * @param string $html
145
     *
146
     * @return \Spatie\Menu\Laravel\Menu
147
     */
148
    public function htmlIfCan($authorization, string $html)
149
    {
150
        return $this->addIfCan($authorization, Html::raw($html));
0 ignored issues
show
Documentation introduced by
\Spatie\Menu\Laravel\Html::raw($html) is of type object<Spatie\Menu\Laravel\Html>, but the function expects a object<Spatie\Menu\Laravel\Item>.

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...
151
    }
152
153
    /**
154
     * @param string|array $authorization
155
     * @param string $path
156
     * @param string $text
157
     * @param array $parameters
158
     * @param bool|null $secure
159
     *
160
     * @return $this
161
     */
162
    public function urlIfCan($authorization, string $path, string $text, array $parameters = [], $secure = null)
163
    {
164
        return $this->addIfCan($authorization, Link::url($path, $text, $parameters, $secure));
0 ignored issues
show
Documentation introduced by
\Spatie\Menu\Laravel\Lin..., $parameters, $secure) is of type object<Spatie\Menu\Laravel\Link>, but the function expects a object<Spatie\Menu\Laravel\Item>.

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...
165
    }
166
167
    /**
168
     * @param string|array $authorization
169
     * @param string $action
170
     * @param string $text
171
     * @param array $parameters
172
     * @param bool $absolute
173
     *
174
     * @return $this
175
     */
176
    public function actionIfCan($authorization, string $action, string $text, array $parameters = [], bool $absolute = true)
177
    {
178
        return $this->addIfCan($authorization, Link::action($action, $text, $parameters, $absolute));
0 ignored issues
show
Documentation introduced by
\Spatie\Menu\Laravel\Lin...$parameters, $absolute) is of type object<Spatie\Menu\Laravel\Link>, but the function expects a object<Spatie\Menu\Laravel\Item>.

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...
179
    }
180
181
    /**
182
     * @param string|array $authorization
183
     * @param string $name
184
     * @param string $text
185
     * @param array $parameters
186
     * @param bool $absolute
187
     * @param \Illuminate\Routing\Route|null $route
188
     *
189
     * @return $this
190
     */
191
    public function routeIfCan($authorization, string $name, string $text, array $parameters = [], bool $absolute = true, $route = null)
192
    {
193
        return $this->addIfCan($authorization, Link::route($name, $text, $parameters, $absolute, $route));
0 ignored issues
show
Documentation introduced by
\Spatie\Menu\Laravel\Lin...ers, $absolute, $route) is of type object<Spatie\Menu\Laravel\Link>, but the function expects a object<Spatie\Menu\Laravel\Item>.

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...
194
    }
195
}
196