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