Total Complexity | 14 |
Total Lines | 101 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
13 | final class Changes implements IteratorAggregate, Countable |
||
14 | { |
||
15 | /** @var Change[] */ |
||
16 | private $changes; |
||
17 | |||
18 | /** Generator|null */ |
||
19 | private $generator; |
||
20 | |||
21 | private function __construct() |
||
22 | { |
||
23 | } |
||
24 | |||
25 | public static function empty() : self |
||
26 | { |
||
27 | static $empty; |
||
28 | |||
29 | if ($empty) { |
||
30 | return $empty; |
||
31 | } |
||
32 | |||
33 | $empty = new self(); |
||
34 | |||
35 | $empty->changes = []; |
||
36 | |||
37 | return $empty; |
||
38 | } |
||
39 | |||
40 | /** @param Change[] $changes */ |
||
41 | public static function fromIterator(iterable $changes) : self |
||
42 | { |
||
43 | $instance = new self(); |
||
44 | |||
45 | $instance->changes = []; |
||
46 | $instance->generator = (function () use ($changes) : Generator { |
||
47 | foreach ($changes as $change) { |
||
48 | yield $change; |
||
49 | } |
||
50 | })(); |
||
51 | |||
52 | return $instance; |
||
53 | } |
||
54 | |||
55 | /** @param Change[] $changes */ |
||
56 | public function mergeWithIterator(iterable $changes) : self |
||
57 | { |
||
58 | $instance = new self(); |
||
59 | |||
60 | $instance->changes = []; |
||
61 | $instance->generator = (function () use ($changes) : Generator { |
||
62 | foreach ($this as $change) { |
||
63 | yield $change; |
||
64 | } |
||
65 | |||
66 | foreach ($changes as $change) { |
||
67 | yield $change; |
||
68 | } |
||
69 | })(); |
||
70 | |||
71 | return $instance; |
||
72 | } |
||
73 | |||
74 | public static function fromList(Change ...$changes) : self |
||
81 | } |
||
82 | |||
83 | public function mergeWith(self $other) : self |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * {@inheritDoc} |
||
90 | * |
||
91 | * @return ArrayIterator|Change[] |
||
92 | */ |
||
93 | public function getIterator() : iterable |
||
94 | { |
||
95 | foreach ($this->changes as $change) { |
||
96 | yield $change; |
||
|
|||
97 | } |
||
98 | |||
99 | foreach ($this->generator ?? [] as $change) { |
||
100 | $this->changes[] = $change; |
||
101 | |||
102 | yield $change; |
||
103 | } |
||
104 | |||
105 | $this->generator = null; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * {@inheritDoc} |
||
110 | */ |
||
111 | public function count() : int |
||
114 | } |
||
115 | } |
||
116 |