1 | <?php |
||
2 | namespace yentu\database; |
||
3 | |||
4 | class EncapsulatedStack |
||
5 | { |
||
6 | private array $stack = []; |
||
7 | |||
8 | public function push(DatabaseItem $encapsulated): void |
||
9 | { |
||
10 | $this->stack[] = $encapsulated; |
||
11 | } |
||
12 | |||
13 | public function pop(): DatabaseItem |
||
14 | { |
||
15 | return array_pop($this->stack); |
||
16 | } |
||
17 | |||
18 | public function top(): DatabaseItem |
||
19 | { |
||
20 | return end($this->stack); |
||
21 | } |
||
22 | |||
23 | public function hasItems(): bool |
||
24 | { |
||
25 | return !empty($this->stack); |
||
26 | } |
||
27 | |||
28 | public function purge(): void |
||
29 | { |
||
30 | for ($i = 0; $i < count($this->stack); $i++) { |
||
0 ignored issues
–
show
|
|||
31 | $item = array_pop($this->stack); |
||
32 | $item->commit(); |
||
33 | } |
||
34 | } |
||
35 | } |
||
36 | |||
37 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: