Completed
Push — master ( be4127...2f3ac5 )
by Arjay
02:30 queued 29s
created

HasColumns::setTitle()   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 2
1
<?php
2
3
namespace Yajra\DataTables\Html\Options;
4
5
use Illuminate\Support\Str;
6
use Yajra\DataTables\Html\Column;
7
use Illuminate\Support\Collection;
8
9
/**
10
 * DataTables - Columns option builder.
11
 *
12
 * @see https://datatables.net/reference/option/
13
 */
14
trait HasColumns
15
{
16
    /**
17
     * Set columnDefs option value.
18
     *
19
     * @param array $value
20
     * @return $this
21
     * @see https://datatables.net/reference/option/columnDefs
22
     */
23
    public function columnDefs(array $value)
24
    {
25
        $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...
26
27
        return $this;
28
    }
29
30
    /**
31
     * Set columns option value.
32
     *
33
     * @param array $columns
34
     * @return $this
35
     * @see https://datatables.net/reference/option/columns
36
     */
37
    public function columns(array $columns)
38
    {
39
        $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...
40
41
        foreach ($columns as $key => $value) {
42
            if (! is_a($value, Column::class)) {
43
                if (is_array($value)) {
44
                    $attributes = array_merge(
45
                        [
46
                            'name' => $value['name'] ?? $value['data'] ?? $key,
47
                            'data' => $value['data'] ?? $key,
48
                        ],
49
                        $this->setTitle($key, $value)
50
                    );
51
                } else {
52
                    $attributes = [
53
                        'name'  => $value,
54
                        'data'  => $value,
55
                        'title' => $this->getQualifiedTitle($value),
56
                    ];
57
                }
58
59
                $this->collection->push(new Column($attributes));
60
            } else {
61
                $this->collection->push($value);
62
            }
63
        }
64
65
        return $this;
66
    }
67
68
    /**
69
     * Set title attribute of an array if not set.
70
     *
71
     * @param string $title
72
     * @param array $attributes
73
     * @return array
74
     */
75
    public function setTitle($title, array $attributes)
76
    {
77
        if (! isset($attributes['title'])) {
78
            $attributes['title'] = $this->getQualifiedTitle($title);
79
        }
80
81
        return $attributes;
82
    }
83
84
    /**
85
     * Convert string into a readable title.
86
     *
87
     * @param string $title
88
     * @return string
89
     */
90
    public function getQualifiedTitle($title)
91
    {
92
        return Str::title(str_replace(['.', '_'], ' ', Str::snake($title)));
93
    }
94
95
    /**
96
     * Add a column in collection usingsl attributes.
97
     *
98
     * @param  array $attributes
99
     * @return $this
100
     */
101
    public function addColumn(array $attributes)
102
    {
103
        $this->collection->push(new Column($attributes));
104
105
        return $this;
106
    }
107
108
    /**
109
     * Add a Column object at the beginning of collection.
110
     *
111
     * @param \Yajra\DataTables\Html\Column $column
112
     * @return $this
113
     */
114
    public function addBefore(Column $column)
115
    {
116
        $this->collection->prepend($column);
117
118
        return $this;
119
    }
120
121
    /**
122
     * Add a column at the beginning of collection using attributes.
123
     *
124
     * @param  array $attributes
125
     * @return $this
126
     */
127
    public function addColumnBefore(array $attributes)
128
    {
129
        $this->collection->prepend(new Column($attributes));
130
131
        return $this;
132
    }
133
134
    /**
135
     * Add a Column object in collection.
136
     *
137
     * @param \Yajra\DataTables\Html\Column $column
138
     * @return $this
139
     */
140
    public function add(Column $column)
141
    {
142
        $this->collection->push($column);
143
144
        return $this;
145
    }
146
147
    /**
148
     * Get collection of columns.
149
     *
150
     * @return \Illuminate\Support\Collection
151
     */
152
    public function getColumns()
153
    {
154
        return $this->collection;
155
    }
156
157
    /**
158
     * Remove column by name.
159
     *
160
     * @param array $names
161
     * @return $this
162
     */
163
    public function removeColumn(...$names)
164
    {
165
        foreach ($names as $name) {
166
            $this->collection = $this->collection->filter(function (Column $column) use ($name) {
167
                return $column->name !== $name;
168
            })->flatten();
169
        }
170
171
        return $this;
172
    }
173
}
174