1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupMiddlewareClientBundle\Dto; |
20
|
|
|
|
21
|
|
|
use LogicException; |
22
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
23
|
|
|
|
24
|
|
|
abstract class CollectionDto implements Dto |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @Assert\Valid |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $elements; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
protected $totalItems; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $currentPage; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
protected $itemsPerPage; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
private $filterOptions; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array $elements |
55
|
|
|
* @param int $totalItems |
56
|
|
|
* @param int $currentPage |
57
|
|
|
* @param int $itemsPerPage |
58
|
|
|
* @param array $filterOptions |
59
|
|
|
*/ |
60
|
|
|
public function __construct(array $elements, $totalItems, $currentPage, $itemsPerPage, $filterOptions = array()) |
61
|
|
|
{ |
62
|
|
|
$this->elements = $elements; |
63
|
|
|
$this->totalItems = (int) $totalItems; |
64
|
|
|
$this->currentPage = (int) $currentPage; |
65
|
|
|
$this->itemsPerPage = (int) $itemsPerPage; |
66
|
|
|
$this->filterOptions = $filterOptions; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $data |
71
|
|
|
* @return static |
72
|
|
|
*/ |
73
|
|
|
public static function fromData(array $data) |
74
|
|
|
{ |
75
|
|
|
$elements = []; |
76
|
|
|
foreach ($data['items'] as $key => $item) { |
77
|
|
|
$elements[$key] = static::createElementFromData($item); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return new static( |
81
|
|
|
$elements, |
82
|
|
|
$data['collection']['total_items'], |
83
|
|
|
$data['collection']['page'], |
84
|
|
|
$data['collection']['page_size'], |
85
|
|
|
$data['filters'] |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Load the element in the collection based on the data given |
91
|
|
|
* |
92
|
|
|
* @param array $item |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
protected static function createElementFromData(array $item) |
96
|
|
|
{ |
97
|
|
|
throw new LogicException(sprintf( |
98
|
|
|
'The method "%s::createElementFromData must be implemented to load the Collection Element"' |
99
|
|
|
)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
public function getCurrentPage() |
106
|
|
|
{ |
107
|
|
|
return $this->currentPage; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function getElements() |
114
|
|
|
{ |
115
|
|
|
return $this->elements; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $key |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function getFilterOption($key) |
123
|
|
|
{ |
124
|
|
|
if (!array_key_exists($key, $this->filterOptions)) { |
125
|
|
|
return []; |
126
|
|
|
} |
127
|
|
|
return $this->filterOptions[$key]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return mixed|null |
132
|
|
|
* @throws LogicException When there is more than 1 element present. |
133
|
|
|
*/ |
134
|
|
|
public function getOnlyElement() |
135
|
|
|
{ |
136
|
|
|
$elementCount = count($this->elements); |
137
|
|
|
|
138
|
|
|
if ($elementCount === 1) { |
139
|
|
|
return reset($this->elements); |
140
|
|
|
} elseif ($elementCount === 0) { |
141
|
|
|
return null; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
throw new LogicException(sprintf('There are %d elements in this collection instead of one.', $elementCount)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return int |
149
|
|
|
*/ |
150
|
|
|
public function getItemsPerPage() |
151
|
|
|
{ |
152
|
|
|
return $this->itemsPerPage; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return int |
157
|
|
|
*/ |
158
|
|
|
public function getTotalItems() |
159
|
|
|
{ |
160
|
|
|
return $this->totalItems; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|