|
1
|
|
|
<?php |
|
2
|
|
|
namespace Nayjest\Grids; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
5
|
|
|
use Event; |
|
6
|
|
|
use Illuminate\Foundation\Application; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
|
|
9
|
|
|
class CollectionDataProvider extends DataProvider |
|
10
|
|
|
{ |
|
11
|
|
|
protected $collection; |
|
12
|
|
|
|
|
13
|
|
|
protected $paginator; |
|
14
|
|
|
|
|
15
|
|
|
/** @var $iterator \ArrayIterator */ |
|
16
|
|
|
protected $iterator; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param Builder $src |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(Collection $src) |
|
24
|
|
|
{ |
|
25
|
|
|
parent::__construct($src); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function reset() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->getIterator()->rewind(); |
|
34
|
|
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getCollection() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->src; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getPaginator() |
|
46
|
|
|
{ |
|
47
|
|
|
if (!$this->paginator) { |
|
48
|
|
|
$this->paginator = new \Illuminate\Pagination\Paginator($this->src, $this->src->count()); |
|
49
|
|
|
} |
|
50
|
|
|
return $this->paginator; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return \Illuminate\Pagination\Factory |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getPaginationFactory() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->getPaginator();//$this->src->getQuery()->getConnection()->getPaginator(); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function getIterator() |
|
62
|
|
|
{ |
|
63
|
|
|
if (!$this->iterator) { |
|
64
|
|
|
$this->iterator = $this->getCollection()->getIterator(); |
|
65
|
|
|
} |
|
66
|
|
|
return $this->iterator; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return Builder |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getBuilder() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->src; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
View Code Duplication |
public function getRow() |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
if(!$this->iterator) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->getIterator(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($this->index < $this->count()) { |
|
85
|
|
|
$this->index++; |
|
86
|
|
|
$item = $this->iterator->current(); |
|
87
|
|
|
$this->iterator->next(); |
|
88
|
|
|
$row = new EloquentDataRow($item, $this->getRowId()); |
|
|
|
|
|
|
89
|
|
|
Event::fire(self::EVENT_FETCH_ROW, [$row, $this]); |
|
90
|
|
|
return $row; |
|
91
|
|
|
} else { |
|
92
|
|
|
return null; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
|
|
public function count() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->getCollection()->count(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
|
|
public function orderBy($fieldName, $direction) |
|
108
|
|
|
{ |
|
109
|
|
|
if($direction === Grid::SORT_ASC){ |
|
110
|
|
|
$this->src->sortBy($fieldName); |
|
111
|
|
|
} |
|
112
|
|
|
else{ |
|
113
|
|
|
$this->src->sortByDesc($fieldName); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritdoc} |
|
121
|
|
|
*/ |
|
122
|
|
|
public function filter($fieldName, $operator, $value) |
|
123
|
|
|
{ |
|
124
|
|
|
/* |
|
125
|
|
|
Currently no filter support on CollectionDataProvider |
|
126
|
|
|
*/ |
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.