|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CL\FluidGallery; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayObject; |
|
6
|
|
|
use Closure; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @author Ivan Kerin <[email protected]> |
|
10
|
|
|
* @copyright 2015, Clippings Ltd. |
|
11
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
|
12
|
|
|
*/ |
|
13
|
|
|
class ItemGroup extends ArrayObject { |
|
14
|
|
|
|
|
15
|
|
|
private $margin; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param Item[] $items |
|
19
|
|
|
* @param float $margin |
|
20
|
|
|
*/ |
|
21
|
1 |
|
public function __construct(array $items, $margin = 0) |
|
22
|
|
|
{ |
|
23
|
1 |
|
foreach ($items as $item) { |
|
24
|
1 |
|
$this->add($item); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
$this->margin = $margin; |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get the margin |
|
32
|
|
|
* @return float |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function getMargin() |
|
35
|
|
|
{ |
|
36
|
1 |
|
return $this->margin; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get the margin as a percentage of total |
|
41
|
|
|
* @param float $total |
|
42
|
|
|
* @return float |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function getMarginPercent($total) |
|
45
|
|
|
{ |
|
46
|
1 |
|
return ($this->margin / $total) * 100; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Change the margin |
|
51
|
|
|
* @param float $margin |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function setMargin($margin) |
|
54
|
|
|
{ |
|
55
|
1 |
|
$this->margin = $margin; |
|
56
|
|
|
|
|
57
|
1 |
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Add item |
|
62
|
|
|
* @param Item $item |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function add(Item $item) |
|
65
|
|
|
{ |
|
66
|
1 |
|
$this->append($item); |
|
67
|
1 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Extract an ItemGroup, removing all the items from the parent ItemGroup |
|
71
|
|
|
* @param Closure $extract |
|
72
|
|
|
* @return ItemGroup |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function extract(Closure $extract) |
|
75
|
|
|
{ |
|
76
|
1 |
|
$array = $this->getArrayCopy(); |
|
77
|
|
|
|
|
78
|
1 |
|
$extracted = $extract(new ItemGroup($array, $this->margin)); |
|
79
|
|
|
|
|
80
|
1 |
|
$this->exchangeArray(array_diff($array, $extracted->getArrayCopy())); |
|
81
|
|
|
|
|
82
|
1 |
|
return $extracted; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Set all widths, keeping aspect ratios |
|
87
|
|
|
* @param integer $width |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function setWidth($width) |
|
90
|
|
|
{ |
|
91
|
1 |
|
foreach ($this as $item) { |
|
92
|
1 |
|
$item->setWidth($width); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Set all hights, keeping aspect ratios |
|
100
|
|
|
* @param integer $height |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public function setHeight($height) |
|
103
|
|
|
{ |
|
104
|
1 |
|
foreach ($this as $item) { |
|
105
|
1 |
|
$item->setHeight($height); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Multiple the width/height of all the items by $scale |
|
113
|
|
|
* @param float $scale |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function setScale($scale) |
|
116
|
|
|
{ |
|
117
|
1 |
|
foreach ($this as $item) { |
|
118
|
1 |
|
$item->setWidth($item->getWidth() * $scale); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Return a new ItemGroup with items, filtered by the Closure |
|
126
|
|
|
* @param Closure $filter |
|
127
|
|
|
* @return ItemGroup |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function filter(Closure $filter) |
|
130
|
|
|
{ |
|
131
|
1 |
|
return new ItemGroup(array_filter($this->getArrayCopy(), $filter), $this->margin); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Return a new ItemGroup with items, sliced by the $offcet and $limit |
|
136
|
|
|
* @param Closure $filter |
|
|
|
|
|
|
137
|
|
|
* @return ItemGroup |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function slice($offset, $limit) |
|
140
|
|
|
{ |
|
141
|
1 |
|
return new ItemGroup(array_slice($this->getArrayCopy(), $offset, $limit), $this->margin); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Scale all of the widths of the items to match an overall width, respecting margins |
|
146
|
|
|
* @param float $width |
|
147
|
|
|
* @return ItemGroup |
|
148
|
|
|
*/ |
|
149
|
1 |
|
public function scaleToWidth($width) |
|
150
|
|
|
{ |
|
151
|
1 |
|
$items = clone $this; |
|
152
|
1 |
|
$widthWithoutMargins = $width - $items->getGaps() * $this->margin; |
|
153
|
1 |
|
$sumWidths = $items->sumWidths(); |
|
154
|
|
|
|
|
155
|
1 |
|
if ($sumWidths and $sumWidths < $widthWithoutMargins) { |
|
156
|
1 |
|
$difference = $widthWithoutMargins / $sumWidths; |
|
157
|
1 |
|
$items->setScale($difference); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
1 |
|
return $items; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Scale all of the widths of the items to match an overall height, respecting margins |
|
165
|
|
|
* @param float $height |
|
166
|
|
|
* @return ItemGroup |
|
167
|
|
|
*/ |
|
168
|
1 |
|
public function scaleToHeight($height) |
|
169
|
|
|
{ |
|
170
|
1 |
|
$items = clone $this; |
|
171
|
1 |
|
$heightWithoutMargins = $height - $items->getGaps() * $this->margin; |
|
172
|
1 |
|
$sumHeights = $items->sumHeights(); |
|
173
|
|
|
|
|
174
|
1 |
|
if ($sumHeights and $sumHeights < $heightWithoutMargins) { |
|
175
|
1 |
|
$difference = $heightWithoutMargins / $sumHeights; |
|
176
|
1 |
|
$items->setScale($difference); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
1 |
|
return $items; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Remove all items that fall outside of width, when aligned horizontally, spaced by $margin |
|
184
|
|
|
* @param integer $width |
|
185
|
|
|
* @param integer $margin |
|
|
|
|
|
|
186
|
|
|
*/ |
|
187
|
1 |
|
public function horizontalSlice($width) |
|
188
|
|
|
{ |
|
189
|
1 |
|
$current = 0; |
|
190
|
|
|
|
|
191
|
|
|
return $this |
|
192
|
|
|
->filter(function(Item $item) use ($width, &$current) { |
|
193
|
1 |
|
$current += $item->getWidth() + $this->margin; |
|
194
|
|
|
|
|
195
|
1 |
|
return $current <= ($width + $this->margin); |
|
196
|
1 |
|
}); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Remove all items that fall outside of height, when aligned horizontally, spaced by $margin |
|
201
|
|
|
* @param integer $height |
|
202
|
|
|
* @param integer $margin |
|
|
|
|
|
|
203
|
|
|
*/ |
|
204
|
1 |
|
public function verticalSlice($height) |
|
205
|
|
|
{ |
|
206
|
1 |
|
$current = 0; |
|
207
|
|
|
|
|
208
|
|
|
return $this->filter(function(Item $item) use ($height, &$current) { |
|
209
|
1 |
|
$current += $item->getHeight() + $this->margin; |
|
210
|
|
|
|
|
211
|
1 |
|
return $current <= ($height + $this->margin); |
|
212
|
1 |
|
}); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @return integer |
|
217
|
|
|
*/ |
|
218
|
1 |
|
public function getGaps() |
|
219
|
|
|
{ |
|
220
|
1 |
|
return max(count($this) - 1, 0); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @return float |
|
225
|
|
|
*/ |
|
226
|
1 |
|
public function getWidth() |
|
227
|
|
|
{ |
|
228
|
1 |
|
return $this->sumWidths() + $this->getGaps() * $this->margin; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @return float |
|
233
|
|
|
*/ |
|
234
|
1 |
|
public function getHeight() |
|
235
|
|
|
{ |
|
236
|
1 |
|
return $this->sumHeights() + $this->getGaps() * $this->margin; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @return float |
|
241
|
|
|
*/ |
|
242
|
1 |
|
public function sumWidths() |
|
243
|
|
|
{ |
|
244
|
|
|
return array_sum(array_map(function(Item $item) { |
|
245
|
1 |
|
return $item->getWidth(); |
|
246
|
1 |
|
}, $this->getArrayCopy())); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @return float |
|
251
|
|
|
*/ |
|
252
|
1 |
|
public function sumHeights() |
|
253
|
|
|
{ |
|
254
|
|
|
return array_sum(array_map(function(Item $item) { |
|
255
|
1 |
|
return $item->getHeight(); |
|
256
|
1 |
|
}, $this->getArrayCopy())); |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.