forked from jkapuscik2/design-patterns-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeTeamIterator.php
More file actions
47 lines (35 loc) · 1.05 KB
/
EmployeeTeamIterator.php
File metadata and controls
47 lines (35 loc) · 1.05 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
<?php
namespace Behavioral\Iterator;
use Iterator, Countable;
class EmployeeTeamIterator implements Iterator, Countable {
private $position = 0;
private $teamMembers = [];
public function __construct (CompanyEmployeeTeam $employee) {
$this->addTeam($employee);
$this->position = 0;
}
protected function addTeam (CompanyEmployeeTeam $employee): void {
foreach ($employee->getSubordinates() as $member) {
array_push($this->teamMembers, $member);
$this->addTeam($member);
}
}
public function current (): CompanyEmployeeTeam {
return $this->teamMembers[$this->position];
}
public function next (): void {
++$this->position;
}
public function key (): int {
return $this->position;
}
public function valid (): bool {
return isset($this->teamMembers[$this->position]);
}
public function rewind (): void {
$this->position = 0;
}
public function count (): int {
return count($this->teamMembers);
}
}