1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Inspirum\Balikobot\Model\Aggregates; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use ArrayIterator; |
7
|
|
|
use Countable; |
8
|
|
|
use Inspirum\Balikobot\Model\Values\Package; |
9
|
|
|
use IteratorAggregate; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @implements \ArrayAccess<int,\Inspirum\Balikobot\Model\Values\Package> |
13
|
|
|
* @implements \IteratorAggregate<int,\Inspirum\Balikobot\Model\Values\Package> |
14
|
|
|
*/ |
15
|
|
|
class PackageCollection implements ArrayAccess, Countable, IteratorAggregate |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Packages |
19
|
|
|
* |
20
|
|
|
* @var array<int,\Inspirum\Balikobot\Model\Values\Package> |
21
|
|
|
*/ |
22
|
|
|
private $packages = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Shipper code |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $shipper; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* PackageCollection constructor |
33
|
|
|
* |
34
|
|
|
* @param string $shipper |
35
|
|
|
*/ |
36
|
19 |
|
public function __construct(string $shipper) |
37
|
|
|
{ |
38
|
19 |
|
$this->shipper = $shipper; |
39
|
19 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add package to collection |
43
|
|
|
* |
44
|
|
|
* @param \Inspirum\Balikobot\Model\Values\Package $package |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
19 |
|
public function add(Package $package): void |
49
|
|
|
{ |
50
|
|
|
// clone package |
51
|
19 |
|
$package = clone $package; |
52
|
|
|
|
53
|
|
|
// set collection EID |
54
|
19 |
|
if ($package->hasEID() === false) { |
55
|
5 |
|
$package->setEID($this->newEID()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// add package to collection |
59
|
19 |
|
$this->packages[] = $package; |
60
|
19 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get packages shipper |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
13 |
|
public function getShipper(): string |
68
|
|
|
{ |
69
|
13 |
|
return $this->shipper; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the collection of packages as a plain array |
74
|
|
|
* |
75
|
|
|
* @return array<array<string,mixed>> |
76
|
|
|
*/ |
77
|
14 |
|
public function toArray(): array |
78
|
|
|
{ |
79
|
|
|
return array_map(function (Package $package) { |
80
|
14 |
|
return $package->toArray(); |
81
|
14 |
|
}, $this->packages); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get new EID for package batch |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
5 |
|
private function newEID(): string |
90
|
|
|
{ |
91
|
5 |
|
return time() . uniqid(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Determine if an item exists at an offset |
96
|
|
|
* |
97
|
|
|
* @param int $key |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
1 |
|
public function offsetExists($key) |
102
|
|
|
{ |
103
|
1 |
|
return array_key_exists($key, $this->packages); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get an item at a given offset |
108
|
|
|
* |
109
|
|
|
* @param int $key |
110
|
|
|
* |
111
|
|
|
* @return \Inspirum\Balikobot\Model\Values\Package |
112
|
|
|
*/ |
113
|
15 |
|
public function offsetGet($key) |
114
|
|
|
{ |
115
|
15 |
|
return $this->packages[$key]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the item at a given offset |
120
|
|
|
* |
121
|
|
|
* @param int $key |
122
|
|
|
* @param \Inspirum\Balikobot\Model\Values\Package $value |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
1 |
|
public function offsetSet($key, $value) |
127
|
|
|
{ |
128
|
1 |
|
$this->packages[$key] = $value; |
129
|
1 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Unset the item at a given offset |
133
|
|
|
* |
134
|
|
|
* @param int $key |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
1 |
|
public function offsetUnset($key) |
139
|
|
|
{ |
140
|
1 |
|
unset($this->packages[$key]); |
141
|
1 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Count elements of an object |
145
|
|
|
* |
146
|
|
|
* @return int |
147
|
|
|
*/ |
148
|
2 |
|
public function count(): int |
149
|
|
|
{ |
150
|
2 |
|
return count($this->packages); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get an iterator for the items |
155
|
|
|
* |
156
|
|
|
* @return \ArrayIterator<int,\Inspirum\Balikobot\Model\Values\Package> |
157
|
|
|
*/ |
158
|
1 |
|
public function getIterator(): ArrayIterator |
159
|
|
|
{ |
160
|
1 |
|
return new ArrayIterator($this->packages); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|