Completed
Push — master ( db6c24...d35733 )
by Arjay
01:54 queued 17s
created

Button::raw()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Yajra\DataTables\Html;
4
5
use Illuminate\Support\Fluent;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Contracts\Auth\Access\Authorizable;
8
9
class Button extends Fluent implements Arrayable
10
{
11
    /**
12
     * Flag to check if user is authorized to use the button.
13
     *
14
     * @var bool
15
     */
16
    protected $authorized = true;
17
18
    /**
19
     * Make a button if condition is true.
20
     *
21
     * @param bool|callable $condition
22
     * @param string|array $options
23
     * @return Button
24
     */
25
    public static function makeIf($condition, $options)
26
    {
27
        if (value($condition)) {
28
            return static::make($options);
29
        }
30
31
        return static::make()->authorized(false);
32
    }
33
34
    /**
35
     * Make a new button instance.
36
     *
37
     * @param string|array $options
38
     * @return Button
39
     */
40
    public static function make($options = [])
41
    {
42
        if (is_string($options)) {
43
            return new static(['extend' => $options]);
44
        }
45
46
        return new static($options);
47
    }
48
49
    /**
50
     * Set authotization status of the button.
51
     *
52
     * @param bool|callable $bool
53
     * @return $this
54
     */
55
    public function authorized($bool)
56
    {
57
        $this->authorized = value($bool);
58
59
        return $this;
60
    }
61
62
    /**
63
     * Make a raw button that does not extend anything.
64
     *
65
     * @param array $options
66
     * @return \Yajra\DataTables\Html\Button
67
     */
68
    public static function raw($options = [])
69
    {
70
        if (is_string($options)) {
71
            return new static(['text' => $options]);
72
        }
73
74
        return new static($options);
75
    }
76
77
    /**
78
     * Make a button if condition is true.
79
     *
80
     * @param string $permission
81
     * @param string|array $options
82
     * @param Authorizable|null $user
83
     * @return Button
84
     */
85
    public static function makeIfCan($permission, $options, Authorizable $user = null)
86
    {
87
        if (is_null($user)) {
88
            $user = auth()->user();
89
        }
90
91
        if ($user->can($permission)) {
92
            return static::make($options);
93
        }
94
95
        return static::make()->authorized(false);
96
    }
97
98
    /**
99
     * Set extend option value.
100
     *
101
     * @param string $value
102
     * @return $this
103
     */
104
    public function extend($value)
105
    {
106
        $this->attributes['extend'] = $value;
107
108
        return $this;
109
    }
110
111
    /**
112
     * Set editor option value.
113
     *
114
     * @param string $value
115
     * @return $this
116
     */
117
    public function editor($value)
118
    {
119
        $this->attributes['editor'] = $value;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param array $buttons
126
     * @return $this
127
     */
128 View Code Duplication
    public function buttons(array $buttons)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        foreach ($buttons as $key => $button) {
131
            if ($button instanceof Arrayable) {
132
                $buttons[$key] = $button->toArray();
133
            }
134
        }
135
136
        $this->attributes['buttons'] = $buttons;
137
138
        return $this;
139
    }
140
141
    /**
142
     * @param array $buttons
143
     * @return $this
144
     * @see https://editor.datatables.net/examples/api/cancelButton
145
     */
146 View Code Duplication
    public function formButtons(array $buttons)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
    {
148
        foreach ($buttons as $key => $button) {
149
            if ($button instanceof Arrayable) {
150
                $buttons[$key] = $button->toArray();
151
            }
152
        }
153
154
        $this->attributes['formButtons'] = $buttons;
155
156
        return $this;
157
    }
158
159
    /**
160
     * Set className option value.
161
     *
162
     * @param string $value
163
     * @return $this
164
     */
165
    public function className($value)
166
    {
167
        $this->attributes['className'] = $value;
168
169
        return $this;
170
    }
171
172
    /**
173
     * Set action option value.
174
     *
175
     * @param string $value
176
     * @return $this
177
     */
178
    public function action($value)
179
    {
180
        $this->attributes['action'] = $value;
181
182
        return $this;
183
    }
184
185
    /**
186
     * Set text option value.
187
     *
188
     * @param string $value
189
     * @return $this
190
     */
191
    public function text($value)
192
    {
193
        $this->attributes['text'] = $value;
194
195
        return $this;
196
    }
197
198
    /**
199
     * Set columns option value.
200
     *
201
     * @param mixed $value
202
     * @return $this
203
     */
204
    public function columns($value)
205
    {
206
        $this->attributes['columns'] = $value;
207
208
        return $this;
209
    }
210
211
    /**
212
     * Set exportOptions option value.
213
     *
214
     * @param mixed $value
215
     * @return $this
216
     */
217
    public function exportOptions($value)
218
    {
219
        $this->attributes['exportOptions'] = $value;
220
221
        return $this;
222
    }
223
224
    /**
225
     * Convert the Fluent instance to an array.
226
     *
227
     * @return array
228
     */
229
    public function toArray()
230
    {
231
        if ($this->authorized) {
232
            return parent::toArray();
233
        }
234
235
        return [];
236
    }
237
}
238