Completed
Push — master ( 4aa15f...354c04 )
by Arjay
02:26
created

CollectionEngine::totalCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 4
rs 10
nc 1
cc 1
eloc 2
nop 0
1
<?php
2
3
namespace Yajra\Datatables\Engines;
4
5
/**
6
 * Laravel Datatables Collection Engine
7
 *
8
 * @package  Laravel
9
 * @category Package
10
 * @author   Arjay Angeles <[email protected]>
11
 */
12
13
use Closure;
14
use Illuminate\Contracts\Support\Arrayable;
15
use Illuminate\Support\Arr;
16
use Illuminate\Support\Collection;
17
use Illuminate\Support\Str;
18
use Yajra\Datatables\Contracts\DataTableEngineContract;
19
use Yajra\Datatables\Request;
20
21
class CollectionEngine extends BaseEngine implements DataTableEngineContract
22
{
23
    /**
24
     * Collection object
25
     *
26
     * @var Collection
27
     */
28
    public $collection;
29
30
    /**
31
     * Collection object
32
     *
33
     * @var Collection
34
     */
35
    public $original_collection;
36
37
    /**
38
     * @param Collection $collection
39
     * @param \Yajra\Datatables\Request $request
40
     */
41
    public function __construct(Collection $collection, Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
42
    {
43
        $this->request             = $request;
44
        $this->collection          = $collection;
45
        $this->original_collection = $collection;
46
        $this->columns             = array_keys($this->serialize($collection->first()));
47
    }
48
49
    /**
50
     * Serialize collection
51
     *
52
     * @param  mixed $collection
53
     * @return mixed|null
54
     */
55
    protected function serialize($collection)
56
    {
57
        return $collection instanceof Arrayable ? $collection->toArray() : (array) $collection;
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function filter(Closure $callback)
64
    {
65
        $this->overrideGlobalSearch($callback, $this);
66
67
        return $this;
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function showDebugger(array $output)
74
    {
75
        $output["input"] = $this->request->all();
76
77
        return $output;
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function totalCount()
84
    {
85
        return $this->count();
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function count()
92
    {
93
        return $this->collection->count();
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    public function ordering()
100
    {
101
        foreach ($this->request->orderableColumns() as $orderable) {
102
            $column           = $this->getColumnName($orderable['column']);
103
            $this->collection = $this->collection->sortBy(
104
                function ($row) use ($column) {
105
                    $data = $this->serialize($row);
106
107
                    return Arr::get($data, $column);
108
                }
109
            );
110
111
            if ($orderable['direction'] == 'desc') {
112
                $this->collection = $this->collection->reverse();
113
            }
114
        }
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120
    public function filtering()
121
    {
122
        $columns          = $this->request['columns'];
123
        $this->collection = $this->collection->filter(
124
            function ($row) use ($columns) {
125
                $data                  = $this->serialize($row);
126
                $this->isFilterApplied = true;
127
                $found                 = [];
128
129
                $keyword = $this->request->keyword();
130
                foreach ($this->request->searchableColumnIndex() as $index) {
131
                    $column = $this->getColumnName($index);
132
                    if (! $value = Arr::get($data, $column)) {
133
                        continue;
134
                    }
135
136
                    if ($this->isCaseInsensitive()) {
137
                        $found[] = Str::contains(Str::lower($value), Str::lower($keyword));
138
                    } else {
139
                        $found[] = Str::contains($value, $keyword);
140
                    }
141
                }
142
143
                return in_array(true, $found);
144
            }
145
        );
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function columnSearch()
152
    {
153
        $columns = $this->request->get('columns');
154
        for ($i = 0, $c = count($columns); $i < $c; $i++) {
155
            if ($this->request->isColumnSearchable($i)) {
156
                $this->isFilterApplied = true;
157
158
                $column  = $this->getColumnName($i);
159
                $keyword = $this->request->columnKeyword($i);
160
161
                $this->collection = $this->collection->filter(
162
                    function ($row) use ($column, $keyword) {
163
                        $data = $this->serialize($row);
164
                        if ($this->isCaseInsensitive()) {
165
                            return strpos(Str::lower($data[$column]), Str::lower($keyword)) !== false;
166
                        } else {
167
                            return strpos($data[$column], $keyword) !== false;
168
                        }
169
                    }
170
                );
171
            }
172
        }
173
    }
174
175
    /**
176
     * @inheritdoc
177
     */
178
    public function paging()
179
    {
180
        $this->collection = $this->collection->slice(
181
            $this->request['start'],
182
            (int) $this->request['length'] > 0 ? $this->request['length'] : 10
183
        );
184
    }
185
186
    /**
187
     * @inheritdoc
188
     */
189
    public function results()
190
    {
191
        return $this->collection->all();
192
    }
193
194
    /**
195
     * @inheritdoc
196
     */
197
    public function make($mDataSupport = false, $orderFirst = true)
198
    {
199
        return parent::make($mDataSupport, $orderFirst);
200
    }
201
}
202