Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

API CHANGE: Introduce SS_Limitable class for adding to SS_Lists that …

…have limit capability.

API CHANGE: Deprecated SS_List::getRange() in favour of SS_Limitable::limit().
API CHANGE: Introduce SS_Limitable::limit($limit, $offset = 0) as the only modern way of specifying limits; deprecate all others.
  • Loading branch information...
commit a55e06f6b55ef7d1e2bb3be6e43c2c7b5b52db49 1 parent 193288a
@sminnee sminnee authored stojg committed
View
2  core/PaginatedList.php
@@ -149,7 +149,7 @@ public function setPaginationFromQuery(SQLQuery $query) {
*/
public function getIterator() {
return new IteratorIterator(
- $this->list->getRange($this->getPageStart(), $this->pageLength)
+ $this->list->limit($this->pageLength, $this->getPageStart())
);
}
View
2  forms/TableListField.php
@@ -430,7 +430,7 @@ function sourceItems() {
$SQL_start = 0;
}
- $items = $items->getRange($SQL_start, $SQL_limit);
+ $items = $items->limit($SQL_limit, $SQL_start);
}
return $items;
View
16 forms/gridfield/GridFieldPaginator.php
@@ -64,14 +64,14 @@ public function getManipulatedData(GridField $gridField, SS_List $dataList) {
if(!is_int($state->currentPage))
$state->currentPage = 1;
- if(!$this->getListPaginatable($dataList)) {
+ if(!($dataList instanceof SS_Limitable)) {
return $dataList;
}
if(!$state->currentPage) {
- return $dataList->getRange(0, (int)$this->itemsPerPage);
+ return $dataList->limit((int)$this->itemsPerPage);
}
$startRow = $this->itemsPerPage * ($state->currentPage - 1);
- return $dataList->getRange((int)$startRow, (int)$this->itemsPerPage);
+ return $dataList->limit((int)$this->itemsPerPage, (int)$startRow);
}
/**
@@ -159,14 +159,4 @@ public function getItemsPerPage() {
return $this->itemsPerPage;
}
- /** Duck check to see if list support methods we need to paginate */
- protected function getListPaginatable(SS_List $list) {
- // If no list yet, not paginatable
- if (!$list) return false;
- // Check for methods we use
- if(!method_exists($list, 'getRange')) return false;
- if(!method_exists($list, 'limit')) return false;
- // Default it true
- return true;
- }
}
View
23 model/ArrayList.php
@@ -5,7 +5,7 @@
* @package sapphire
* @subpackage model
*/
-class ArrayList extends ViewableData implements SS_List {
+class ArrayList extends ViewableData implements SS_List, SS_Limitable {
/**
* Holds the items in the list
@@ -119,6 +119,18 @@ public function toNestedArray() {
* @return ArrayList
*/
public function getRange($offset, $length) {
+ Deprecation::notice("3.0", 'getRange($offset, $length) is deprecated. Use limit($length, $offset) instead. Note the new argument order.');
+ return $this->limit($length, $offset);
+ }
+
+ /**
+ * Get a sub-range of this dataobjectset as an array
+ *
+ * @param int $offset
+ * @param int $length
+ * @return ArrayList
+ */
+ public function limit($length, $offset = 0) {
return new ArrayList(array_slice($this->items, $offset, $length));
}
@@ -357,6 +369,15 @@ public function sort() {
call_user_func_array('array_multisort', $multisortArgs);
return $this;
}
+
+ /**
+ * Returns true if the given column can be used to filter the records.
+ *
+ * It works by checking the fields available in the first record of the list.
+ */
+ public function canFilterBy($by) {
+ return array_key_exists($by, $this->first());
+ }
/**
* Filter the list to include items with these charactaristics
View
16 model/DataList.php
@@ -6,7 +6,7 @@
* @package sapphire
* @subpackage model
*/
-class DataList extends ViewableData implements SS_List {
+class DataList extends ViewableData implements SS_List, SS_Limitable {
/**
* The DataObject class name that this data list is querying
*
@@ -145,8 +145,11 @@ public function join($join) {
*
* @param string $limit
*/
- public function limit($limit) {
- $this->dataQuery->limit($limit);
+ public function limit($limit, $offset = 0) {
+ if(!is_numeric($limit)) {
+ Deprecation::notice('3.0', 'Please pass limits as 2 arguments, rather than an array or SQL fragment.');
+ }
+ $this->dataQuery->limit($limit, $offset);
return $this;
}
@@ -545,7 +548,8 @@ public function exists() {
* @return DataList
*/
public function getRange($offset, $length) {
- return $this->limit(array('start' => $offset, 'limit' => $length));
+ Deprecation::notice("3.0", 'getRange($offset, $length) is deprecated. Use limit($length, $offset) instead. Note the new argument order.');
+ return $this->limit($length, $offset);
}
/**
@@ -813,7 +817,7 @@ public function removeDuplicates() {
* @return bool
*/
public function offsetExists($key) {
- return ($this->getRange($key, 1)->First() != null);
+ return ($this->limit(1,$key)->First() != null);
}
/**
@@ -823,7 +827,7 @@ public function offsetExists($key) {
* @return DataObject
*/
public function offsetGet($key) {
- return $this->getRange($key, 1)->First();
+ return $this->limit(1, $key)->First();
}
/**
View
4 model/DataQuery.php
@@ -373,9 +373,9 @@ function sort($sort) {
/**
* Set the limit of this query
*/
- function limit($limit) {
+ function limit($limit, $offset = 0) {
$clone = $this;
- $clone->query->limit($limit);
+ $clone->query->limit($limit, $offset);
return $clone;
}
View
19 model/Limitable.php
@@ -0,0 +1,19 @@
+<?php
+
+/**
+ * Additional interface for {@link SS_List} classes that are limitable - able to have a subset of the list extracted.
+ *
+ * @see SS_List, SS_Sortable, SS_Filterable
+ */
+interface SS_Limitable {
+
+ /**
+ * Returns a filtered version of this where no more than $limit records are included.
+ * If $offset is specified, then that many records at the beginning of the list will be skipped.
+ * This matches the behaviour of the SQL LIMIT clause.
+ *
+ * @return SS_List
+ */
+ public function limit($limit, $offset = 0);
+
+}
View
9 model/List.php
@@ -22,15 +22,6 @@ public function toArray();
public function toNestedArray();
/**
- * Returns a subset of the items within the list.
- *
- * @param int $offset
- * @param int $length
- * @return SS_List
- */
- public function getRange($offset, $length);
-
- /**
* Adds an item to the list, making no guarantees about where it will
* appear.
*
View
10 model/ListDecorator.php
@@ -7,7 +7,7 @@
* @package sapphire
* @subpackage model
*/
-abstract class SS_ListDecorator extends ViewableData implements SS_List {
+abstract class SS_ListDecorator extends ViewableData implements SS_List, S_Limitable {
protected $list;
@@ -61,10 +61,6 @@ public function remove($itemObject) {
$this->list->remove($itemObject);
}
- public function getRange($offset, $length) {
- return $this->list->getRange($offset, $length);
- }
-
public function getIterator() {
return $this->list->getIterator();
}
@@ -136,6 +132,10 @@ public function filter(){
return call_user_func_array(array($this->list, 'filter'), $args);
}
+ public function limit($length, $offset) {
+ return $this->list->getRange($length, $offset);
+ }
+
/**
* Exclude the list to not contain items with these charactaristics
*
View
8 model/SQLQuery.php
@@ -252,10 +252,10 @@ public function queriedTables() {
* @param string|array $limit
* @return SQLQuery This instance
*/
- public function limit($limit) {
+ public function limit($limit, $offset = 0) {
if($limit && is_numeric($limit)) {
$this->limit = array(
- 'start' => 0,
+ 'start' => $offset,
'limit' => $limit,
);
} else if($limit && is_string($limit)) {
@@ -667,7 +667,7 @@ function aggregate($columns) {
function firstRow() {
$query = clone $this;
$offset = $this->limit ? $this->limit['start'] : 0;
- $query->limit(array('start' => $offset, 'limit' => 1));
+ $query->limit(1, $offset);
return $query;
}
@@ -677,7 +677,7 @@ function firstRow() {
function lastRow() {
$query = clone $this;
$offset = $this->limit ? $this->limit['start'] : 0;
- $query->limit(array('start' => $this->count() + $offset - 1, 'limit' => 1));
+ $query->limit(1, $this->count() + $offset - 1);
return $query;
}
View
4 tests/model/ArrayListTest.php
@@ -61,11 +61,11 @@ public function testToNestedArray() {
));
}
- public function testGetRange() {
+ public function testLimit() {
$list = new ArrayList(array(
array('Key' => 1), array('Key' => 2), array('Key' => 3)
));
- $this->assertEquals($list->getRange(1, 2)->toArray(), array(
+ $this->assertEquals($list->limit(2,1)->toArray(), array(
array('Key' => 2), array('Key' => 3)
));
}
View
2  tests/model/DataListTest.php
@@ -50,7 +50,7 @@ function testListCreationSortAndLimit() {
$this->assertEquals(array('Bob', 'Joe', 'Phil'), $list->column('Name'));
// We can also restrict the output to a range
- $this->assertEquals(array('Joe', 'Phil'), $list->getRange(1,2)->column('Name'));
+ $this->assertEquals(array('Joe', 'Phil'), $list->limit(2, 1)->column('Name'));
}
function testDataClass() {
View
16 tests/model/DataObjectTest.php
@@ -197,18 +197,12 @@ function testLimitAndCount() {
// There's 4 records in total
$this->assertEquals(4, $players->count());
- // Testing "## offset ##" syntax
- $this->assertEquals(4, $players->limit("20 OFFSET 0")->count());
- $this->assertEquals(0, $players->limit("20 OFFSET 20")->count());
- $this->assertEquals(2, $players->limit("2 OFFSET 0")->count());
- $this->assertEquals(1, $players->limit("5 OFFSET 3")->count());
-
// Testing "##, ##" syntax
- $this->assertEquals(4, $players->limit("20")->count());
- $this->assertEquals(4, $players->limit("0, 20")->count());
- $this->assertEquals(0, $players->limit("20, 20")->count());
- $this->assertEquals(2, $players->limit("0, 2")->count());
- $this->assertEquals(1, $players->limit("3, 5")->count());
+ $this->assertEquals(4, $players->limit(20)->count());
+ $this->assertEquals(4, $players->limit(20, 0)->count());
+ $this->assertEquals(0, $players->limit(20, 20)->count());
+ $this->assertEquals(2, $players->limit(2, 0)->count());
+ $this->assertEquals(1, $players->limit(5, 3)->count());
}
/**
View
10 tests/model/SQLQueryTest.php
@@ -87,19 +87,13 @@ function testSelectWithLimitClause() {
// numeric limit
$query = new SQLQuery();
$query->from[] = "MyTable";
- $query->limit("99");
+ $query->limit(99);
$this->assertEquals("SELECT * FROM MyTable LIMIT 99", $query->sql());
- // array limit
- $query = new SQLQuery();
- $query->from[] = "MyTable";
- $query->limit(array('limit'=>99));
- $this->assertEquals("SELECT * FROM MyTable LIMIT 99", $query->sql());
-
// array limit with start (MySQL specific)
$query = new SQLQuery();
$query->from[] = "MyTable";
- $query->limit(array('limit'=>99, 'start'=>97));
+ $query->limit(99, 97);
$this->assertEquals("SELECT * FROM MyTable LIMIT 99 OFFSET 97", $query->sql());
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.