1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/mvc package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Mvc\Service\Entity; |
11
|
|
|
|
12
|
|
|
use Slick\Common\Utils\CollectionInterface; |
13
|
|
|
use Slick\Database\Sql\Select; |
14
|
|
|
use Slick\Mvc\Service\Entity\QueryFilter\QueryFilterCollection; |
15
|
|
|
use Slick\Mvc\Service\EntityServiceInterface; |
16
|
|
|
use Slick\Mvc\Utils\Pagination; |
17
|
|
|
use Slick\Orm\EntityInterface; |
18
|
|
|
use Slick\Orm\Orm; |
19
|
|
|
use Slick\Orm\Repository\QueryObject\QueryObjectInterface; |
20
|
|
|
use Slick\Orm\RepositoryInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Entity Listing Service |
24
|
|
|
* |
25
|
|
|
* @package Slick\Mvc\Service\Entity |
26
|
|
|
* @author Filipe Silva <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class EntityListingService extends AbstractEntityService implements |
29
|
|
|
EntityServiceInterface |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Pagination |
34
|
|
|
*/ |
35
|
|
|
private $pagination; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var QueryFilterInterface[]|QueryFilterCollectionInterface |
39
|
|
|
*/ |
40
|
|
|
private $filters; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var RepositoryInterface |
44
|
|
|
*/ |
45
|
|
|
private $repository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Entity Listing Service needs an entity or entity class name. |
49
|
|
|
* |
50
|
|
|
* @param string|EntityInterface $className |
51
|
|
|
*/ |
52
|
10 |
|
public function __construct($className) |
53
|
|
|
{ |
54
|
10 |
|
$method = 'setEntity'; |
55
|
10 |
|
if (!$className instanceof EntityInterface) { |
56
|
10 |
|
$method = 'setEntityClass'; |
57
|
5 |
|
} |
58
|
10 |
|
$this->$method($className); |
59
|
10 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get a paginated list of entities |
63
|
|
|
* |
64
|
|
|
* @return \Slick\Orm\Entity\EntityCollection |
65
|
|
|
*/ |
66
|
2 |
|
public function getList() |
67
|
|
|
{ |
68
|
|
|
/** @var Select|QueryObjectInterface $query */ |
69
|
2 |
|
$query = $this->getRepository()->find(); |
70
|
2 |
|
$this->getFilters()->apply($query); |
|
|
|
|
71
|
2 |
|
$this->getPagination()->setTotal($query->count()); |
72
|
2 |
|
$query->limit( |
|
|
|
|
73
|
2 |
|
$this->getPagination()->offset, |
74
|
2 |
|
$this->getPagination()->rowsPerPage |
75
|
1 |
|
); |
76
|
2 |
|
return $query->all(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Gets the pagination object |
81
|
|
|
* |
82
|
|
|
* @return Pagination |
83
|
|
|
*/ |
84
|
4 |
|
public function getPagination() |
85
|
|
|
{ |
86
|
4 |
|
if (null == $this->pagination) { |
87
|
4 |
|
$this->setPagination(new Pagination()); |
88
|
2 |
|
} |
89
|
4 |
|
return $this->pagination; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set pagination |
94
|
|
|
* |
95
|
|
|
* @param Pagination $pagination |
96
|
|
|
* |
97
|
|
|
* @return EntityListingService |
98
|
|
|
*/ |
99
|
4 |
|
public function setPagination($pagination) |
100
|
|
|
{ |
101
|
4 |
|
$this->pagination = $pagination; |
102
|
4 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get query filters collection |
107
|
|
|
* |
108
|
|
|
* @return CollectionInterface|QueryFilterCollectionInterface |
109
|
|
|
*/ |
110
|
4 |
|
public function getFilters() |
111
|
|
|
{ |
112
|
4 |
|
if (null == $this->filters) { |
113
|
4 |
|
$this->setFilters(new QueryFilterCollection()); |
114
|
2 |
|
} |
115
|
4 |
|
return $this->filters; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set filters collection |
120
|
|
|
* |
121
|
|
|
* @param QueryFilterCollectionInterface $filters |
122
|
|
|
* |
123
|
|
|
* @return EntityListingService |
124
|
|
|
*/ |
125
|
4 |
|
public function setFilters(QueryFilterCollectionInterface $filters) |
126
|
|
|
{ |
127
|
4 |
|
$this->filters = $filters; |
128
|
4 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get entity query |
133
|
|
|
* |
134
|
|
|
* @return RepositoryInterface |
135
|
|
|
*/ |
136
|
6 |
|
public function getRepository() |
137
|
|
|
{ |
138
|
6 |
|
if (null == $this->repository) { |
139
|
4 |
|
$this->setRepository( |
140
|
4 |
|
Orm::getRepository($this->getEntityClassName()) |
141
|
2 |
|
); |
142
|
2 |
|
} |
143
|
6 |
|
return $this->repository; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set entity repository |
148
|
|
|
* |
149
|
|
|
* @param RepositoryInterface $repository |
150
|
|
|
* |
151
|
|
|
* @return EntityListingService |
152
|
|
|
*/ |
153
|
6 |
|
public function setRepository(RepositoryInterface $repository) |
154
|
|
|
{ |
155
|
6 |
|
$this->repository = $repository; |
156
|
6 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
|
161
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.