1: <?php
2:
3: namespace Yajra\Datatables\Engines;
4:
5: use Closure;
6: use Illuminate\Contracts\Support\Arrayable;
7: use Illuminate\Support\Arr;
8: use Illuminate\Support\Collection;
9: use Illuminate\Support\Str;
10: use Yajra\Datatables\Request;
11:
12: 13: 14: 15: 16: 17:
18: class CollectionEngine extends BaseEngine
19: {
20: 21: 22: 23: 24:
25: public $collection;
26:
27: 28: 29: 30: 31:
32: public $original_collection;
33:
34: 35: 36: 37: 38: 39:
40: public function __construct(Collection $collection, Request $request)
41: {
42: $this->request = $request;
43: $this->collection = $collection;
44: $this->original_collection = $collection;
45: $this->columns = array_keys($this->serialize($collection->first()));
46: }
47:
48: 49: 50: 51: 52: 53:
54: protected function serialize($collection)
55: {
56: return $collection instanceof Arrayable ? $collection->toArray() : (array) $collection;
57: }
58:
59: 60: 61: 62: 63: 64: 65: 66:
67: public function filter(Closure $callback, $globalSearch = false)
68: {
69: $this->overrideGlobalSearch($callback, $this, $globalSearch);
70:
71: return $this;
72: }
73:
74: 75: 76: 77: 78: 79:
80: public function showDebugger(array $output)
81: {
82: $output["input"] = $this->request->all();
83:
84: return $output;
85: }
86:
87: 88: 89: 90: 91:
92: public function totalCount()
93: {
94: return $this->totalRecords ? $this->totalRecords : $this->collection->count();
95: }
96:
97: 98: 99: 100: 101:
102: public function count()
103: {
104: return $this->collection->count() > $this->totalRecords ? $this->totalRecords : $this->collection->count();
105: }
106:
107: 108: 109: 110: 111:
112: public function ordering()
113: {
114: if ($this->orderCallback) {
115: call_user_func($this->orderCallback, $this);
116:
117: return;
118: }
119:
120: foreach ($this->request->orderableColumns() as $orderable) {
121: $column = $this->getColumnName($orderable['column']);
122: $this->collection = $this->collection->sortBy(
123: function ($row) use ($column) {
124: $data = $this->serialize($row);
125:
126: return Arr::get($data, $column);
127: }
128: );
129:
130: if ($orderable['direction'] == 'desc') {
131: $this->collection = $this->collection->reverse();
132: }
133: }
134: }
135:
136: 137: 138: 139: 140:
141: public function filtering()
142: {
143: $columns = $this->request['columns'];
144: $this->collection = $this->collection->filter(
145: function ($row) use ($columns) {
146: $data = $this->serialize($row);
147: $this->isFilterApplied = true;
148: $found = [];
149:
150: $keyword = $this->request->keyword();
151: foreach ($this->request->searchableColumnIndex() as $index) {
152: $column = $this->getColumnName($index);
153: if (! $value = Arr::get($data, $column)) {
154: continue;
155: }
156:
157: if ($this->isCaseInsensitive()) {
158: $found[] = Str::contains(Str::lower($value), Str::lower($keyword));
159: } else {
160: $found[] = Str::contains($value, $keyword);
161: }
162: }
163:
164: return in_array(true, $found);
165: }
166: );
167: }
168:
169: 170: 171: 172: 173:
174: public function columnSearch()
175: {
176: $columns = $this->request->get('columns');
177: for ($i = 0, $c = count($columns); $i < $c; $i++) {
178: if ($this->request->isColumnSearchable($i)) {
179: $this->isFilterApplied = true;
180: $regex = $this->request->isRegex($i);
181:
182: $column = $this->getColumnName($i);
183: $keyword = $this->request->columnKeyword($i);
184:
185: $this->collection = $this->collection->filter(
186: function ($row) use ($column, $keyword, $regex) {
187: $data = $this->serialize($row);
188:
189: $value = Arr::get($data, $column);
190:
191: if ($this->isCaseInsensitive()) {
192: if ($regex) {
193: return preg_match('/' . $keyword . '/i', $value) == 1;
194: } else {
195: return strpos(Str::lower($value), Str::lower($keyword)) !== false;
196: }
197: } else {
198: if ($regex) {
199: return preg_match('/' . $keyword . '/', $value) == 1;
200: } else {
201: return strpos($value, $keyword) !== false;
202: }
203: }
204: }
205: );
206: }
207: }
208: }
209:
210: 211: 212: 213: 214:
215: public function paging()
216: {
217: $this->collection = $this->collection->slice(
218: $this->request['start'],
219: (int)$this->request['length'] > 0 ? $this->request['length'] : 10
220: );
221: }
222:
223: 224: 225: 226: 227:
228: public function results()
229: {
230: return $this->collection->all();
231: }
232:
233: 234: 235: 236: 237: 238: 239:
240: public function make($mDataSupport = false, $orderFirst = true)
241: {
242: return parent::make($mDataSupport, $orderFirst);
243: }
244: }
245: