mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #8439 from sminnee/consistent-limit
FIX: Make ArrayList::limit() consistent with DataList::limit()
This commit is contained in:
commit
c18e9b1298
@ -8,6 +8,7 @@ use SilverStripe\View\ViewableData;
|
|||||||
use ArrayIterator;
|
use ArrayIterator;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use LogicException;
|
use LogicException;
|
||||||
|
use SilverStripe\Dev\Deprecation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A list object that wraps around an array of objects or arrays.
|
* 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)
|
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) {
|
||||||
|
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);
|
$length = count($this->items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ use SilverStripe\ORM\DataObject;
|
|||||||
use SilverStripe\ORM\Filterable;
|
use SilverStripe\ORM\Filterable;
|
||||||
use SilverStripe\Dev\SapphireTest;
|
use SilverStripe\Dev\SapphireTest;
|
||||||
use SilverStripe\View\ArrayData;
|
use SilverStripe\View\ArrayData;
|
||||||
|
use SilverStripe\Dev\Deprecation;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
|
|
||||||
class ArrayListTest extends SapphireTest
|
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()
|
public function testAddRemove()
|
||||||
{
|
{
|
||||||
$list = new ArrayList(
|
$list = new ArrayList(
|
||||||
|
Loading…
Reference in New Issue
Block a user