Skip to content

Commit

Permalink
Add DateTimeComparator class
Browse files Browse the repository at this point in the history
  • Loading branch information
Awkan committed Apr 23, 2018
1 parent 19bfa70 commit 4ca2136
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
60 changes: 60 additions & 0 deletions spec/Nekland/Tools/DateTimeComparatorSpec.php
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);
}
}
61 changes: 61 additions & 0 deletions src/DateTimeComparator.php
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;
}
}
}

0 comments on commit 4ca2136

Please sign in to comment.