1
|
|
|
<?php |
2
|
|
|
namespace Solvire\API\Renderers; |
3
|
|
|
|
4
|
|
|
use Solvire\API\Serializers\BaseSerializer; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Like the view. |
8
|
|
|
* Just outputs what the serializer is going to give back to it |
9
|
|
|
* must implement the get() funtion |
10
|
|
|
* |
11
|
|
|
* @author solvire <[email protected]> |
12
|
|
|
* @package Renderers |
13
|
|
|
* @namespace Solvire\API\Renderers |
14
|
|
|
*/ |
15
|
|
|
abstract class ListRenderer extends GenericRenderer implements Getable |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
protected $resultSet = null; |
19
|
|
|
|
20
|
|
|
protected $resultArray = []; |
21
|
|
|
|
22
|
|
|
protected $paginationLimit = null; |
23
|
|
|
|
24
|
1 |
|
public function nextPageUrl() |
25
|
|
|
{ |
26
|
1 |
|
return $this->resultSet->nextPageUrl(); |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
public function previousPageUrl() |
30
|
|
|
{ |
31
|
1 |
|
return $this->resultSet->previousPageUrl(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function firstItem() |
35
|
|
|
{ |
36
|
|
|
return $this->resultSet->firstItem(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function lastItem() |
40
|
|
|
{ |
41
|
|
|
return $this->resultSet->lastItem(); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function lastPage() |
45
|
|
|
{ |
46
|
1 |
|
return $this->resultSet->lastPage(); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
public function perPage() |
50
|
|
|
{ |
51
|
|
|
return $this->resultSet->perPage(); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
1 |
|
public function currentPage() |
55
|
|
|
{ |
56
|
1 |
|
return $this->resultSet->currentPage(); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function hasPages() |
60
|
|
|
{ |
61
|
1 |
|
return $this->resultSet->hasPages(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function hasMorePages() |
65
|
|
|
{ |
66
|
|
|
return $this->resultSet->hasMorePages(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function isEmpty() |
70
|
|
|
{ |
71
|
|
|
return $this->resultSet->isEmpty(); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
public function total() |
75
|
|
|
{ |
76
|
1 |
|
return $this->resultSet->total(); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
public function setPaginationLimit($paginationLimit) |
80
|
|
|
{ |
81
|
1 |
|
$this->paginationLimit = $paginationLimit; |
82
|
1 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function getPaginationLimit() |
86
|
|
|
{ |
87
|
1 |
|
if($this->paginationLimit === null || $this->paginationLimit < 1) |
88
|
1 |
|
throw new \RuntimeException("The page limit is not set"); |
89
|
1 |
|
return $this->paginationLimit; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* |
94
|
|
|
* @param |
95
|
|
|
* array transformable object |
96
|
|
|
*/ |
97
|
1 |
|
public function paginate($resultSet) |
98
|
|
|
{ |
99
|
1 |
|
$this->resultSet = $resultSet; |
100
|
|
|
// TODO figure out how to check to see if we can paginate this |
101
|
|
|
// something about transformable |
102
|
1 |
|
$retarr = []; |
103
|
|
|
|
104
|
|
|
// we know that the set can be iterated over |
105
|
|
|
// and that each item in the record set should match up to the serializer object |
106
|
1 |
|
foreach ($resultSet as $key => $record) { |
107
|
1 |
|
$item = $this->serializer->loadData($record); |
108
|
1 |
|
$retarr[] = $item->toArray(); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
1 |
|
$this->resultArray = $retarr; |
112
|
|
|
|
113
|
|
|
return [ |
114
|
|
|
'items' => $retarr |
115
|
1 |
|
]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* |
120
|
|
|
* @see https://google-styleguide.googlecode.com/svn/trunk/jsoncstyleguide.xml#Reserved_Property_Names_for_Paging |
121
|
|
|
*/ |
122
|
1 |
|
public function generateLinks() |
123
|
|
|
{ |
124
|
1 |
|
$retarr = []; |
125
|
|
|
|
126
|
1 |
|
$retarr['currentItemCount'] = count($this->resultArray); |
127
|
|
|
|
128
|
1 |
|
$retarr['itemsPerPage'] = $this->getPaginationLimit(); |
129
|
|
|
|
130
|
|
|
// TODO get the ID of the starting element |
131
|
|
|
// $retarr['startIndex'] = $this->resultArray[0]; |
|
|
|
|
132
|
|
|
|
133
|
1 |
|
$retarr['totalItems'] = $this->total(); |
134
|
|
|
|
135
|
|
|
// TODO fix the paging template for links |
136
|
|
|
// $retarr['pagingLinkTemplate'] = $this->resultSet |
|
|
|
|
137
|
|
|
|
138
|
|
|
// 1 based |
139
|
1 |
|
$retarr['pageIndex'] = $this->currentPage(); |
140
|
|
|
|
141
|
1 |
|
$retarr['totalPages'] = $this->lastPage(); |
142
|
|
|
|
143
|
1 |
|
if ($this->hasPages()) { |
144
|
|
|
// TODO need to set up the current page link |
145
|
|
|
// $retarr['selfLink'] |
146
|
1 |
|
$retarr['nextLink'] = $this->nextPageUrl(); |
147
|
1 |
|
$retarr['previousLink'] = $this->previousPageUrl(); |
148
|
1 |
|
} |
149
|
|
|
|
150
|
1 |
|
return $retarr; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.