1 | <?php |
||
5 | class LoopFactory |
||
6 | { |
||
7 | |||
8 | /** |
||
9 | * The stack of Loop instances |
||
10 | * |
||
11 | * @var Loop[] $stack |
||
12 | */ |
||
13 | protected $stack = []; |
||
14 | |||
15 | /** |
||
16 | * Creates a new loop with the given array and adds it to the stack |
||
17 | * |
||
18 | * @param array $items The array that will be iterated |
||
19 | */ |
||
20 | 15 | public function newLoop($items) |
|
24 | |||
25 | /** |
||
26 | * Adds a Loop to the stack |
||
27 | * |
||
28 | * @param Loop $stackItem |
||
29 | */ |
||
30 | 15 | protected function addLoopStack(Loop $stackItem) |
|
31 | { |
||
32 | // Check stack for parent loop to register it with this loop |
||
33 | 15 | if (count($this->stack) > 0) { |
|
34 | 3 | $stackItem->setParentLoop(last($this->stack)); |
|
35 | 3 | } |
|
36 | |||
37 | 15 | array_push($this->stack, $stackItem); |
|
38 | 15 | } |
|
39 | |||
40 | /** |
||
41 | * Returns the stack |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | public function getStack() |
||
49 | |||
50 | /** |
||
51 | * getLastStack method |
||
52 | * |
||
53 | * @return Loop |
||
54 | */ |
||
55 | 15 | public function getLastStack() |
|
59 | |||
60 | /** |
||
61 | * Resets the stack |
||
62 | */ |
||
63 | public function reset() |
||
67 | |||
68 | /** |
||
69 | * To be called first inside the foreach loop. Returns the current loop |
||
70 | * |
||
71 | * @return Loop $current The current loop data |
||
72 | */ |
||
73 | 15 | public function loop() |
|
80 | |||
81 | /** |
||
82 | * To be called before the end of the loop |
||
83 | */ |
||
84 | 15 | public function looped() |
|
90 | |||
91 | /** |
||
92 | * Should be called after the loop has finished |
||
93 | * |
||
94 | * @param $loop |
||
95 | */ |
||
96 | 15 | public function endLoop(&$loop) |
|
107 | } |
||
108 |