Skip to content
Permalink
Browse files

Implemented Collection::isEmpty()

  • Loading branch information...
lorenzo committed May 4, 2015
1 parent 029501d commit bac36e05d7070d1a5ef15b99cfd59785c20e61db
@@ -830,4 +830,21 @@ public function unfold(callable $transformer = null);
* @return \Cake\Collection\CollectionInterface
*/
public function through(callable $handler);
/**
* Returns whether or not there are elements in this collection
*
* ### Example:
*
* ```
* $items [1, 2, 3];
* (new Collection($items))->isEmpty(); // false
* ```
* ```
* (new Collection([]))->isEmpty(); // true
* ```
*
* @return bool
*/
public function isEmpty();
}
@@ -531,6 +531,15 @@ public function through(callable $handler)
return $result instanceof CollectionInterface ? $result: new Collection($result);
}
/**
* {@inheritDoc}
*
*/
public function isEmpty()
{
return iterator_count($this->take(1)) === 0;
}
/**
* Returns the closest nested iterator that can be safely traversed without
* losing any possible transformations.
@@ -1226,4 +1226,23 @@ public function testDebug()
];
$this->assertSame($expected, $result);
}
/**
* Tests the isEmpty() method
*
* @return void
*/
public function testIsEmpty()
{
$collection = new Collection([1, 2, 3]);
$this->assertFalse($collection->isEmpty());
$collection = $collection->map(function () {
return null;
});
$this->assertFalse($collection->isEmpty());
$collection = $collection->filter();
$this->assertTrue($collection->isEmpty());
}
}

0 comments on commit bac36e0

Please sign in to comment.
You can’t perform that action at this time.