Completed
Push — master ( 274043...94c6c6 )
by Tomáš
03:17
created

PackageCollection::offsetUnset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 1
nc 1
nop 1
crap 2
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
     * EID
33
     *
34
     * @var string
35
     */
36
    private $eid;
37
38
    /**
39
     * PackageCollection constructor
40
     *
41
     * @param string      $shipper
42
     * @param string|null $eid
43
     */
44 17
    public function __construct(string $shipper, string $eid = null)
45
    {
46 17
        $this->shipper = $shipper;
47 17
        $this->eid     = $eid ?: $this->newEID();
48 17
    }
49
50
    /**
51
     * Add package to collection
52
     *
53
     * @param \Inspirum\Balikobot\Model\Values\Package $package
54
     *
55
     * @return void
56
     */
57 17
    public function add(Package $package): void
58
    {
59
        // clone package
60 17
        $package = clone $package;
61
62
        // set collection EID
63 17
        if ($package->hasEID() === false) {
64 14
            $package->setEID($this->eid);
65
        }
66
67
        // add package to collection
68 17
        $this->packages[] = $package;
69 17
    }
70
71
    /**
72
     * Get packages shipper
73
     *
74
     * @return string
75
     */
76 13
    public function getShipper(): string
77
    {
78 13
        return $this->shipper;
79
    }
80
81
    /**
82
     * Get the collection of packages as a plain array
83
     *
84
     * @return array<array<string,mixed>>
85
     */
86 14
    public function toArray(): array
87
    {
88
        return array_map(function (Package $package) {
89 14
            return $package->toArray();
90 14
        }, $this->packages);
91
    }
92
93
    /**
94
     * Get new EID for package batch
95
     *
96
     * @return string
97
     */
98 3
    private function newEID(): string
99
    {
100 3
        return time() . uniqid();
101
    }
102
103
    /**
104
     * Determine if an item exists at an offset
105
     *
106
     * @param int $key
107
     *
108
     * @return bool
109
     */
110
    public function offsetExists($key)
111
    {
112
        return array_key_exists($key, $this->packages);
113
    }
114
115
    /**
116
     * Get an item at a given offset
117
     *
118
     * @param int $key
119
     *
120
     * @return \Inspirum\Balikobot\Model\Values\Package
121
     */
122 13
    public function offsetGet($key)
123
    {
124 13
        return $this->packages[$key];
125
    }
126
127
    /**
128
     * Set the item at a given offset
129
     *
130
     * @param int                                      $key
131
     * @param \Inspirum\Balikobot\Model\Values\Package $value
132
     *
133
     * @return void
134
     */
135
    public function offsetSet($key, $value)
136
    {
137
        $this->packages[$key] = $value;
138
    }
139
140
    /**
141
     * Unset the item at a given offset
142
     *
143
     * @param int $key
144
     *
145
     * @return void
146
     */
147
    public function offsetUnset($key)
148
    {
149
        unset($this->packages[$key]);
150
    }
151
152
    /**
153
     * Count elements of an object
154
     *
155
     * @return int
156
     */
157 1
    public function count(): int
158
    {
159 1
        return count($this->packages);
160
    }
161
162
    /**
163
     * Get an iterator for the items
164
     *
165
     * @return \ArrayIterator<int,\Inspirum\Balikobot\Model\Values\Package>
166
     */
167 1
    public function getIterator(): ArrayIterator
168
    {
169 1
        return new ArrayIterator($this->packages);
170
    }
171
}
172