Completed
Push — master ( 1a8d3e...7d5060 )
by Arjay
01:43 queued 12s
created

HasColumns::addColumnDef()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace Yajra\DataTables\Html\Options;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Yajra\DataTables\Html\Column;
8
use Illuminate\Contracts\Support\Arrayable;
9
10
/**
11
 * DataTables - Columns option builder.
12
 *
13
 * @see https://datatables.net/reference/option/
14
 */
15
trait HasColumns
16
{
17
    /**
18
     * Set columnDefs option value.
19
     *
20
     * @param mixed $value
21
     * @return $this
22
     * @see https://datatables.net/reference/option/columnDefs
23
     */
24 View Code Duplication
    public function columnDefs($value)
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...
25
    {
26
        if (is_callable($value)) {
27
            $value = app()->call($value);
28
        }
29
30
        if ($value instanceof Arrayable) {
31
            $value = $value->toArray();
32
        }
33
34
        $this->attributes['columnDefs'] = $value;
0 ignored issues
show
Bug introduced by
The property attributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
36
        return $this;
37
    }
38
39
    /**
40
     * Add a columnDef option.
41
     *
42
     * @param mixed $value
43
     * @return $this
44
     * @see https://datatables.net/reference/option/columnDefs
45
     */
46 View Code Duplication
    public function addColumnDef($value)
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...
47
    {
48
        if (is_callable($value)) {
49
            $value = app()->call($value);
50
        }
51
52
        if ($value instanceof Arrayable) {
53
            $value = $value->toArray();
54
        }
55
56
        $this->attributes['columnDefs'][] = $value;
57
58
        return $this;
59
    }
60
61
    /**
62
     * Set columns option value.
63
     *
64
     * @param array $columns
65
     * @return $this
66
     * @see https://datatables.net/reference/option/columns
67
     */
68
    public function columns(array $columns)
69
    {
70
        $this->collection = new Collection;
0 ignored issues
show
Bug introduced by
The property collection does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
71
72
        foreach ($columns as $key => $value) {
73
            if (! is_a($value, Column::class)) {
74
                if (is_array($value)) {
75
                    $attributes = array_merge(
76
                        [
77
                            'name' => $value['name'] ?? $value['data'] ?? $key,
78
                            'data' => $value['data'] ?? $key,
79
                        ],
80
                        $this->setTitle($key, $value)
81
                    );
82
                } else {
83
                    $attributes = [
84
                        'name' => $value,
85
                        'data' => $value,
86
                        'title' => $this->getQualifiedTitle($value),
87
                    ];
88
                }
89
90
                $this->collection->push(new Column($attributes));
91
            } else {
92
                $this->collection->push($value);
93
            }
94
        }
95
96
        return $this;
97
    }
98
99
    /**
100
     * Set title attribute of an array if not set.
101
     *
102
     * @param string $title
103
     * @param array $attributes
104
     * @return array
105
     */
106
    public function setTitle($title, array $attributes)
107
    {
108
        if (! isset($attributes['title'])) {
109
            $attributes['title'] = $this->getQualifiedTitle($title);
110
        }
111
112
        return $attributes;
113
    }
114
115
    /**
116
     * Convert string into a readable title.
117
     *
118
     * @param string $title
119
     * @return string
120
     */
121
    public function getQualifiedTitle($title)
122
    {
123
        return Str::title(str_replace(['.', '_'], ' ', Str::snake($title)));
124
    }
125
126
    /**
127
     * Add a column in collection usingsl attributes.
128
     *
129
     * @param  array $attributes
130
     * @return $this
131
     */
132
    public function addColumn(array $attributes)
133
    {
134
        $this->collection->push(new Column($attributes));
135
136
        return $this;
137
    }
138
139
    /**
140
     * Add a Column object at the beginning of collection.
141
     *
142
     * @param \Yajra\DataTables\Html\Column $column
143
     * @return $this
144
     */
145
    public function addBefore(Column $column)
146
    {
147
        $this->collection->prepend($column);
148
149
        return $this;
150
    }
151
152
    /**
153
     * Add a column at the beginning of collection using attributes.
154
     *
155
     * @param  array $attributes
156
     * @return $this
157
     */
158
    public function addColumnBefore(array $attributes)
159
    {
160
        $this->collection->prepend(new Column($attributes));
161
162
        return $this;
163
    }
164
165
    /**
166
     * Add a Column object in collection.
167
     *
168
     * @param \Yajra\DataTables\Html\Column $column
169
     * @return $this
170
     */
171
    public function add(Column $column)
172
    {
173
        $this->collection->push($column);
174
175
        return $this;
176
    }
177
178
    /**
179
     * Get collection of columns.
180
     *
181
     * @return \Illuminate\Support\Collection
182
     */
183
    public function getColumns()
184
    {
185
        return $this->collection;
186
    }
187
188
    /**
189
     * Remove column by name.
190
     *
191
     * @param array $names
192
     * @return $this
193
     */
194
    public function removeColumn(...$names)
195
    {
196
        foreach ($names as $name) {
197
            $this->collection = $this->collection->filter(function (Column $column) use ($name) {
198
                return $column->name !== $name;
199
            })->flatten();
200
        }
201
202
        return $this;
203
    }
204
}
205