From 4ca21362c406b40f5e07c8351baccad8a1afc6ca Mon Sep 17 00:00:00 2001 From: Donovan Bourlard Date: Fri, 20 Apr 2018 13:39:56 +0200 Subject: [PATCH] Add DateTimeComparator class --- README.md | 28 +++++++++ spec/Nekland/Tools/DateTimeComparatorSpec.php | 60 ++++++++++++++++++ src/DateTimeComparator.php | 61 +++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 spec/Nekland/Tools/DateTimeComparatorSpec.php create mode 100644 src/DateTimeComparator.php diff --git a/README.md b/README.md index 37ac20e..7927841 100644 --- a/README.md +++ b/README.md @@ -91,3 +91,31 @@ Helps you equals on objects on a similar way as [java](http://stackoverflow.com/ #### equals Method that you must implements to check if the object taking as parameter is equals or not. + +### DateTimeComparator class + +#### ::greatest + +Compare two \DateTimeInterface and return the greatest +_If one of given \DateTimeInterface parameters is null, return is the other one \DateTimeInterface_ +_If both \DateTimeInterface parameters are null, return is null_ + +```php +DateTimeComparator::greatest($dateTime1, $dateTime2) : ?\DateTimeInterface +``` + +* `$dateTime1` The first \DateTimeInterface or null +* `$dateTime2` The second \DateTimeInterface or null + +#### ::lowest + +Compare two \DateTimeInterface and return the lowest +_If one of given \DateTimeInterface parameters is null, return is the other one \DateTimeInterface_ +_If both \DateTimeInterface parameters are null, return is null_ + +```php +DateTimeComparator::lowest($dateTime1, $dateTime2) : ?\DateTimeInterface +``` + +* `$dateTime1` The first \DateTimeInterface or null +* `$dateTime2` The second \DateTimeInterface or null \ No newline at end of file diff --git a/spec/Nekland/Tools/DateTimeComparatorSpec.php b/spec/Nekland/Tools/DateTimeComparatorSpec.php new file mode 100644 index 0000000..a63ef87 --- /dev/null +++ b/spec/Nekland/Tools/DateTimeComparatorSpec.php @@ -0,0 +1,60 @@ +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); + } +} diff --git a/src/DateTimeComparator.php b/src/DateTimeComparator.php new file mode 100644 index 0000000..e3fe026 --- /dev/null +++ b/src/DateTimeComparator.php @@ -0,0 +1,61 @@ + $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; + } + } +}