|
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
|
|
|
* @param array $elements |
|
50
|
|
|
* @param int $totalItems |
|
51
|
|
|
* @param int $currentPage |
|
52
|
|
|
* @param int $itemsPerPage |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(array $elements, $totalItems, $currentPage, $itemsPerPage) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->elements = $elements; |
|
57
|
|
|
$this->totalItems = (int) $totalItems; |
|
58
|
|
|
$this->currentPage = (int) $currentPage; |
|
59
|
|
|
$this->itemsPerPage = (int) $itemsPerPage; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param array $data |
|
64
|
|
|
* @return static |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function fromData(array $data) |
|
67
|
|
|
{ |
|
68
|
|
|
$elements = []; |
|
69
|
|
|
foreach ($data['items'] as $key => $item) { |
|
70
|
|
|
$elements[$key] = static::createElementFromData($item); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return new static( |
|
74
|
|
|
$elements, |
|
75
|
|
|
$data['collection']['total_items'], |
|
76
|
|
|
$data['collection']['page'], |
|
77
|
|
|
$data['collection']['page_size'] |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Load the element in the collection based on the data given |
|
83
|
|
|
* |
|
84
|
|
|
* @param array $item |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
protected static function createElementFromData(array $item) |
|
88
|
|
|
{ |
|
89
|
|
|
throw new LogicException(sprintf( |
|
90
|
|
|
'The method "%s::createElementFromData must be implemented to load the Collection Element"' |
|
91
|
|
|
)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return int |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getCurrentPage() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->currentPage; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getElements() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->elements; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return mixed|null |
|
112
|
|
|
* @throws LogicException When there is more than 1 element present. |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getOnlyElement() |
|
115
|
|
|
{ |
|
116
|
|
|
$elementCount = count($this->elements); |
|
117
|
|
|
|
|
118
|
|
|
if ($elementCount === 1) { |
|
119
|
|
|
return reset($this->elements); |
|
120
|
|
|
} elseif ($elementCount === 0) { |
|
121
|
|
|
return null; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
throw new LogicException(sprintf('There are %d elements in this collection instead of one.', $elementCount)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return int |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getItemsPerPage() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->itemsPerPage; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return int |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getTotalItems() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->totalItems; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|