-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace spec\Nekland\Tools; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
class DateTimeComparatorSpec extends ObjectBehavior | ||
{ | ||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Nekland\Tools\DateTimeComparator'); | ||
} | ||
|
||
function it_should_return_greatest_date_time() | ||
{ | ||
$dateTimeImmutable1 = new \DateTimeImmutable('+2 days'); | ||
$dateTimeImmutable2 = new \DateTimeImmutable(); | ||
|
||
$dateTime1 = new \DateTime('+2 days'); | ||
$dateTime2 = new \DateTime(); | ||
|
||
// DateTimeImmutable tests | ||
$this::greatest($dateTimeImmutable1, $dateTimeImmutable2)->shouldReturn($dateTimeImmutable1); | ||
$this::greatest($dateTimeImmutable2, $dateTimeImmutable1)->shouldReturn($dateTimeImmutable1); | ||
$this::greatest($dateTimeImmutable1, null)->shouldReturn($dateTimeImmutable1); | ||
$this::greatest(null, $dateTimeImmutable1)->shouldReturn($dateTimeImmutable1); | ||
|
||
// DateTime tests | ||
$this::greatest($dateTime1, $dateTime2)->shouldReturn($dateTime1); | ||
$this::greatest($dateTime2, $dateTime1)->shouldReturn($dateTime1); | ||
$this::greatest($dateTime1, null)->shouldReturn($dateTime1); | ||
$this::greatest(null, $dateTime1)->shouldReturn($dateTime1); | ||
|
||
$this::greatest(null, null)->shouldReturn(null); | ||
} | ||
|
||
function it_should_return_lowest_date_time() | ||
{ | ||
$dateTimeImmutable1 = new \DateTimeImmutable(); | ||
$dateTimeImmutable2 = new \DateTimeImmutable('+2 days'); | ||
|
||
$dateTime1 = new \DateTime(); | ||
$dateTime2 = new \DateTime('+2 days'); | ||
|
||
// DateTimeImmutable tests | ||
$this::lowest($dateTimeImmutable1, $dateTimeImmutable2)->shouldReturn($dateTimeImmutable1); | ||
$this::lowest($dateTimeImmutable2, $dateTimeImmutable1)->shouldReturn($dateTimeImmutable1); | ||
$this::lowest($dateTimeImmutable1, null)->shouldReturn($dateTimeImmutable1); | ||
$this::lowest(null, $dateTimeImmutable1)->shouldReturn($dateTimeImmutable1); | ||
|
||
// DateTime tests | ||
$this::lowest($dateTime1, $dateTime2)->shouldReturn($dateTime1); | ||
$this::lowest($dateTime2, $dateTime1)->shouldReturn($dateTime1); | ||
$this::lowest($dateTime1, null)->shouldReturn($dateTime1); | ||
$this::lowest(null, $dateTime1)->shouldReturn($dateTime1); | ||
|
||
$this::lowest(null, null)->shouldReturn(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Nekland\Tools; | ||
|
||
class DateTimeComparator | ||
{ | ||
/** | ||
* Get the greatest DateTime between the twice given as parameter | ||
* If one DateTime is null, return the other | ||
* If the twice DateTime are null, return null | ||
*/ | ||
public static function greatest(\DateTimeInterface $dateTime1 = null, \DateTimeInterface $dateTime2 = null) | ||
{ | ||
// - If one DateTime is null, return the other date | ||
// - If the twice DateTime are null, return null | ||
if (\is_null($dateTime1) || \is_null($dateTime2)) { | ||
return self::getNonNullableDateTime($dateTime1, $dateTime2); | ||
} | ||
|
||
return $dateTime1 > $dateTime2 ? $dateTime1 : $dateTime2; | ||
} | ||
|
||
/** | ||
* Get the lowest DateTime between the twice given as parameter | ||
* If one DateTime is null, return the other | ||
* If the twice DateTime are null, return null | ||
*/ | ||
public static function lowest(\DateTimeInterface $dateTime1 = null, \DateTimeInterface $dateTime2 = null) | ||
{ | ||
// - If one DateTime is null, return the other date | ||
// - If the twice DateTime are null, return null | ||
if (\is_null($dateTime1) || \is_null($dateTime2)) { | ||
return self::getNonNullableDateTime($dateTime1, $dateTime2); | ||
} | ||
|
||
return $dateTime1 < $dateTime2 ? $dateTime1 : $dateTime2; | ||
} | ||
|
||
/** | ||
* Check which DateTime parameter is null | ||
* Use this method when you know that at least one or more DateTime is/are null | ||
* - Only one DateTime is null: return the other one which is the greatest/lowest DateTime by default | ||
* - The twice DateTime are null: return null since no DateTime could be returned | ||
*/ | ||
private static function getNonNullableDateTime(\DateTimeInterface $dateTime1 = null, \DateTimeInterface $dateTime2 = null) | ||
{ | ||
if (\is_null($dateTime1) && \is_null($dateTime2)) { | ||
return null; | ||
} | ||
|
||
// If date1 is null, the only result can be date2 | ||
if (\is_null($dateTime1)) { | ||
return $dateTime2; | ||
} | ||
|
||
// If date2 is null, the only result can be date1 | ||
if (\is_null($dateTime2)) { | ||
return $dateTime1; | ||
} | ||
} | ||
} |