/**
  * Tests sorting a complex structure with a deep path
  *
  * @return void
  */
 public function testSortComplexDeepPath()
 {
     $items = new ArrayObject([['foo' => ['bar' => 1], 'bar' => 'a'], ['foo' => ['bar' => 12], 'bar' => 'a'], ['foo' => ['bar' => 10], 'bar' => 'a'], ['foo' => ['bar' => 2], 'bar' => 'a']]);
     $sorted = new SortIterator($items, 'foo.bar', SORT_ASC, SORT_NUMERIC);
     $expected = [['foo' => ['bar' => 1], 'bar' => 'a'], ['foo' => ['bar' => 2], 'bar' => 'a'], ['foo' => ['bar' => 10], 'bar' => 'a'], ['foo' => ['bar' => 12], 'bar' => 'a']];
     $this->assertEquals($expected, $sorted->toList());
 }
Example #2
0
 /**
  * {@inheritDoc}
  *
  */
 public function min($callback, $type = SORT_NUMERIC)
 {
     $sorted = new SortIterator($this, $callback, SORT_ASC, $type);
     return $sorted->top();
 }
Example #3
0
 /**
  * Tests sorting datetime
  *
  * @return void
  */
 public function testSortDateTime()
 {
     $items = new ArrayObject([new \DateTime('2014-07-21'), new \DateTime('2015-06-30'), new \DateTime('2013-08-12')]);
     $a = new \DateTime();
     $callback = function ($a) {
         return $a->add(new \DateInterval('P1Y'));
     };
     $sorted = new SortIterator($items, $callback);
     $expected = [new \DateTime('2016-06-30'), new \DateTime('2015-07-21'), new \DateTime('2014-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')]);
     $sorted = new SortIterator($items, $callback, SORT_ASC);
     $expected = [new \DateTime('2014-08-12'), new \DateTime('2015-07-21'), new \DateTime('2016-06-30')];
     $this->assertEquals($expected, $sorted->toList());
 }
 /**
  * Tests top
  *
  * @return void
  */
 public function testTop()
 {
     $items = new ArrayObject([3, 5, 1, 2, 4]);
     $identity = function ($a) {
         return $a;
     };
     $sorted = new SortIterator($items, $identity);
     $this->assertEquals(5, $sorted->top());
     $sorted = new SortIterator([], $identity);
     $this->assertNull($sorted->top());
 }