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\Controller; |
11
|
|
|
|
12
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
13
|
|
|
use Slick\Filter\StaticFilter; |
14
|
|
|
use Slick\Http\PhpEnvironment\Request; |
15
|
|
|
use Slick\Mvc\Service\Entity\EntityListingService; |
16
|
|
|
use Slick\Mvc\Service\Entity\QueryFilter\SearchFilter; |
17
|
|
|
use Slick\Mvc\Utils\Pagination; |
18
|
|
|
use Slick\Orm\Repository\EntityRepository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Entity Listing Methods |
22
|
|
|
* |
23
|
|
|
* @package Slick\Mvc\Controller |
24
|
|
|
* @author Filipe Silva <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
trait EntityListingMethods |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $rowsPerPage = 12; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Pagination |
36
|
|
|
*/ |
37
|
|
|
protected $pagination; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var EntityListingService |
41
|
|
|
*/ |
42
|
|
|
protected $listingService; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string[] |
46
|
|
|
*/ |
47
|
|
|
protected $searchFields; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Handle the request to display a list of entities |
51
|
|
|
*/ |
52
|
2 |
|
public function index() |
53
|
|
|
{ |
54
|
2 |
|
$this->getListingService() |
55
|
2 |
|
->setOrder($this->getOrder()) |
56
|
2 |
|
->setPagination($this->getPagination()) |
57
|
2 |
|
->getFilters()->add($this->getSearchFilter()); |
58
|
2 |
|
$this->set( |
|
|
|
|
59
|
|
|
[ |
60
|
2 |
|
$this->getEntityNamePlural() => $this->getListingService() |
61
|
2 |
|
->getList(), |
62
|
2 |
|
'pagination' => $this->getListingService()->getPagination() |
63
|
1 |
|
] |
64
|
1 |
|
); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get pagination for roes per page property |
69
|
|
|
* |
70
|
|
|
* @return Pagination |
71
|
|
|
*/ |
72
|
4 |
|
protected function getPagination() |
73
|
|
|
{ |
74
|
4 |
|
if (null == $this->pagination) { |
75
|
4 |
|
$this->pagination = new Pagination( |
76
|
|
|
[ |
77
|
4 |
|
'rowsPerPage' => $this->rowsPerPage, |
78
|
4 |
|
'request' => $this->getRequest() |
79
|
2 |
|
] |
80
|
2 |
|
); |
81
|
2 |
|
} |
82
|
4 |
|
return $this->pagination; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the entity listing service |
87
|
|
|
* |
88
|
|
|
* @return EntityListingService |
89
|
|
|
*/ |
90
|
4 |
|
protected function getListingService() |
91
|
|
|
{ |
92
|
4 |
|
if (null == $this->listingService) { |
93
|
2 |
|
$this->listingService = new EntityListingService( |
94
|
2 |
|
$this->getEntityClassName() |
|
|
|
|
95
|
1 |
|
); |
96
|
1 |
|
} |
97
|
4 |
|
return $this->listingService; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get search filter |
102
|
|
|
* |
103
|
|
|
* @return SearchFilter |
104
|
|
|
*/ |
105
|
4 |
|
protected function getSearchFilter() |
106
|
|
|
{ |
107
|
4 |
|
$pattern = $this->getRequest()->getQuery('pattern', null); |
|
|
|
|
108
|
4 |
|
$pattern = StaticFilter::filter('text', $pattern); |
109
|
4 |
|
$this->set('pattern', $pattern); |
|
|
|
|
110
|
|
|
|
111
|
4 |
|
return new SearchFilter(['pattern' => $pattern]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the fields list to use on search filter |
116
|
|
|
* |
117
|
|
|
* @return array|\string[] |
118
|
|
|
*/ |
119
|
2 |
|
protected function getSearchFields() |
120
|
|
|
{ |
121
|
2 |
|
if (null == $this->searchFields) { |
122
|
2 |
|
$field = $this->getEntityDescriptor()->getDisplayFiled(); |
123
|
2 |
|
$this->searchFields = [ |
124
|
2 |
|
$this->getEntityDescriptor() |
125
|
2 |
|
->getTableName().'.'.$field->getField() |
126
|
1 |
|
]; |
127
|
1 |
|
} |
128
|
2 |
|
return $this->searchFields; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the query order by clause |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
2 |
|
protected function getOrder() |
137
|
|
|
{ |
138
|
|
|
/** @var EntityRepository $repo */ |
139
|
2 |
|
$repo = $this->getRepository(); |
|
|
|
|
140
|
2 |
|
$table = $repo->getEntityDescriptor()->getTableName(); |
141
|
2 |
|
$pmk = $repo->getEntityDescriptor()->getPrimaryKey()->getField(); |
142
|
2 |
|
return "{$table}.{$pmk} DESC"; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Gets updated HTTP request |
147
|
|
|
* |
148
|
|
|
* @return ServerRequestInterface|Request |
149
|
|
|
*/ |
150
|
|
|
abstract public function getRequest(); |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the current entity descriptor |
154
|
|
|
* |
155
|
|
|
* @return \Slick\Orm\Descriptor\EntityDescriptorInterface |
156
|
|
|
*/ |
157
|
|
|
abstract protected function getEntityDescriptor(); |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the plural name of the entity |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
abstract protected function getEntityNamePlural(); |
165
|
|
|
} |
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.