|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Advmaker\BladeLoop; |
|
4
|
|
|
|
|
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
|
|
|
protected $loopFactory; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Sets the parent loop |
|
32
|
|
|
* |
|
33
|
|
|
* @param Loop $parentLoop |
|
34
|
|
|
* {@inheritdocs} |
|
35
|
|
|
*/ |
|
36
|
|
|
public function setParentLoop(Loop $parentLoop) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->parentLoop = $parentLoop; |
|
39
|
|
|
$this->data['parent'] = $parentLoop; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns the full loop stack of the LoopFactory |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getLoopStack() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->loopFactory->getStack(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Resets the loop stack of the LoopFactory |
|
54
|
|
|
*/ |
|
55
|
|
|
public function resetLoopStack() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->loopFactory->reset(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Instantiates the class |
|
62
|
|
|
* |
|
63
|
|
|
* @param LoopFactory $loopFactory |
|
64
|
|
|
* @param array $items The array that's being iterated |
|
65
|
|
|
*/ |
|
66
|
6 |
|
public function __construct(LoopFactory $loopFactory, $items) |
|
67
|
|
|
{ |
|
68
|
6 |
|
$this->loopFactory = $loopFactory; |
|
69
|
6 |
|
$this->setItems($items); |
|
70
|
6 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Sets the array to monitor |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $items The array that's being iterated |
|
76
|
|
|
*/ |
|
77
|
6 |
|
public function setItems($items) |
|
78
|
|
|
{ |
|
79
|
6 |
|
$this->items = $items; |
|
80
|
6 |
|
$total = count($items); |
|
81
|
6 |
|
$this->data = [ |
|
82
|
6 |
|
'index1' => 1, |
|
83
|
6 |
|
'index' => 0, |
|
84
|
6 |
|
'revindex1' => $total, |
|
85
|
6 |
|
'revindex' => $total - 1, |
|
86
|
6 |
|
'first' => true, |
|
87
|
6 |
|
'last' => false, |
|
88
|
6 |
|
'odd' => false, |
|
89
|
6 |
|
'even' => true, |
|
90
|
|
|
'length' => $total |
|
91
|
6 |
|
]; |
|
92
|
6 |
|
} |
|
93
|
|
|
|
|
94
|
6 |
|
public function getItems() |
|
95
|
|
|
{ |
|
96
|
6 |
|
return $this->items; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Magic method to access the loop data properties |
|
101
|
|
|
* |
|
102
|
|
|
* @param $key |
|
103
|
|
|
* |
|
104
|
|
|
* @return mixed |
|
105
|
|
|
*/ |
|
106
|
6 |
|
public function __get($key) |
|
107
|
|
|
{ |
|
108
|
6 |
|
return $this->data[$key]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* To be called first in a loop before anything else |
|
113
|
|
|
*/ |
|
114
|
6 |
|
public function before() |
|
115
|
|
|
{ |
|
116
|
6 |
|
$this->data['even'] = $this->data['index'] % 2 === 0; |
|
117
|
6 |
|
$this->data['odd'] = ! $this->data['even']; |
|
118
|
|
|
|
|
119
|
6 |
|
$this->data['first'] = $this->data['index'] === 0; |
|
120
|
6 |
|
$this->data['last'] = $this->data['revindex'] === 0; |
|
121
|
6 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* To be called last in a loop after everything else |
|
125
|
|
|
*/ |
|
126
|
6 |
|
public function after() |
|
127
|
|
|
{ |
|
128
|
6 |
|
$this->data['index']++; |
|
129
|
6 |
|
$this->data['index1']++; |
|
130
|
6 |
|
$this->data['revindex']--; |
|
131
|
6 |
|
$this->data['revindex1']--; |
|
132
|
6 |
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|