1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kata\Algorithm\Model; |
4
|
|
|
|
5
|
|
|
final class CoupleCollection implements \Iterator, \Countable, \ArrayAccess |
6
|
|
|
{ |
7
|
|
|
/** @var Couple[] */ |
8
|
|
|
private $all_items; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @param Couple[] $couples_array |
12
|
|
|
*/ |
13
|
6 |
|
private function __construct(array $couples_array) |
14
|
|
|
{ |
15
|
6 |
|
$this->all_items = $couples_array; |
16
|
6 |
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param Person[] $people_array |
20
|
|
|
* |
21
|
|
|
* @return CoupleCollection |
22
|
|
|
*/ |
23
|
6 |
|
public static function buildAllPossibleCouplesFromPeopleArray(array $people_array): CoupleCollection |
24
|
|
|
{ |
25
|
6 |
|
$couples = []; |
26
|
|
|
|
27
|
6 |
|
foreach ($people_array as $first_person) { |
28
|
5 |
|
foreach ($people_array as $second_person) { |
29
|
5 |
|
if (!$first_person->equals($second_person)) { |
30
|
5 |
|
$couples[] = new Couple($first_person, $second_person); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
6 |
|
return new self($couples); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function allItems() |
39
|
|
|
{ |
40
|
|
|
return $this->all_items; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function offsetExists($offset) |
44
|
|
|
{ |
45
|
|
|
return isset($this->all_items[$offset]); |
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
public function offsetGet($offset) |
49
|
|
|
{ |
50
|
4 |
|
return $this->all_items[$offset]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function offsetSet($offset, $value) |
54
|
|
|
{ |
55
|
|
|
return $this->all_items[$offset] = $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function offsetUnset($offset) |
59
|
|
|
{ |
60
|
|
|
unset($this->all_items[$offset]); |
61
|
|
|
} |
62
|
|
|
|
63
|
6 |
|
public function count() |
64
|
|
|
{ |
65
|
6 |
|
return count($this->all_items); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
4 |
|
public function current() |
70
|
|
|
{ |
71
|
4 |
|
return current($this->all_items); |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
public function next() |
75
|
|
|
{ |
76
|
4 |
|
next($this->all_items); |
77
|
4 |
|
} |
78
|
|
|
|
79
|
|
|
public function key() |
80
|
|
|
{ |
81
|
|
|
return key($this->all_items); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
public function valid() |
85
|
|
|
{ |
86
|
4 |
|
return current($this->all_items); |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
public function rewind() |
90
|
|
|
{ |
91
|
4 |
|
reset($this->all_items); |
92
|
4 |
|
} |
93
|
|
|
} |
94
|
|
|
|