Skip to content

Commit

Permalink
Adding unlimited pagination (#24)
Browse files Browse the repository at this point in the history
* adding unlimited

* add test

* update example

* readme / changelog

* add unlimited to toArray

* fix tests

* spelling

* setting limit or page falisfies unlimited

* fix toJson order

* use cleaner asserts
  • Loading branch information
sdsomma authored Dec 26, 2017
1 parent 69724c4 commit aff747a
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 4 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This library provides a set of classes that help represent requests for complex data and provides a way to convert requests to and from a standard JSON format. If you have interfaces with tons of parameters ($filters, $groupings, $page, $rowsPerPage, etc.), or if you're just looking for a standard way to communicate complex requests to other apps without racking your brain over how to represent this data in JSON, you will like this library.

- **Version:** 5.4.1
- **Version:** 5.5.0

[![Build Status](https://travis-ci.org/mongerinc/search-request.png?branch=master)](https://travis-ci.org/mongerinc/search-request)

Expand Down Expand Up @@ -137,6 +137,17 @@ $databaseQuery->take($limit)->skip(($page - 1) * $limit);

Alternatively, you can call `getSkip()` to avoid doing the calculation above.

If you want to ignore the pagination for a request, you can call the `unlimited()` or `all()` methods.

```php
$request = $request->unlimited();

if (!$request->isUnlimited())
$databaseQuery->take($request->getLimit())->skip($request->getSkip());
```

The unlimited feature is only a flag. Your database query applier must check for this with the `isUnlimited()` method to support this feature.

#### Filtering

Filtering a `SearchRequest` can be done using the `where()` method. An operator can be provided as the second argument where the possible types are `=`, `>`, `>=`, `<`, `<=`, `!=`, `in`, `not in`, `like`, `not like`, `regex`, `not regex`, `exists`, `not exists`, `between`, and `not between`. If no operator is provided, it is assumed to be `=`.
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

### 5.5.0
- Adding the unlimited flag for ignoring pagination

### 5.4.1
- Fixing `toJson()` for DateTime values

Expand Down
3 changes: 2 additions & 1 deletion examples/Illuminate/Applier.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public function apply(Query $query, SearchRequest $request)

$this->group($query, $request);

$query->forPage($request->getPage(), $request->getLimit());
if (!$request->isUnlimited())
$query->forPage($request->getPage(), $request->getLimit());
}

protected function select(Query $query, SearchRequest $request)
Expand Down
49 changes: 47 additions & 2 deletions src/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ class SearchRequest {
*/
protected $limit = 10;

/**
* Determines if pagination should be ignored
*
* @var bool
*/
protected $unlimited = false;

/**
* The list of applicable filters
*
Expand Down Expand Up @@ -115,8 +122,9 @@ public function overrideWithJson($json)
$this->addFacets($inputs['facets']);
$this->groupBy($inputs['groups']);
$this->addFilterSetFromArray($inputs['filterSet']);
$this->page = $inputs['page'];
$this->limit = $inputs['limit'];
$this->page($inputs['page']);
$this->limit($inputs['limit']);
$this->unlimited = isset($inputs['unlimited']) ? $inputs['unlimited'] : false;
}

/**
Expand Down Expand Up @@ -392,6 +400,7 @@ public function page($page)
throw new InvalidArgumentException("A page can only be a positive integer.");

$this->page = (int) $page;
$this->unlimited(false);

return $this;
}
Expand Down Expand Up @@ -423,10 +432,35 @@ public function limit($limit)
throw new InvalidArgumentException("A page row limit can only be a positive integer.");

$this->limit = (int) $limit;
$this->unlimited(false);

return $this;
}

/**
* Sets the unlimited flag
*
* @param bool $unlimited
*
* @return this
*/
public function unlimited($unlimited = true)
{
$this->unlimited = (bool) $unlimited;

return $this;
}

/**
* Alias for calling unlimited with true
*
* @return this
*/
public function all()
{
return $this->unlimited(true);
}

/**
* Gets the current page
*
Expand All @@ -447,6 +481,16 @@ public function getLimit()
return $this->limit;
}

/**
* Gets the unlimited flag
*
* @return bool
*/
public function isUnlimited()
{
return $this->unlimited;
}

/**
* Gets the current number of rows to skip
*
Expand Down Expand Up @@ -561,6 +605,7 @@ public function toArray()
'term' => $this->term,
'page' => $this->page,
'limit' => $this->limit,
'unlimited' => $this->unlimited,
'selects' => $this->selects,
'groups' => $this->groups,
'sorts' => array_map(function(Sort $sort) {return $sort->toArray();}, $this->sorts),
Expand Down
1 change: 1 addition & 0 deletions tests/DeepClone/DeepCloneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected function getExpectedJson()
'term' => 'something',
'page' => 2,
'limit' => 10,
'unlimited' => false,
'selects' => ['something'],
'groups' => ['something'],
'sorts' => [
Expand Down
37 changes: 37 additions & 0 deletions tests/Json/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function dates()
'term' => null,
'page' => 1,
'limit' => 10,
'unlimited' => false,
'selects' => [],
'groups' => [],
'sorts' => [],
Expand All @@ -60,6 +61,41 @@ public function dates()
]), $request->toJson());
}

/**
* @test
*/
public function unlimited()
{
$request = SearchRequest::create()->unlimited();

$this->assertEquals(json_encode([
'term' => null,
'page' => 1,
'limit' => 10,
'unlimited' => true,
'selects' => [],
'groups' => [],
'sorts' => [],
'filterSet' => [],
'filterSet' => [
'boolean' => 'and',
'filters' => []
],
'facets' => [],
]), $request->toJson());
}

/**
* @test
*/
public function unlimitedFromJson()
{
$originalRequest = SearchRequest::create()->limit(1)->unlimited();
$newRequest = SearchRequest::create($originalRequest->toJson());

$this->assertTrue($newRequest->isUnlimited());
}

/**
* Gets the expected search request
*
Expand Down Expand Up @@ -94,6 +130,7 @@ protected function getExpectedJson()
'term' => 'search this',
'page' => 5,
'limit' => 50,
'unlimited' => false,
'selects' => ['field1', 'field2'],
'groups' => ['field', 'anotherField'],
'sorts' => [
Expand Down
30 changes: 30 additions & 0 deletions tests/Pagination/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,34 @@ public function resetsWhenReenabled()
$this->assertEquals(1, $request->getPage());
}

/**
* @test
*/
public function unlimited()
{
$request = SearchRequest::create();
$this->assertFalse($request->isUnlimited());

$request->unlimited();
$this->assertTrue($request->isUnlimited());

$request->unlimited(false);
$this->assertFalse($request->isUnlimited());

$request->all();
$this->assertTrue($request->isUnlimited());
}

/**
* @test
*/
public function settingPaginationFalsifiesUnlimited()
{
$request = SearchRequest::create()->unlimited()->page(1);
$this->assertFalse($request->isUnlimited());

$request = SearchRequest::create()->unlimited()->limit(1);
$this->assertFalse($request->isUnlimited());
}

}

0 comments on commit aff747a

Please sign in to comment.