/
SSViewer_BasicIteratorSupport.php
211 lines (193 loc) · 5.16 KB
/
SSViewer_BasicIteratorSupport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
namespace SilverStripe\View;
/**
* Defines an extra set of basic methods that can be used in templates
* that are not defined on sub-classes of {@link ViewableData}.
*/
class SSViewer_BasicIteratorSupport implements TemplateIteratorProvider
{
/**
* @var int
*/
protected $iteratorPos;
/**
* @var int
*/
protected $iteratorTotalItems;
/**
* @return array
*/
public static function get_template_iterator_variables()
{
return [
'IsFirst',
'IsLast',
'FirstLast',
'Middle',
'MiddleString',
'Even',
'Odd',
'EvenOdd',
'Pos',
'FromEnd',
'TotalItems',
'Modulus',
'MultipleOf',
];
}
/**
* Set the current iterator properties - where we are on the iterator.
*
* @param int $pos position in iterator
* @param int $totalItems total number of items
*/
public function iteratorProperties($pos, $totalItems)
{
$this->iteratorPos = $pos;
$this->iteratorTotalItems = $totalItems;
}
/**
* Returns true if this object is the first in a set.
*
* @return bool
*/
public function IsFirst()
{
return $this->iteratorPos == 0;
}
/**
* Returns true if this object is the last in a set.
*
* @return bool
*/
public function IsLast()
{
return $this->iteratorPos == $this->iteratorTotalItems - 1;
}
/**
* Returns 'first' or 'last' if this is the first or last object in the set.
*
* @return string|null
*/
public function FirstLast()
{
if ($this->IsFirst() && $this->IsLast()) {
return 'first last';
}
if ($this->IsFirst()) {
return 'first';
}
if ($this->IsLast()) {
return 'last';
}
return null;
}
/**
* Return true if this object is between the first & last objects.
*
* @return bool
*/
public function Middle()
{
return !$this->IsFirst() && !$this->IsLast();
}
/**
* Return 'middle' if this object is between the first & last objects.
*
* @return string
*/
public function MiddleString()
{
if ($this->Middle()) {
return 'middle';
}
return null;
}
/**
* Return true if this object is an even item in the set.
* The count starts from $startIndex, which defaults to 1.
*
* @param int $startIndex Number to start count from.
* @return bool
*/
public function Even($startIndex = 1)
{
return !$this->Odd($startIndex);
}
/**
* Return true if this is an odd item in the set.
*
* @param int $startIndex Number to start count from.
* @return bool
*/
public function Odd($startIndex = 1)
{
return (bool)(($this->iteratorPos + $startIndex) % 2);
}
/**
* Return 'even' or 'odd' if this object is in an even or odd position in the set respectively.
*
* @param int $startIndex Number to start count from.
* @return string
*/
public function EvenOdd($startIndex = 1)
{
return ($this->Even($startIndex)) ? 'even' : 'odd';
}
/**
* Return the numerical position of this object in the container set. The count starts at $startIndex.
* The default is the give the position using a 1-based index.
*
* @param int $startIndex Number to start count from.
* @return int
*/
public function Pos($startIndex = 1)
{
return $this->iteratorPos + $startIndex;
}
/**
* Return the position of this item from the last item in the list. The position of the final
* item is $endIndex, which defaults to 1.
*
* @param int $endIndex Value of the last item
* @return int
*/
public function FromEnd($endIndex = 1)
{
return $this->iteratorTotalItems - $this->iteratorPos + $endIndex - 1;
}
/**
* Return the total number of "sibling" items in the dataset.
*
* @return int
*/
public function TotalItems()
{
return $this->iteratorTotalItems;
}
/**
* Returns the modulus of the numerical position of the item in the data set.
* The count starts from $startIndex, which defaults to 1.
*
* @param int $mod The number to perform Mod operation to.
* @param int $startIndex Number to start count from.
* @return int
*/
public function Modulus($mod, $startIndex = 1)
{
return ($this->iteratorPos + $startIndex) % $mod;
}
/**
* Returns true or false depending on if the pos of the iterator is a multiple of a specific number.
* So, <% if MultipleOf(3) %> would return true on indexes: 3,6,9,12,15, etc.
* The count starts from $offset, which defaults to 1.
*
* @param int $factor The multiple of which to return
* @param int $offset Number to start count from.
* @return bool
*/
public function MultipleOf($factor, $offset = 1)
{
return (bool)($this->Modulus($factor, $offset) == 0);
}
}