Completed
Push — master ( 4c8ea7...2ea838 )
by Arjay
01:19
created

Field::attr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Yajra\DataTables\Html\Editor\Fields;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Support\Fluent;
7
use Illuminate\Support\Str;
8
9
/**
10
 * @see https://editor.datatables.net/reference/option/
11
 */
12
class Field extends Fluent
13
{
14
    /**
15
     * Field type.
16
     *
17
     * @var string
18
     */
19
    protected $type = 'text';
20
21
    /**
22
     * Password constructor.
23
     */
24
    public function __construct($attributes = [])
25
    {
26
        $attributes['type'] = $attributes['type'] ?? $this->type;
27
28
        parent::__construct($attributes);
29
    }
30
31
    /**
32
     * Make a new instance of a field.
33
     *
34
     * @param string $name
35
     * @param string $label
36
     * @return static
37
     */
38
    public static function make($name, $label = '')
39
    {
40
        if (is_array($name)) {
41
            return new static($name);
42
        }
43
44
        $data = [
45
            'name' => $name,
46
            'label' => $label ?: Str::title(str_replace('_', ' ', $name)),
47
        ];
48
49
        return new static($data);
50
    }
51
52
    /**
53
     * @param string $label
54
     * @return $this
55
     * @see https://editor.datatables.net/reference/option/fields.label
56
     */
57
    public function label($label)
58
    {
59
        $this->attributes['label'] = $label;
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param string $name
66
     * @return $this
67
     * @see https://editor.datatables.net/reference/option/fields.name
68
     */
69
    public function name($name)
70
    {
71
        $this->attributes['name'] = $name;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @param string $data
78
     * @return $this
79
     * @see https://editor.datatables.net/reference/option/fields.data
80
     */
81
    public function data($data)
82
    {
83
        $this->attributes['data'] = $data;
84
85
        return $this;
86
    }
87
88
    /**
89
     * @param string $type
90
     * @return $this
91
     * @see https://editor.datatables.net/reference/option/fields.type
92
     */
93
    public function type($type)
94
    {
95
        $this->attributes['type'] = $type;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Get options from a model.
102
     *
103
     * @param mixed $model
104
     * @param string $value
105
     * @param string $key
106
     * @return $this
107
     */
108
    public function modelOptions($model, $value, $key = 'id')
109
    {
110
        return $this->options(
111
            Options::model($model, $value, $key)
112
        );
113
    }
114
115
    /**
116
     * Set select options.
117
     *
118
     * @param array|mixed $options
119
     * @return $this
120
     */
121
    public function options($options)
122
    {
123
        if ($options instanceof Arrayable) {
124
            $options = $options->toArray();
125
        }
126
127
        $this->attributes['options'] = $options;
128
129
        return $this;
130
    }
131
132
    /**
133
     * Get options from a table.
134
     *
135
     * @param mixed $table
136
     * @param string $value
137
     * @param string $key
138
     * @param \Closure $whereCallback
139
     * @param string|null $key
140
     * @return $this
141
     */
142
    public function tableOptions($table, $value, $key = 'id', \Closure $whereCallback = null, $connection = null)
143
    {
144
        return $this->options(
145
            Options::table($table, $value, $key, $whereCallback, $connection)
146
        );
147
    }
148
149
    /**
150
     * Set checkbox separator.
151
     *
152
     * @param string $separator
153
     * @return $this
154
     */
155
    public function separator($separator = ',')
156
    {
157
        $this->attributes['separator'] = $separator;
158
159
        return $this;
160
    }
161
162
    /**
163
     * Set dateTime format.
164
     *
165
     * @param string $format
166
     * @return $this
167
     * @see https://editor.datatables.net/reference/field/datetime
168
     */
169
    public function format($format)
170
    {
171
        $this->attributes['format'] = $format;
172
173
        return $this;
174
    }
175
176
    /**
177
     * Set field default value.
178
     *
179
     * @param mixed $value
180
     * @return $this
181
     * @see https://editor.datatables.net/reference/option/fields.def
182
     */
183
    public function default($value)
184
    {
185
        $this->attributes['def'] = $value;
186
187
        return $this;
188
    }
189
190
    /**
191
     * Set field message value.
192
     *
193
     * @param string $value
194
     * @return $this
195
     * @see https://editor.datatables.net/reference/option/fields.message
196
     */
197
    public function message($value)
198
    {
199
        $this->attributes['message'] = $value;
200
201
        return $this;
202
    }
203
204
    /**
205
     * Set field fieldInfo value.
206
     *
207
     * @param string $value
208
     * @return $this
209
     * @see https://editor.datatables.net/reference/option/fields.fieldInfo
210
     */
211
    public function fieldInfo($value)
212
    {
213
        $this->attributes['fieldInfo'] = $value;
214
215
        return $this;
216
    }
217
218
    /**
219
     * Set field labelInfo value.
220
     *
221
     * @param string $value
222
     * @return $this
223
     * @see https://editor.datatables.net/reference/option/fields.labelInfo
224
     */
225
    public function labelInfo($value)
226
    {
227
        $this->attributes['labelInfo'] = $value;
228
229
        return $this;
230
    }
231
232
    /**
233
     * Set field entityDecode value.
234
     *
235
     * @param mixed|bool $value
236
     * @return $this
237
     * @see https://editor.datatables.net/reference/option/fields.entityDecode
238
     */
239
    public function entityDecode($value)
240
    {
241
        $this->attributes['entityDecode'] = $value;
242
243
        return $this;
244
    }
245
246
    /**
247
     * Set field multiEditable value.
248
     *
249
     * @param mixed|bool $value
250
     * @return $this
251
     * @see https://editor.datatables.net/reference/option/fields.multiEditable
252
     */
253
    public function multiEditable($value)
254
    {
255
        $this->attributes['multiEditable'] = $value;
256
257
        return $this;
258
    }
259
260
    /**
261
     * Set field id value.
262
     *
263
     * @param string $value
264
     * @return $this
265
     * @see https://editor.datatables.net/reference/option/fields.id
266
     */
267
    public function id($value)
268
    {
269
        $this->attributes['id'] = $value;
270
271
        return $this;
272
    }
273
274
    /**
275
     * Set field submit value.
276
     *
277
     * @param bool $value
278
     * @return $this
279
     * @see https://editor.datatables.net/reference/option/fields.submit
280
     */
281
    public function submit($value)
282
    {
283
        $this->attributes['submit'] = $value;
284
285
        return $this;
286
    }
287
288
    /**
289
     * Set field compare value.
290
     *
291
     * @param bool $value
292
     * @return $this
293
     * @see https://editor.datatables.net/reference/option/fields.compare
294
     */
295
    public function compare($value)
296
    {
297
        $this->attributes['compare'] = $value;
298
299
        return $this;
300
    }
301
302
    /**
303
     * Set field opts value.
304
     *
305
     * @param bool $value
306
     * @return $this
307
     */
308
    public function opts(array $value)
309
    {
310
        $this->attributes['opts'] = $value;
311
312
        return $this;
313
    }
314
315
    /**
316
     * Set field attr option.
317
     *
318
     * @param string $attribute
319
     * @param string $value
320
     * @return $this
321
     */
322
    public function attr($attribute, $value)
323
    {
324
        $this->attributes['attr'][$attribute] = $value;
325
326
        return $this;
327
    }
328
}
329