diff --git a/src/ORM/ArrayList.php b/src/ORM/ArrayList.php index 9ac0558e2..612347fe2 100644 --- a/src/ORM/ArrayList.php +++ b/src/ORM/ArrayList.php @@ -8,6 +8,7 @@ use SilverStripe\View\ViewableData; use ArrayIterator; use InvalidArgumentException; use LogicException; +use SilverStripe\Dev\Deprecation; /** * A list object that wraps around an array of objects or arrays. @@ -160,7 +161,29 @@ class ArrayList extends ViewableData implements SS_List, Filterable, Sortable, L */ public function limit($length, $offset = 0) { + // Type checking: designed for consistency with DataList::limit() + if (!is_numeric($length) || !is_numeric($offset)) { + Deprecation::notice( + '4.3', + 'Arguments to ArrayList::limit() should be numeric' + ); + } + + if ($length < 0 || $offset < 0) { + Deprecation::notice( + '4.3', + 'Arguments to ArrayList::limit() should be positive' + ); + } + if (!$length) { + if ($length === 0) { + Deprecation::notice( + '4.3', + "limit(0) is deprecated in SS4. In SS5 a limit of 0 will instead return no records." + ); + } + $length = count($this->items); } diff --git a/tests/php/ORM/ArrayListTest.php b/tests/php/ORM/ArrayListTest.php index 513b5d79b..3d6ae6250 100644 --- a/tests/php/ORM/ArrayListTest.php +++ b/tests/php/ORM/ArrayListTest.php @@ -7,6 +7,7 @@ use SilverStripe\ORM\DataObject; use SilverStripe\ORM\Filterable; use SilverStripe\Dev\SapphireTest; use SilverStripe\View\ArrayData; +use SilverStripe\Dev\Deprecation; use stdClass; class ArrayListTest extends SapphireTest @@ -133,6 +134,19 @@ class ArrayListTest extends SapphireTest ); } + /** + * @expectedException PHPUnit_Framework_Error + */ + public function testZeroLimit() + { + Deprecation::notification_version('4.3.0'); + $list = new ArrayList([ + ['Key' => 1], + ['Key' => 2], + ]); + $list->limit(0); + } + public function testAddRemove() { $list = new ArrayList(