Skip to content

Commit

Permalink
Merge pull request #12 from mcsky/feature/datetime-comparator-handle-…
Browse files Browse the repository at this point in the history
…n-dates

DateTimeComparator now handle N parameters with same behavior
  • Loading branch information
Nek- authored Jan 13, 2019
2 parents 439b9b1 + 45a0d6e commit 5c37615
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 59 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Modified - 2019-01-10
- Update the DateTimeComparator functions to handle N parameters

## [2.4.0] - 2019-01-07
### Added
Expand Down
21 changes: 7 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,32 +126,25 @@ Method that you must implements to check if the object taking as parameter is eq

### DateTimeComparator class

For following methods `lowest` and `greatest`, you can provide unlimited `DateTimeInterface` objects.
Please note that non DateTimeInterface objects will be ignored by functions.

#### ::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_
Compare \DateTimeInterface from parameters and return the greatest

```php
DateTimeComparator::greatest($dateTime1, $dateTime2) : ?\DateTimeInterface
DateTimeComparator::greatest($dateTime1, $dateTime2, $dateTime3, ...) : ?\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_
Compare \DateTimeInterface from parameters and return the lowest

```php
DateTimeComparator::lowest($dateTime1, $dateTime2) : ?\DateTimeInterface
DateTimeComparator::lowest($dateTime1, $dateTime2, $dateTime3, ...) : ?\DateTimeInterface
```

* `$dateTime1` The first \DateTimeInterface or null
* `$dateTime2` The second \DateTimeInterface or null

### Temporary file management

The class `TemporaryFile` helps you to create temporary file with ease.
Expand Down
25 changes: 21 additions & 4 deletions spec/Nekland/Tools/DateTimeComparatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace spec\Nekland\Tools;

use Nekland\Tools\DateTimeComparator;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class DateTimeComparatorSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Nekland\Tools\DateTimeComparator');
$this->shouldHaveType(DateTimeComparator::class);
}

function it_should_return_greatest_date_time()
Expand All @@ -31,7 +31,7 @@ function it_should_return_greatest_date_time()
$this::greatest($dateTime2, $dateTime1)->shouldReturn($dateTime1);
$this::greatest($dateTime1, null)->shouldReturn($dateTime1);
$this::greatest(null, $dateTime1)->shouldReturn($dateTime1);

$this::greatest(null, null)->shouldReturn(null);
}

Expand All @@ -54,7 +54,24 @@ function it_should_return_lowest_date_time()
$this::lowest($dateTime2, $dateTime1)->shouldReturn($dateTime1);
$this::lowest($dateTime1, null)->shouldReturn($dateTime1);
$this::lowest(null, $dateTime1)->shouldReturn($dateTime1);

$this::lowest(null, null)->shouldReturn(null);
}

function it_should_can_handle_multiple_datetimes()
{
$dateTime1 = new \DateTimeImmutable();
$dateTime2 = new \DateTimeImmutable('+2 days');
$dateTime3 = new \DateTimeImmutable('+4 days');
$dateTime4 = new \DateTimeImmutable('+6 days');

$this::lowest($dateTime2, $dateTime1, $dateTime4, $dateTime3)->shouldReturn($dateTime1);
$this::greatest($dateTime3, $dateTime4, $dateTime1, $dateTime2)->shouldReturn($dateTime4);
}

function it_should_return_null_if_no_datetime_given()
{
$this::lowest('this', 'is', 0, false, new \Exception())->shouldReturn(null);
$this::greatest('this', 'is', 0, false, new \Exception())->shouldReturn(null);
}
}
69 changes: 28 additions & 41 deletions src/Tools/DateTimeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,44 @@
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
* Get the greatest "DateTimeInterface" from the parameters
* @param mixed $datetimes,... A various number of instances implementing `DateTimeInterface`
* @return \DateTimeInterface|null
*/
public static function greatest(\DateTimeInterface $dateTime1 = null, \DateTimeInterface $dateTime2 = null)
public static function greatest(... $datetimes)
{
// - 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);
$greatest = null;
foreach ($datetimes as $datetime) {
if (!$datetime instanceof \DateTimeInterface) {
continue;
}

if ($datetime > $greatest || null === $greatest) {
$greatest = $datetime;
}
}

return $dateTime1 > $dateTime2 ? $dateTime1 : $dateTime2;
return $greatest;
}

/**
* 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
* Get the lowest "DateTimeInterface" from the parameters
* @param mixed $datetimes,... A various number of instances implementing `DateTimeInterface`
* @return \DateTimeInterface|null
*/
public static function lowest(\DateTimeInterface $dateTime1 = null, \DateTimeInterface $dateTime2 = null)
public static function lowest(... $datetimes)
{
// - 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);
$lowest = null;
foreach ($datetimes as $datetime) {
if (!$datetime instanceof \DateTimeInterface) {
continue;
}

if ($datetime < $lowest || null === $lowest) {
$lowest = $datetime;
}
}

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;
}
return $lowest;
}
}

0 comments on commit 5c37615

Please sign in to comment.