1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 19.03.16 at 16:02 |
5
|
|
|
*/ |
6
|
|
|
namespace samsoncms\api; |
7
|
|
|
|
8
|
|
|
use samsoncms\api\exception\RenderableViewNotSet; |
9
|
|
|
use samsonframework\core\ViewInterface; |
10
|
|
|
use samsonframework\view\View; |
11
|
|
|
|
12
|
|
|
define('RENDERABLE_ITEMS_VARIABLE', 'items'); |
13
|
|
|
define('RENDERABLE_ITEM_VARIABLE', 'item'); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Generic renderable methods and variables. |
17
|
|
|
* |
18
|
|
|
* @package samsoncms\api |
19
|
|
|
*/ |
20
|
|
|
trait Renderable |
21
|
|
|
{ |
22
|
|
|
/** @var string|callable Block view file or callback */ |
23
|
|
|
protected $indexView; |
24
|
|
|
|
25
|
|
|
/** @var string|callable Item view file or callback */ |
26
|
|
|
protected $itemView; |
27
|
|
|
|
28
|
|
|
/** @var string|callable Empty view file or callback */ |
29
|
|
|
protected $emptyView; |
30
|
|
|
|
31
|
|
|
/** @var ViewInterface View render object */ |
32
|
|
|
protected $renderer; |
33
|
|
|
|
34
|
|
|
/** @var int Count of entities on one page */ |
35
|
|
|
protected $pageSize; |
36
|
|
|
|
37
|
|
|
/** @var int Current page number */ |
38
|
|
|
protected $pageNumber; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set pagination for SamsonCMS query. |
42
|
|
|
* |
43
|
|
|
* @param int $pageNumber Result page number |
44
|
|
|
* @param int|null $pageSize Results page size |
45
|
|
|
* |
46
|
|
|
* @return $this Chaining |
47
|
|
|
*/ |
48
|
|
|
public function pager($pageNumber, $pageSize = null) |
49
|
|
|
{ |
50
|
|
|
$this->pageNumber = $pageNumber; |
51
|
|
|
$this->pageSize = null !== $pageSize ? $pageSize : $this->pageSize; |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set index view path. |
58
|
|
|
* |
59
|
|
|
* @param string|callable|View $indexView Index view path or callback |
60
|
|
|
* |
61
|
|
|
* @return $this Chaining |
62
|
|
|
*/ |
63
|
|
|
public function indexView($indexView) |
64
|
|
|
{ |
65
|
|
|
$this->indexView = $indexView; |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set item view path. |
71
|
|
|
* |
72
|
|
|
* @param string|callable|View $itemView Item view path or callback |
73
|
|
|
* |
74
|
|
|
* @return $this Chaining |
75
|
|
|
*/ |
76
|
|
|
public function itemView($itemView) |
77
|
|
|
{ |
78
|
|
|
$this->itemView = $itemView; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set empty view path. |
85
|
|
|
* |
86
|
|
|
* @param string|callable|View $emptyView Empty view path or callback |
87
|
|
|
* |
88
|
|
|
* @return $this Chaining |
89
|
|
|
*/ |
90
|
|
|
public function emptyView($emptyView) |
91
|
|
|
{ |
92
|
|
|
$this->emptyView = $emptyView; |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Render Entity collection item. |
98
|
|
|
* |
99
|
|
|
* @param mixed $item SamsonCMS entity for rendering |
100
|
|
|
* |
101
|
|
|
* @return string Rendered HTML |
102
|
|
|
*/ |
103
|
|
|
public function renderItem($item) |
104
|
|
|
{ |
105
|
|
|
// Set correct renderer old style or new \samsonframework\view |
106
|
|
|
$renderer = ($this->itemView instanceof View) |
107
|
|
|
? $this->itemView |
108
|
|
|
: $this->renderer->view($this->itemView); |
109
|
|
|
|
110
|
|
|
return $renderer |
111
|
|
|
->set($item, RENDERABLE_ITEM_VARIABLE) |
112
|
|
|
->output(); |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Render empty collection item. |
118
|
|
|
* |
119
|
|
|
* @return string Rendered HTML |
120
|
|
|
*/ |
121
|
|
|
public function renderEmpty() |
122
|
|
|
{ |
123
|
|
|
// Set correct renderer old style or new \samsonframework\view |
124
|
|
|
$renderer = ($this->emptyView instanceof View) |
125
|
|
|
? $this->emptyView |
126
|
|
|
: $this->renderer->view($this->emptyView); |
127
|
|
|
|
128
|
|
|
return $renderer->output(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Render Entity collection index. |
133
|
|
|
* |
134
|
|
|
* @param string $items Collection of rendered items |
135
|
|
|
* |
136
|
|
|
* @return string Rendered HTML |
137
|
|
|
*/ |
138
|
|
|
public function renderIndex($items) |
139
|
|
|
{ |
140
|
|
|
// Set correct renderer old style or new \samsonframework\view |
141
|
|
|
$renderer = ($this->indexView instanceof View) |
142
|
|
|
? $this->indexView |
143
|
|
|
: $this->renderer->view($this->indexView); |
144
|
|
|
|
145
|
|
|
return $renderer |
146
|
|
|
->set($items, RENDERABLE_ITEMS_VARIABLE) |
147
|
|
|
->output(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** @return string Rendered fields table */ |
151
|
|
|
public function __toString() |
152
|
|
|
{ |
153
|
|
|
return $this->output(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** @return string Rendered HTML for fields table */ |
157
|
|
|
public function output() |
158
|
|
|
{ |
159
|
|
|
// Validate renderable views |
160
|
|
|
if ($this->indexView === null) { |
161
|
|
|
throw new RenderableViewNotSet('indexView'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ($this->itemView === null) { |
165
|
|
|
throw new RenderableViewNotSet('itemView'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($this->emptyView === null) { |
169
|
|
|
throw new RenderableViewNotSet('emptyView'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// Perform SamsonCMS query |
173
|
|
|
$collection = $this->find($this->pageNumber, $this->pageSize); |
|
|
|
|
174
|
|
|
|
175
|
|
|
if (count($collection)) { |
176
|
|
|
return $this->renderer($collection); |
177
|
|
|
} else { // Render empty entity view |
178
|
|
|
return $this->innerRender('', $collection, 'emptyView', 'renderEmpty'); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Collection items renderer. |
184
|
|
|
* |
185
|
|
|
* @param mixed $collection Items collection for rendering |
186
|
|
|
* @return string Rendered items |
187
|
|
|
*/ |
188
|
|
|
protected function renderer($collection) |
189
|
|
|
{ |
190
|
|
|
// Render each entity view in collection |
191
|
|
|
$html = ''; |
192
|
|
|
foreach ($collection as $row) { |
193
|
|
|
$html .= $this->innerRender($row, $collection, 'itemView', 'renderItem'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// Render collection main view with items |
197
|
|
|
return $this->innerRender($html, $collection, 'indexView', 'renderIndex'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Generic view renderer. |
202
|
|
|
* |
203
|
|
|
* @param mixed $item |
204
|
|
|
* @param Entity[] $collection |
205
|
|
|
* @param string $variableName |
206
|
|
|
* @param string $methodName |
207
|
|
|
* |
208
|
|
|
* @return mixed Rendered view |
209
|
|
|
*/ |
210
|
|
|
protected function innerRender($item, $collection, $variableName, $methodName) |
211
|
|
|
{ |
212
|
|
|
return is_callable($this->$variableName) |
213
|
|
|
? call_user_func($this->$variableName, $item, $this->renderer, $this->query, $collection) |
|
|
|
|
214
|
|
|
: $this->$methodName($item); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
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.