-
Notifications
You must be signed in to change notification settings - Fork 0
/
loopy.php
50 lines (42 loc) · 1.13 KB
/
loopy.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
<?php
class Loopy {
protected $i;
protected $count;
protected $first_index;
protected $last_index;
protected $first;
protected $not_first;
protected $last;
protected $not_last;
protected $middle;
public function __construct($countable, $starting_index = 0){
$this->i = (int) $starting_index;
$this->count = count($countable);
$this->first_index = (int) $starting_index;
$this->last_index = $this->count - 1;
$this->update_position();
}
public function __get($name){
return $this->$name;
}
public function only($n){
return $this->count === (int) $n;
}
public function more_than($n){
return $this->count > (int) $n;
}
public function less_than($n){
return $this->count < (int) $n;
}
public function next(){
$this->i++;
$this->update_position();
}
protected function update_position(){
$this->first = $this->i === $this->first_index;
$this->not_first = !$this->first;
$this->last = $this->i === $this->last_index;
$this->not_last = !$this->last;
$this->middle = $this->i > $this->first_index && $this->i < $this->last_index;
}
}