-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbehavioral-iterator.php
57 lines (48 loc) · 1.12 KB
/
behavioral-iterator.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
<?php
/**
* This example just to simulate how Iterator can be work
* 2nees.com
*/
class Countries implements \Iterator {
private int $pointer = 0;
private array $countriesList;
/**
* Countries constructor.
* @param array $countriesList
*/
public function __construct(array $countriesList)
{
$this->countriesList = $countriesList;
}
public function current()
{
echo "CURRENT VALUE" . PHP_EOL;
return $this->countriesList[$this->pointer];
}
public function next()
{
echo "NEXT VALUE" . PHP_EOL;
$this->pointer += 1;
}
public function key()
{
echo "CURRENT KEY" . PHP_EOL;
return $this->pointer;
}
public function valid()
{
echo "VALIDATE VALUE" . PHP_EOL;
return isset($this->countriesList[$this->pointer]);
}
public function rewind()
{
echo "RESET VALUE" . PHP_EOL;
$this->pointer = 0;
}
}
$countriesList = new Countries([
"Jordan", "Egypt", "Palestine", "Syria"
]);
foreach ($countriesList as $country){
echo "^_^ => " . $country . PHP_EOL;
}