|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FRZB\Component\TransactionalMessenger\Storage; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @template T |
|
9
|
|
|
* |
|
10
|
|
|
* @extends StorageInterface<T> |
|
11
|
|
|
*/ |
|
12
|
|
|
class Storage implements StorageInterface |
|
13
|
|
|
{ |
|
14
|
|
|
public function __construct( |
|
15
|
|
|
/** @param \Iterator<T> $items */ |
|
16
|
|
|
private iterable $items = [], |
|
17
|
|
|
) { |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** {@inheritdoc} */ |
|
21
|
|
|
public function init(iterable|StorageInterface $storage): static |
|
22
|
|
|
{ |
|
23
|
|
|
$this->items = $storage instanceof StorageInterface ? $storage->list() : $storage; |
|
24
|
|
|
|
|
25
|
|
|
return $this; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** {@inheritdoc} */ |
|
29
|
|
|
public function append(object ...$items): static |
|
30
|
|
|
{ |
|
31
|
|
|
array_push($this->items, ...$items); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** {@inheritdoc} */ |
|
37
|
|
|
public function prepend(object ...$items): static |
|
38
|
|
|
{ |
|
39
|
|
|
array_unshift($this->items, ...$items); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** {@inheritdoc} */ |
|
45
|
|
|
public function next(): ?object |
|
46
|
|
|
{ |
|
47
|
|
|
return array_shift($this->items); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** {@inheritdoc} */ |
|
51
|
|
|
public function iterate(): iterable |
|
52
|
|
|
{ |
|
53
|
|
|
while ($item = $this->next()) { |
|
54
|
|
|
yield $item; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** {@inheritdoc} */ |
|
59
|
|
|
public function map(callable $callback): static |
|
60
|
|
|
{ |
|
61
|
|
|
return new static(array_map($callback, $this->items)); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** {@inheritdoc} */ |
|
65
|
|
|
public function filter(callable $callback): static |
|
66
|
|
|
{ |
|
67
|
|
|
return new static(array_filter($this->items, $callback)); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** {@inheritdoc} */ |
|
71
|
|
|
public function merge(iterable|StorageInterface $storage): static |
|
72
|
|
|
{ |
|
73
|
|
|
$this->items = [...$this->items, ...$storage instanceof StorageInterface ? $storage->list() : $storage]; |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** {@inheritdoc} */ |
|
79
|
|
|
public function clear(): static |
|
80
|
|
|
{ |
|
81
|
|
|
$this->items = []; |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** {@inheritdoc} */ |
|
87
|
|
|
public function list(): iterable |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->items; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** {@inheritdoc} */ |
|
93
|
|
|
public function count(): int |
|
94
|
|
|
{ |
|
95
|
|
|
return \count($this->items); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|