1
|
|
|
<?php namespace BinPacking3d\Entity; |
2
|
|
|
|
3
|
|
|
use BinPacking3d\Entity; |
4
|
|
|
use BinPacking3d\EntityInterface; |
5
|
|
|
use BinPacking3d\Exception\CriticalException; |
6
|
|
|
use BinPacking3d\Utils; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Bin |
10
|
|
|
* @package BinPacking3d\Entity |
11
|
|
|
*/ |
12
|
|
|
class Bin implements EntityInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var |
17
|
|
|
*/ |
18
|
|
|
private $width; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var |
22
|
|
|
*/ |
23
|
|
|
private $height; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var |
27
|
|
|
*/ |
28
|
|
|
private $depth; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var |
32
|
|
|
*/ |
33
|
|
|
private $identifier; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var |
37
|
|
|
*/ |
38
|
|
|
private $internalIdentifier; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var |
42
|
|
|
*/ |
43
|
|
|
private $maxWeight; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var |
47
|
|
|
*/ |
48
|
|
|
private $outerWidth; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var |
52
|
|
|
*/ |
53
|
|
|
private $outerHeight; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var |
57
|
|
|
*/ |
58
|
|
|
private $outerDepth; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var |
62
|
|
|
*/ |
63
|
|
|
private $weight; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var |
67
|
|
|
*/ |
68
|
|
|
private $items; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var |
72
|
|
|
*/ |
73
|
|
|
private $usedSpace; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var |
77
|
|
|
*/ |
78
|
|
|
private $usedWeight; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var |
82
|
|
|
*/ |
83
|
|
|
private $image; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function render() |
89
|
|
|
{ |
90
|
|
|
return [ |
91
|
|
|
'w' => $this->getWidth(), |
92
|
|
|
'h' => $this->getHeight(), |
93
|
|
|
'd' => $this->getDepth(), |
94
|
|
|
'id' => $this->getIdentifier(), |
95
|
|
|
'max_wg' => $this->getMaxWeight(), |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function getWidth() |
103
|
|
|
{ |
104
|
|
|
return $this->width; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param mixed $width |
109
|
|
|
* @return Bin |
110
|
|
|
*/ |
111
|
|
|
public function setWidth($width) |
112
|
|
|
{ |
113
|
|
|
$this->width = (float)$width; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
|
|
public function getHeight() |
122
|
|
|
{ |
123
|
|
|
return $this->height; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param mixed $height |
128
|
|
|
* @return Bin |
129
|
|
|
*/ |
130
|
|
|
public function setHeight($height) |
131
|
|
|
{ |
132
|
|
|
$this->height = (float)$height; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
|
|
public function getDepth() |
141
|
|
|
{ |
142
|
|
|
return $this->depth; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param mixed $depth |
147
|
|
|
* @return Bin |
148
|
|
|
*/ |
149
|
|
|
public function setDepth($depth) |
150
|
|
|
{ |
151
|
|
|
$this->depth = (float)$depth; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return mixed |
158
|
|
|
*/ |
159
|
|
|
public function getIdentifier() |
160
|
|
|
{ |
161
|
|
|
return $this->identifier; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param mixed $identifier |
166
|
|
|
* @return Bin |
167
|
|
|
*/ |
168
|
|
|
public function setIdentifier($identifier) |
169
|
|
|
{ |
170
|
|
|
$this->identifier = (string)$identifier; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return mixed |
177
|
|
|
*/ |
178
|
|
|
public function getMaxWeight() |
179
|
|
|
{ |
180
|
|
|
return $this->maxWeight; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param mixed $maxWeight |
185
|
|
|
* @return Bin |
186
|
|
|
*/ |
187
|
|
|
public function setMaxWeight($maxWeight) |
188
|
|
|
{ |
189
|
|
|
$this->maxWeight = (float)$maxWeight; |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return bool |
196
|
|
|
* @throws \Exception |
197
|
|
|
*/ |
198
|
|
View Code Duplication |
final public function validate() |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
return Utils::noEmptyItems( |
201
|
|
|
[ |
202
|
|
|
$this->getWidth(), |
203
|
|
|
$this->getHeight(), |
204
|
|
|
$this->getDepth(), |
205
|
|
|
$this->getIdentifier(), |
206
|
|
|
$this->getMaxWeight(), |
207
|
|
|
$this->getInternalIdentifier(), |
208
|
|
|
] |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return mixed |
214
|
|
|
*/ |
215
|
|
|
public function getInternalIdentifier() |
216
|
|
|
{ |
217
|
|
|
return $this->internalIdentifier; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param mixed $internalIdentifier |
222
|
|
|
* @return Bin |
223
|
|
|
*/ |
224
|
|
|
public function setInternalIdentifier($internalIdentifier) |
225
|
|
|
{ |
226
|
|
|
$this->internalIdentifier = $internalIdentifier; |
227
|
|
|
|
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param $path |
233
|
|
|
* @return bool|int |
234
|
|
|
*/ |
235
|
|
|
public function saveImage($path) |
236
|
|
|
{ |
237
|
|
|
if (!$this->getImage()) { |
238
|
|
|
return false; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return (bool)file_put_contents($path, base64_decode($this->getImage())); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return mixed |
246
|
|
|
*/ |
247
|
|
|
public function getImage() |
248
|
|
|
{ |
249
|
|
|
return $this->image; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param mixed $image |
254
|
|
|
* @return Bin |
255
|
|
|
*/ |
256
|
|
|
public function setImage($image) |
257
|
|
|
{ |
258
|
|
|
$this->image = (string)$image; |
259
|
|
|
|
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return mixed |
265
|
|
|
*/ |
266
|
|
|
public function getUsedSpace() |
267
|
|
|
{ |
268
|
|
|
return $this->usedSpace; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param mixed $usedSpace |
273
|
|
|
* @return Bin |
274
|
|
|
*/ |
275
|
|
|
public function setUsedSpace($usedSpace) |
276
|
|
|
{ |
277
|
|
|
$this->usedSpace = (float)$usedSpace; |
278
|
|
|
|
279
|
|
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return mixed |
284
|
|
|
*/ |
285
|
|
|
public function getUsedWeight() |
286
|
|
|
{ |
287
|
|
|
return $this->usedWeight; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param mixed $usedWeight |
292
|
|
|
* @return Bin |
293
|
|
|
*/ |
294
|
|
|
public function setUsedWeight($usedWeight) |
295
|
|
|
{ |
296
|
|
|
$this->usedWeight = (float)$usedWeight; |
297
|
|
|
|
298
|
|
|
return $this; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param Item $item |
303
|
|
|
*/ |
304
|
|
|
public function addItem(Item $item) |
305
|
|
|
{ |
306
|
|
|
$this->items[] = $item; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @return mixed |
311
|
|
|
*/ |
312
|
|
|
public function getItems() |
313
|
|
|
{ |
314
|
|
|
return $this->items; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @return \Generator |
319
|
|
|
*/ |
320
|
|
|
public function yieldItems() |
321
|
|
|
{ |
322
|
|
|
foreach ($this->items as $item) { |
323
|
|
|
yield $item; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @return mixed |
329
|
|
|
*/ |
330
|
|
|
public function getOuterWidth() |
331
|
|
|
{ |
332
|
|
|
return $this->outerWidth; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @param mixed $outerWidth |
337
|
|
|
* @return Bin |
338
|
|
|
*/ |
339
|
|
|
public function setOuterWidth($outerWidth) |
340
|
|
|
{ |
341
|
|
|
$outerWidth = (float)$outerWidth; |
342
|
|
|
|
343
|
|
|
if (!$this->getWidth() || $outerWidth < $this->getWidth()) { |
344
|
|
|
throw new CriticalException('Outer width should be bigger than inner width.'); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
$this->outerWidth = $outerWidth; |
348
|
|
|
|
349
|
|
|
return $this; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* @return mixed |
354
|
|
|
*/ |
355
|
|
|
public function getOuterHeight() |
356
|
|
|
{ |
357
|
|
|
return $this->outerHeight; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @param mixed $outerHeight |
362
|
|
|
* @return Bin |
363
|
|
|
*/ |
364
|
|
|
public function setOuterHeight($outerHeight) |
365
|
|
|
{ |
366
|
|
|
$outerHeight = (float)$outerHeight; |
367
|
|
|
|
368
|
|
|
if (!$this->getHeight() || $outerHeight < $this->getHeight()) { |
369
|
|
|
throw new CriticalException('Outer height should be bigger than inner width.'); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
$this->outerHeight = $outerHeight; |
373
|
|
|
|
374
|
|
|
return $this; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @return mixed |
379
|
|
|
*/ |
380
|
|
|
public function getOuterDepth() |
381
|
|
|
{ |
382
|
|
|
return $this->outerDepth; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* @param mixed $outerDepth |
387
|
|
|
* @return Bin |
388
|
|
|
*/ |
389
|
|
|
public function setOuterDepth($outerDepth) |
390
|
|
|
{ |
391
|
|
|
$outerDepth = (float)$outerDepth; |
392
|
|
|
|
393
|
|
|
if (!$this->getDepth() || $outerDepth < $this->getDepth()) { |
394
|
|
|
throw new CriticalException('Outer depth should be bigger than inner width.'); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
$this->outerDepth = $outerDepth; |
398
|
|
|
|
399
|
|
|
return $this; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* @return mixed |
404
|
|
|
*/ |
405
|
|
|
public function getWeight() |
406
|
|
|
{ |
407
|
|
|
return $this->weight; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* @param mixed $weight |
412
|
|
|
* @return Bin |
413
|
|
|
*/ |
414
|
|
|
public function setWeight($weight) |
415
|
|
|
{ |
416
|
|
|
$this->weight = (float)$weight; |
417
|
|
|
|
418
|
|
|
return $this; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
} |
422
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.