-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathPiecesCollection.php
More file actions
41 lines (34 loc) · 871 Bytes
/
PiecesCollection.php
File metadata and controls
41 lines (34 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
namespace Mvdnbrk\DhlParcel\Resources;
use Illuminate\Support\Collection;
class PiecesCollection extends Collection
{
public function __construct(array $items = [])
{
foreach ($items as $item) {
$this->add($item);
}
}
/**
* Add a piece item to this collection.
*
* @param \Mvdnbrk\DhlParcel\Resources\Piece|array $value
* @return $this
*/
public function add($value): self
{
if ($value instanceof Piece) {
return parent::add($value);
}
if (is_array($value)) {
return parent::add(new Piece($value));
}
return $this;
}
public function toArray(): array
{
return array_map(function ($item) {
return $item->toArray();
}, empty($this->items) ? [new Piece] : $this->items);
}
}