class-loop.php
1.59 KB
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
<?php
/**
* Repeater row looping.
*
* @package Block_Lab
* @copyright Copyright(c) 2020, Block Lab
* @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0)
*/
namespace Block_Lab\Blocks;
/**
* Class Loop
*/
class Loop {
/**
* Current pointer in active loops.
*
* An associative array of $loop_name => $pointer.
* The $pointer is an int of the current iteration, e.g: 0, 1, or 2.
*
* @var array
*/
public $loops = [];
/**
* Currently active loop
*
* @var string
*/
public $active;
/**
* Set a loop to active.
*
* @param string $name The field name.
*/
public function set_active( $name ) {
$this->active = $name;
}
/**
* Get the current pointer for a loop.
*
* @param string $name The field name.
*
* @return bool
*/
public function get_row( $name = '' ) {
if ( empty( $name ) ) {
$name = $this->active;
}
if ( isset( $this->loops[ $name ] ) ) {
return $this->loops[ $name ];
}
return false;
}
/**
* Increment the row pointer for a loop.
*
* @param string $name The field name.
* @return int
*/
public function increment( $name = '' ) {
if ( empty( $name ) ) {
$name = $this->active;
}
if ( isset( $this->loops[ $name ] ) ) {
$this->loops[ $name ]++;
} else {
$this->loops[ $name ] = 0;
}
return $this->loops[ $name ];
}
/**
* Reset the loop so that it can be restarted.
*
* @param string $name The field name.
*/
public function reset( $name = '' ) {
if ( empty( $name ) ) {
$name = $this->active;
}
unset( $this->loops[ $name ] );
}
}