Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Danoha committed Jun 7, 2017
1 parent 366c51e commit 657258b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
25 changes: 13 additions & 12 deletions src/DateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

namespace Danoha;

class DateRange {
class DateRange
{

/** @var \DateTime|NULL */
protected $from;
/** @var \DateTime|NULL */
protected $from;

/** @var \DateTime|NULL */
protected $to;
/** @var \DateTime|NULL */
protected $to;

/**
* @param \DateTime|NULL $from
* @param \DateTime|NULL $to
*/
/**
* @param \DateTime|NULL $from
* @param \DateTime|NULL $to
*/
public function __construct(\DateTime $from = NULL, \DateTime $to = NULL)
{
$this->from = $from;
$this->to = $to;
}
$this->from = $from;
$this->to = $to;
}

/**
* @return \DateTime|NULL
Expand Down
29 changes: 16 additions & 13 deletions src/DateRangeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
namespace Danoha;


class DateRangeCollection {
class DateRangeCollection
{

/** @var DateRange[] */
/** @var DateRange[] */
protected $ranges = [];

/**
* @param array $collection
/**
* @param array $collection
* @throws \InvalidArgumentException
*/
public function __construct($collection) {
foreach ($collection as $item) {
$this->ranges[] = DateRange::wrap($item);
}
*/
public function __construct($collection)
{
foreach ($collection as $item) {
$this->ranges[] = DateRange::wrap($item);
}

usort($this->ranges, function (DateRange $a, DateRange $b) {
$fromA = $a->getFrom();
Expand Down Expand Up @@ -53,16 +55,17 @@ public function __construct($collection) {

return 0;
});
}
}

/**
* @return array
*/
public function unwrap() {
public function unwrap()
{
return array_map(function (DateRange $range) {
return $range->unwrap();
}, $this->ranges);
}
}

/**
* @return DateRange[]
Expand Down Expand Up @@ -168,7 +171,7 @@ public function join($collection = NULL)
}

return new static(array_filter($ranges));
}
}

/**
* @param array|self $subtrahends
Expand Down
14 changes: 7 additions & 7 deletions tests/DateRange/Wrap.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
require_once './../bootstrap.php';
use Tester\Assert;

Assert::same(\Danoha\DateRange::class, get_class(\Danoha\DateRange::wrap([ 'from' => NULL, 'to' => NULL, ])));
Assert::same(\Danoha\DateRange::class, get_class(\Danoha\DateRange::wrap(['from' => NULL, 'to' => NULL,])));

$range = \Danoha\DateRange::wrap([ 'from' => NULL, 'to' => NULL, ]);
$range = \Danoha\DateRange::wrap(['from' => NULL, 'to' => NULL,]);
Assert::same($range, \Danoha\DateRange::wrap($range));

$from = new \DateTime('2016-01-01');
$to = new \DateTime('2017-01-01');
$range = \Danoha\DateRange::wrap([ 'from' => $from, 'to' => $to, ]);
$range = \Danoha\DateRange::wrap(['from' => $from, 'to' => $to,]);
$range2 = new \Danoha\DateRange($from, $to);
Assert::same($from, $range->getFrom());
Assert::same($to, $range->getTo());
Expand All @@ -21,15 +21,15 @@ Assert::same([
'to' => $to,
], $range->unwrap());

Assert::same(NULL, \Danoha\DateRange::wrap([ new \DateTime('2016-01-01'), NULL, ])->getTo());
Assert::same(NULL, \Danoha\DateRange::wrap([ NULL, new \DateTime('2016-01-01'), ])->getFrom());
Assert::same(NULL, \Danoha\DateRange::wrap([new \DateTime('2016-01-01'), NULL,])->getTo());
Assert::same(NULL, \Danoha\DateRange::wrap([NULL, new \DateTime('2016-01-01'),])->getFrom());

Assert::throws(function () {
\Danoha\DateRange::wrap([ 'asd' => '', ]);
\Danoha\DateRange::wrap(['asd' => '',]);
}, InvalidArgumentException::class, 'Expected array with from and to or exactly 2 items');


Assert::throws(function () {
\Danoha\DateRange::wrap([ 'asd', ]);
\Danoha\DateRange::wrap(['asd',]);
}, InvalidArgumentException::class, 'Expected array with from and to or exactly 2 items');

2 changes: 1 addition & 1 deletion tests/DateRangeCollection/Wrap.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Assert::same(\Danoha\DateRangeCollection::class, get_class($coll1));

$from = new \DateTime;
$coll2 = [
[ 'from' => $from, 'to' => NULL, ],
['from' => $from, 'to' => NULL,],
];
Assert::same($coll2, (new \Danoha\DateRangeCollection($coll2))->unwrap());

Expand Down

0 comments on commit 657258b

Please sign in to comment.