1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Kata\Algorithm; |
6
|
|
|
|
7
|
|
|
final class Finder |
8
|
|
|
{ |
9
|
|
|
/** @var Person[] */ |
10
|
|
|
private $people; |
11
|
|
|
|
12
|
6 |
|
public function __construct(array $a_people) |
13
|
|
|
{ |
14
|
6 |
|
$this->people = $a_people; |
15
|
6 |
|
} |
16
|
|
|
|
17
|
6 |
|
public function find(int $birthday_sort): PeopleComparison |
18
|
|
|
{ |
19
|
|
|
/** @var PeopleComparison[] $people_comparisons */ |
20
|
6 |
|
$people_comparisons = []; |
21
|
|
|
|
22
|
6 |
|
for ($i = 0; $i < count($this->people); $i++) { |
|
|
|
|
23
|
5 |
|
for ($j = $i + 1; $j < count($this->people); $j++) { |
|
|
|
|
24
|
4 |
|
$current_people_comparison = new PeopleComparison(); |
25
|
|
|
|
26
|
4 |
|
$first_person = $this->people[$i]; |
27
|
4 |
|
$second_person = $this->people[$j]; |
28
|
|
|
|
29
|
4 |
|
if ($this->isFirstPersonMoreYoungerThanSecondPerson($i, $j)) { |
30
|
3 |
|
$current_people_comparison->setFirstPerson($first_person); |
31
|
3 |
|
$current_people_comparison->setSecondPerson($second_person); |
32
|
|
|
} else { |
33
|
3 |
|
$current_people_comparison->setFirstPerson($second_person); |
34
|
3 |
|
$current_people_comparison->setSecondPerson($first_person); |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
$current_people_comparison->setBirthdayDifference($current_people_comparison->secondPerson()->birthDate()->getTimestamp() |
38
|
4 |
|
- $current_people_comparison->firstPerson()->birthDate()->getTimestamp()); |
39
|
|
|
|
40
|
4 |
|
$people_comparisons[] = $current_people_comparison; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
6 |
|
if (empty($people_comparisons)) { |
45
|
2 |
|
return new PeopleComparison(); |
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
return $this->getPeopleComparisonWith($birthday_sort, $people_comparisons); |
49
|
|
|
} |
50
|
|
|
|
51
|
4 |
|
private function isFirstPersonMoreYoungerThanSecondPerson($first_person_index, $second_person_index): bool |
52
|
|
|
{ |
53
|
4 |
|
return $this->people[$first_person_index]->birthDate() < $this->people[$second_person_index]->birthDate(); |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
private function getPeopleComparisonWith(int $birthday_sort, array $people_comparisons) |
57
|
|
|
{ |
58
|
4 |
|
$answer = $people_comparisons[0]; |
59
|
|
|
|
60
|
4 |
|
foreach ($people_comparisons as $result) { |
61
|
|
|
switch ($birthday_sort) { |
62
|
4 |
|
case BirthdaySort::CLOSEST: |
63
|
2 |
|
if ($result->birthdayDifference() < $answer->birthdayDifference()) { |
64
|
1 |
|
$answer = $result; |
65
|
|
|
} |
66
|
2 |
|
break; |
67
|
|
|
|
68
|
2 |
|
case BirthdaySort::FURTHEST: |
69
|
2 |
|
if ($result->birthdayDifference() > $answer->birthdayDifference()) { |
70
|
|
|
$answer = $result; |
71
|
|
|
} |
72
|
2 |
|
break; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
return $answer; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: