-
Notifications
You must be signed in to change notification settings - Fork 12
/
DefaultPackageDataCollection.php
75 lines (61 loc) · 1.68 KB
/
DefaultPackageDataCollection.php
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace Inspirum\Balikobot\Model\PackageData;
use Inspirum\Arrayable\BaseCollection;
use function sprintf;
use function substr;
use function time;
use function uniqid;
/**
* @extends \Inspirum\Arrayable\BaseCollection<string,mixed,int,\Inspirum\Balikobot\Model\PackageData\PackageData>
*/
final class DefaultPackageDataCollection extends BaseCollection implements PackageDataCollection
{
/**
* @param array<\Inspirum\Balikobot\Model\PackageData\PackageData> $items
*/
public function __construct(
private string $carrier,
array $items = [],
) {
parent::__construct([]);
foreach ($items as $package) {
$this->add($package);
}
}
public function getCarrier(): string
{
return $this->carrier;
}
/** @inheritDoc */
public function getPackages(): array
{
return $this->getItems();
}
public function add(PackageData $item): void
{
$this->offsetAdd($item);
}
/** @inheritDoc */
public function offsetSet(mixed $key, mixed $value): void
{
parent::offsetSet($key, $this->clonePackageWithEid($value));
}
/** @inheritDoc */
public function offsetAdd(mixed $value): void
{
parent::offsetAdd($this->clonePackageWithEid($value));
}
private function clonePackageWithEid(PackageData $package): PackageData
{
$package = clone $package;
if ($package->hasEID() === false) {
$package->setEID($this->newEID());
}
return $package;
}
private function newEID(): string
{
return substr(sprintf('%s%s', time(), uniqid()), -20, 20);
}
}