Skip to content

Commit

Permalink
Add third Parameter $others to farthest and closest Methods (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoroosevelt committed Sep 22, 2023
1 parent 7ca7713 commit 478b801
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 9 deletions.
32 changes: 27 additions & 5 deletions src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -1888,23 +1888,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;
}

/**
Expand Down
30 changes: 26 additions & 4 deletions src/ChronosDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -1088,23 +1088,45 @@ 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;
}

/**
* Get the farthest date from the instance.
*
* @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;
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/TestCase/Date/ComparisonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
22 changes: 22 additions & 0 deletions tests/TestCase/DateTime/ComparisonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}

0 comments on commit 478b801

Please sign in to comment.