1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kata\Algorithm\Model; |
4
|
|
|
|
5
|
|
|
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 array $people_array |
20
|
|
|
* |
21
|
|
|
* @return CoupleCollection |
22
|
|
|
*/ |
23
|
6 |
|
public static function buildAllPossibleCouplesFromPeopleArray(array $people_array): CoupleCollection |
24
|
|
|
{ |
25
|
6 |
|
$couples = []; |
26
|
6 |
|
for ($i = 0; $i < count($people_array); $i++) { |
|
|
|
|
27
|
5 |
|
for ($j = $i + 1; $j < count($people_array); $j++) { |
|
|
|
|
28
|
4 |
|
$couples[] = new Couple($people_array[$i], $people_array[$j]); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
6 |
|
return new self($couples); |
33
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function allItems() |
37
|
|
|
{ |
38
|
|
|
return $this->all_items; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function offsetExists($offset) |
42
|
|
|
{ |
43
|
|
|
return isset($this->all_items[$offset]); |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
public function offsetGet($offset) |
47
|
|
|
{ |
48
|
4 |
|
return $this->all_items[$offset]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function offsetSet($offset, $value) |
52
|
|
|
{ |
53
|
|
|
return $this->all_items[$offset] = $value; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function offsetUnset($offset) |
57
|
|
|
{ |
58
|
|
|
unset($this->all_items[$offset]); |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
public function count() |
62
|
|
|
{ |
63
|
6 |
|
return count($this->all_items); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
4 |
|
public function current() |
68
|
|
|
{ |
69
|
4 |
|
return current($this->all_items); |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
public function next() |
73
|
|
|
{ |
74
|
4 |
|
next($this->all_items); |
75
|
4 |
|
} |
76
|
|
|
|
77
|
|
|
public function key() |
78
|
|
|
{ |
79
|
|
|
return key($this->all_items); |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
public function valid() |
83
|
|
|
{ |
84
|
4 |
|
return current($this->all_items); |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
public function rewind() |
88
|
|
|
{ |
89
|
4 |
|
reset($this->all_items); |
90
|
4 |
|
} |
91
|
|
|
} |
92
|
|
|
|