Skip to content

Commit

Permalink
Fix sorting of DateTimeImmutable instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
ndm2 committed Feb 19, 2016
1 parent 4bb343d commit 162336b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Collection/Iterator/SortIterator.php
Expand Up @@ -15,7 +15,7 @@
namespace Cake\Collection\Iterator;

use Cake\Collection\Collection;
use DateTime;
use DateTimeInterface;

/**
* An iterator that will return the passed items in order. The order is given by
Expand Down Expand Up @@ -68,7 +68,7 @@ public function __construct($items, $callback, $dir = SORT_DESC, $type = SORT_NU
$results = [];
foreach ($items as $key => $value) {
$value = $callback($value);
if ($value instanceof DateTime && $type === SORT_NUMERIC) {
if ($value instanceof DateTimeInterface && $type === SORT_NUMERIC) {
$value = $value->format('U');
}
$results[$key] = $value;
Expand Down
9 changes: 4 additions & 5 deletions tests/TestCase/Collection/Iterator/SortIteratorTest.php
Expand Up @@ -203,9 +203,8 @@ public function testSortDateTime()
$items = new ArrayObject([
new \DateTime('2014-07-21'),
new \DateTime('2015-06-30'),
new \DateTime('2013-08-12')
new \DateTimeImmutable('2013-08-12')
]);
$a = new \DateTime();

$callback = function ($a) {
return $a->add(new \DateInterval('P1Y'));
Expand All @@ -214,20 +213,20 @@ public function testSortDateTime()
$expected = [
new \DateTime('2016-06-30'),
new \DateTime('2015-07-21'),
new \DateTime('2014-08-12')
new \DateTimeImmutable('2013-08-12')

];
$this->assertEquals($expected, $sorted->toList());

$items = new ArrayObject([
new \DateTime('2014-07-21'),
new \DateTime('2015-06-30'),
new \DateTime('2013-08-12')
new \DateTimeImmutable('2013-08-12')
]);

$sorted = new SortIterator($items, $callback, SORT_ASC);
$expected = [
new \DateTime('2014-08-12'),
new \DateTimeImmutable('2013-08-12'),
new \DateTime('2015-07-21'),
new \DateTime('2016-06-30'),
];
Expand Down

0 comments on commit 162336b

Please sign in to comment.