Completed
Push — master ( 1c3c2e...248014 )
by Arjay
10:57
created

HasColumns::columns()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace Yajra\DataTables\Html\Options;
4
5
use Illuminate\Support\Collection;
6
use Yajra\DataTables\Html\Column;
7
8
/**
9
 * DataTables - Columns option builder.
10
 *
11
 * @see https://datatables.net/reference/option/
12
 */
13
trait HasColumns
14
{
15
    /**
16
     * Set columnDefs option value.
17
     *
18
     * @param array $value
19
     * @return $this
20
     * @see https://datatables.net/reference/option/columnDefs
21
     */
22
    public function columnDefs(array $value)
23
    {
24
        $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...
25
26
        return $this;
27
    }
28
29
    /**
30
     * Set columns option value.
31
     *
32
     * @param array $columns
33
     * @return $this
34
     * @see https://datatables.net/reference/option/columns
35
     */
36
    public function columns(array $columns)
37
    {
38
        $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...
39
40
        foreach ($columns as $key => $value) {
41
            if (! is_a($value, Column::class)) {
42
                if (is_array($value)) {
43
                    $attributes = array_merge(
44
                        [
45
                            'name' => $value['name'] ?? $value['data'] ?? $key,
46
                            'data' => $value['data'] ?? $key,
47
                        ],
48
                        $this->setTitle($key, $value)
0 ignored issues
show
Bug introduced by
It seems like setTitle() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
49
                    );
50
                } else {
51
                    $attributes = [
52
                        'name' => $value,
53
                        'data' => $value,
54
                        'title' => $this->getQualifiedTitle($value),
0 ignored issues
show
Bug introduced by
It seems like getQualifiedTitle() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
55
                    ];
56
                }
57
58
                $this->collection->push(new Column($attributes));
59
            } else {
60
                $this->collection->push($value);
61
            }
62
        }
63
64
        return $this;
65
    }
66
67
    /**
68
     * Add a column in collection usingsl attributes.
69
     *
70
     * @param  array $attributes
71
     * @return $this
72
     */
73
    public function addColumn(array $attributes)
74
    {
75
        $this->collection->push(new Column($attributes));
76
77
        return $this;
78
    }
79
80
    /**
81
     * Add a Column object at the beginning of collection.
82
     *
83
     * @param \Yajra\DataTables\Html\Column $column
84
     * @return $this
85
     */
86
    public function addBefore(Column $column)
87
    {
88
        $this->collection->prepend($column);
89
90
        return $this;
91
    }
92
93
    /**
94
     * Add a column at the beginning of collection using attributes.
95
     *
96
     * @param  array $attributes
97
     * @return $this
98
     */
99
    public function addColumnBefore(array $attributes)
100
    {
101
        $this->collection->prepend(new Column($attributes));
102
103
        return $this;
104
    }
105
106
    /**
107
     * Add a Column object in collection.
108
     *
109
     * @param \Yajra\DataTables\Html\Column $column
110
     * @return $this
111
     */
112
    public function add(Column $column)
113
    {
114
        $this->collection->push($column);
115
116
        return $this;
117
    }
118
119
    /**
120
     * Get collection of columns.
121
     *
122
     * @return \Illuminate\Support\Collection
123
     */
124
    public function getColumns()
125
    {
126
        return $this->collection;
127
    }
128
129
    /**
130
     * Remove column by name.
131
     *
132
     * @param array $names
133
     * @return $this
134
     */
135
    public function removeColumn(...$names)
136
    {
137
        foreach ($names as $name) {
138
            $this->collection = $this->collection->filter(function (Column $column) use ($name) {
139
                return $column->name !== $name;
140
            })->flatten();
141
        }
142
143
        return $this;
144
    }
145
}
146