diff --git a/src/Chronos.php b/src/Chronos.php index 969802b..36f74e0 100644 --- a/src/Chronos.php +++ b/src/Chronos.php @@ -1887,23 +1887,45 @@ public function between(Chronos $start, Chronos $end, bool $equals = true): bool * * @param \Cake\Chronos\Chronos $first The instance to compare with. * @param \Cake\Chronos\Chronos $second The instance to compare with. + * @param \Cake\Chronos\Chronos ...$others Others instances to compare with. * @return self */ - public function closest(Chronos $first, Chronos $second): Chronos + public function closest(Chronos $first, Chronos $second, Chronos ...$others): Chronos { - return $this->diffInSeconds($first) < $this->diffInSeconds($second) ? $first : $second; + $closest = $first; + $closestDiffInSeconds = $this->diffInSeconds($first); + foreach ([$second, ...$others] as $other) { + $otherDiffInSeconds = $this->diffInSeconds($other); + if ($otherDiffInSeconds < $closestDiffInSeconds) { + $closest = $other; + $closestDiffInSeconds = $otherDiffInSeconds; + } + } + + return $closest; } /** * Get the farthest date from the instance. * * @param \Cake\Chronos\Chronos $first The instance to compare with. - * @param \Cake\Chronos\Chronos $seocnd The instance to compare with. + * @param \Cake\Chronos\Chronos $second The instance to compare with. + * @param \Cake\Chronos\Chronos ...$others Others instances to compare with. * @return self */ - public function farthest(Chronos $first, Chronos $seocnd): Chronos + public function farthest(Chronos $first, Chronos $second, Chronos ...$others): Chronos { - return $this->diffInSeconds($first) > $this->diffInSeconds($seocnd) ? $first : $seocnd; + $farthest = $first; + $farthestDiffInSeconds = $this->diffInSeconds($first); + foreach ([$second, ...$others] as $other) { + $otherDiffInSeconds = $this->diffInSeconds($other); + if ($otherDiffInSeconds > $farthestDiffInSeconds) { + $farthest = $other; + $farthestDiffInSeconds = $otherDiffInSeconds; + } + } + + return $farthest; } /** diff --git a/src/ChronosDate.php b/src/ChronosDate.php index 941b515..a49681f 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -1087,11 +1087,22 @@ public function between(ChronosDate $start, ChronosDate $end, bool $equals = tru * * @param \Cake\Chronos\ChronosDate $first The instance to compare with. * @param \Cake\Chronos\ChronosDate $second The instance to compare with. + * @param \Cake\Chronos\ChronosDate ...$others Others instance to compare with. * @return self */ - public function closest(ChronosDate $first, ChronosDate $second): ChronosDate + public function closest(ChronosDate $first, ChronosDate $second, ChronosDate ...$others): ChronosDate { - return $this->diffInDays($first) < $this->diffInDays($second) ? $first : $second; + $closest = $first; + $closestDiffInDays = $this->diffInDays($first); + foreach ([$second, ...$others] as $other) { + $otherDiffInDays = $this->diffInDays($other); + if ($otherDiffInDays < $closestDiffInDays) { + $closest = $other; + $closestDiffInDays = $otherDiffInDays; + } + } + + return $closest; } /** @@ -1099,11 +1110,22 @@ public function closest(ChronosDate $first, ChronosDate $second): ChronosDate * * @param \Cake\Chronos\ChronosDate $first The instance to compare with. * @param \Cake\Chronos\ChronosDate $second The instance to compare with. + * @param \Cake\Chronos\ChronosDate ...$others Others instance to compare with. * @return self */ - public function farthest(ChronosDate $first, ChronosDate $second): ChronosDate + public function farthest(ChronosDate $first, ChronosDate $second, ChronosDate ...$others): ChronosDate { - return $this->diffInDays($first) > $this->diffInDays($second) ? $first : $second; + $farthest = $first; + $farthestDiffInDays = $this->diffInDays($first); + foreach ([$second, ...$others] as $other) { + $otherDiffInDays = $this->diffInDays($other); + if ($otherDiffInDays > $farthestDiffInDays) { + $farthest = $other; + $farthestDiffInDays = $otherDiffInDays; + } + } + + return $farthest; } /** diff --git a/tests/TestCase/Date/ComparisonTest.php b/tests/TestCase/Date/ComparisonTest.php index a095615..1613d45 100644 --- a/tests/TestCase/Date/ComparisonTest.php +++ b/tests/TestCase/Date/ComparisonTest.php @@ -180,6 +180,17 @@ public function testClosestWithEquals() $this->assertSame($dt1, $closest); } + public function testClosestWithOthers(): void + { + $instance = ChronosDate::create(2015, 5, 10); + $dt1 = ChronosDate::create(2015, 5, 4); + $dt2 = ChronosDate::create(2015, 5, 20); + $dt3 = ChronosDate::create(2015, 5, 21); + $dt4 = ChronosDate::create(2015, 5, 22); + $closest = $instance->closest($dt4, $dt3, $dt1, $dt2); + $this->assertSame($dt1, $closest); + } + public function testFarthest() { $instance = ChronosDate::create(2015, 5, 10); @@ -197,4 +208,16 @@ public function testFarthestWithEquals() $Farthest = $instance->farthest($dt1, $dt2); $this->assertSame($dt2, $Farthest); } + + public function testFarthestWithOthers(): void + { + $instance = ChronosDate::create(2015, 5, 10); + $dt1 = ChronosDate::create(2015, 5, 4); + $dt2 = ChronosDate::create(2015, 5, 20); + $dt3 = ChronosDate::create(2015, 5, 21); + $dt4 = ChronosDate::create(2015, 5, 22); + $dt5 = ChronosDate::create(2015, 5, 23); + $Farthest = $instance->farthest($dt1, $dt2, $dt3, $dt4, $dt5); + $this->assertSame($dt5, $Farthest); + } } diff --git a/tests/TestCase/DateTime/ComparisonTest.php b/tests/TestCase/DateTime/ComparisonTest.php index f7ff51b..eeef1c5 100644 --- a/tests/TestCase/DateTime/ComparisonTest.php +++ b/tests/TestCase/DateTime/ComparisonTest.php @@ -296,6 +296,17 @@ public function testClosestWithEquals() $this->assertSame($dt1, $closest); } + public function testClosestWithOthers(): void + { + $instance = Chronos::create(2015, 5, 28, 12, 0, 0); + $dt1 = Chronos::create(2015, 5, 28, 11, 0, 0); + $dt2 = Chronos::create(2015, 5, 28, 14, 0, 0); + $dt3 = Chronos::create(2015, 5, 28, 15, 0, 0); + $dt4 = Chronos::create(2015, 5, 28, 16, 0, 0); + $closest = $instance->closest($dt4, $dt3, $dt1, $dt2); + $this->assertSame($dt1, $closest); + } + public function testFarthest() { $instance = Chronos::create(2015, 5, 28, 12, 0, 0); @@ -313,4 +324,15 @@ public function testFarthestWithEquals() $Farthest = $instance->farthest($dt1, $dt2); $this->assertSame($dt2, $Farthest); } + + public function testFarthestWithOthers(): void + { + $instance = Chronos::create(2015, 5, 28, 12, 0, 0); + $dt1 = Chronos::create(2015, 5, 28, 11, 0, 0); + $dt2 = Chronos::create(2015, 5, 28, 14, 0, 0); + $dt3 = Chronos::create(2015, 5, 28, 15, 0, 0); + $dt4 = Chronos::create(2015, 5, 28, 16, 0, 0); + $Farthest = $instance->farthest($dt1, $dt2, $dt3, $dt4); + $this->assertSame($dt4, $Farthest); + } }