| 1 | <?php |
||
| 5 | class Loop |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * The array that is being iterated |
||
| 9 | * |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | protected $items = []; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * The data for the current $loop item |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $data; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The parent loop, if any |
||
| 23 | * |
||
| 24 | * @var Loop |
||
| 25 | */ |
||
| 26 | protected $parentLoop; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Sets the parent loop |
||
| 30 | * |
||
| 31 | * @param Loop $parentLoop |
||
| 32 | * {@inheritdocs} |
||
| 33 | */ |
||
| 34 | 3 | public function setParentLoop(Loop $parentLoop) |
|
| 35 | { |
||
| 36 | 3 | $this->parentLoop = $parentLoop; |
|
| 37 | 3 | $this->data['parent'] = $parentLoop; |
|
| 38 | 3 | } |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Instantiates the class |
||
| 42 | * |
||
| 43 | * @param array $items The array that's being iterated |
||
| 44 | */ |
||
| 45 | 15 | public function __construct($items) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Sets the array to monitor |
||
| 52 | * |
||
| 53 | * @param array $items The array that's being iterated |
||
| 54 | */ |
||
| 55 | 15 | public function setItems($items) |
|
| 71 | |||
| 72 | 15 | public function getItems() |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Magic method to access the loop data properties |
||
| 79 | * |
||
| 80 | * @param $key |
||
| 81 | * |
||
| 82 | * @return mixed |
||
| 83 | */ |
||
| 84 | 15 | public function __get($key) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * To be called first in a loop before anything else |
||
| 91 | */ |
||
| 92 | 15 | public function before() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * To be called last in a loop after everything else |
||
| 103 | */ |
||
| 104 | 15 | public function after() |
|
| 111 | } |
||
| 112 |