Skip to content

Commit

Permalink
Version 5.4.0 (#21)
Browse files Browse the repository at this point in the history
* pagination resets

* version bump
  • Loading branch information
janhartigan authored Dec 1, 2017
1 parent c0274b5 commit 70678ab
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 13 deletions.
2 changes: 1 addition & 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.3.2
- **Version:** 5.4.0

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

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.4.0
- Automatically resetting the page to 1 whenever a filter, term, sort, or grouping changes. Same for facets with the other filter/sort changes.

### 5.3.2
- Fixing the exists query request applier example

Expand Down
47 changes: 45 additions & 2 deletions src/Facet.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class Facet {
*/
protected $excludesOwnFilters = true;

/**
* Determines if the page should reset when filter/sort changes
*
* @var bool
*/
protected $pageShouldAutomaticallyReset = true;

/**
* @param string $field
*/
Expand All @@ -49,10 +56,10 @@ public function __construct(array $values)
$this->setField($values['field']);
$this->setSortType($values['sortType']);
$this->setSortDirection($values['sortDirection']);
$this->setPage($values['page']);
$this->setLimit($values['limit']);
$this->setMinimumCount($values['minimumCount']);
$this->setExcludesOwnFilters($values['excludesOwnFilters']);
$this->setPage($values['page']);
$this->setLimit($values['limit']);
}

/**
Expand Down Expand Up @@ -174,6 +181,9 @@ public function setSortType($type)

$this->sortType = $type;

if ($this->pageShouldAutomaticallyReset)
$this->page(1);

return $this;
}

Expand All @@ -189,6 +199,9 @@ public function setSortDirection($direction)

$this->sortDirection = $direction;

if ($this->pageShouldAutomaticallyReset)
$this->page(1);

return $this;
}

Expand Down Expand Up @@ -268,6 +281,9 @@ public function setMinimumCount($minimumCount)

$this->minimumCount = (int) $minimumCount;

if ($this->pageShouldAutomaticallyReset)
$this->page(1);

return $this;
}

Expand Down Expand Up @@ -296,6 +312,33 @@ public function setExcludesOwnFilters($value)
{
$this->excludesOwnFilters = (bool) $value;

if ($this->pageShouldAutomaticallyReset)
$this->page(1);

return $this;
}

/**
* Disables automatic page resetting
*
* @return $this
*/
public function disableAutomaticPageReset()
{
$this->pageShouldAutomaticallyReset = false;

return $this;
}

/**
* Enables automatic page resetting
*
* @return $this
*/
public function enableAutomaticPageReset()
{
$this->pageShouldAutomaticallyReset = true;

return $this;
}

Expand Down
51 changes: 48 additions & 3 deletions src/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ class SearchRequest {
*/
protected $filterSet;

/**
* Determines if the page should reset when filter/group/sort changes
*
* @var bool
*/
protected $pageShouldAutomaticallyReset = true;

/**
* @param mixed $json //null | string
*/
Expand Down Expand Up @@ -104,12 +111,12 @@ public function overrideWithJson($json)
$this->groups = [];
$this->term = $inputs['term'];
$this->selects = $inputs['selects'];
$this->page = $inputs['page'];
$this->limit = $inputs['limit'];
$this->addSortsFromArray($inputs['sorts']);
$this->addFacets($inputs['facets']);
$this->groupBy($inputs['groups']);
$this->addFilterSetFromArray($inputs['filterSet']);
$this->page = $inputs['page'];
$this->limit = $inputs['limit'];
}

/**
Expand Down Expand Up @@ -195,6 +202,9 @@ public function term($term)

$this->term = $term;

if ($this->pageShouldAutomaticallyReset)
$this->page(1);

return $this;
}

Expand All @@ -218,7 +228,9 @@ public function getTerm()
*/
public function sort($field, $direction = 'asc')
{
$this->sorts = [new Sort($field, $direction)];
$this->sorts = [];

$this->addSort($field, $direction);

return $this;
}
Expand All @@ -235,6 +247,9 @@ public function addSort($field, $direction = 'asc')
{
$this->sorts[] = new Sort($field, $direction);

if ($this->pageShouldAutomaticallyReset)
$this->page(1);

return $this;
}

Expand Down Expand Up @@ -346,6 +361,9 @@ public function groupBy($field)

$this->groups = array_merge($this->groups, (array) $field);

if ($this->pageShouldAutomaticallyReset)
$this->page(1);

return $this;
}

Expand Down Expand Up @@ -439,6 +457,30 @@ public function getSkip()
return ($this->page - 1) * $this->limit;
}

/**
* Disables automatic page resetting
*
* @return $this
*/
public function disableAutomaticPageReset()
{
$this->pageShouldAutomaticallyReset = false;

return $this;
}

/**
* Enables automatic page resetting
*
* @return $this
*/
public function enableAutomaticPageReset()
{
$this->pageShouldAutomaticallyReset = true;

return $this;
}

/**
* Gets the top-level filter set
*
Expand Down Expand Up @@ -594,6 +636,9 @@ protected function isFilterSetPassthrough($method)
$isFilterFetcher = strpos($method, 'getFilter') !== false;
$isFilterRemover = strpos($method, 'removeFilter') !== false;

if ($isWhere && $this->pageShouldAutomaticallyReset)
$this->page(1);

return $isWhere || $isFilterFetcher || $isFilterRemover;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/DeepClone/DeepCloneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public function deepClone()
{
$originalRequest = SearchRequest::create();

$originalRequest->page(2)->limit(10)
->term('something')
$originalRequest->term('something')
->select('something')
->where('something', true)
->addSort('something', 'asc')
->groupBy('something')
->facet('something')->page(2)->limit(100)->sortByCount()->setSortDirection('desc')->setMinimumCount(5)->includeOwnFilters();
->page(2)->limit(10)
->facet('something')->sortByCount()->setSortDirection('desc')->setMinimumCount(5)->includeOwnFilters()->page(2)->limit(100);

$newRequest = clone $originalRequest;
$originalRequest->where('somethingElse', false);
Expand Down
62 changes: 61 additions & 1 deletion tests/Facet/FacetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,70 @@ public function getFacetByName()
'sortType' => 'value',
'sortDirection' => 'asc',
'page' => 1,
'limit' => 10,
'limit' => 10,
'minimumCount' => 1,
'excludesOwnFilters' => true,
], $facet->toArray());
}

/**
* @test
*/
public function paginationResetsByDefault()
{
$request = new SearchRequest;
$facet = $request->facet('someField');

$facet->page(5)->sortByCount();
$this->assertEquals(1, $facet->getPage());

$facet->page(5)->sortByValue();
$this->assertEquals(1, $facet->getPage());

$facet->page(5)->setMinimumCount(5);
$this->assertEquals(1, $facet->getPage());

$facet->page(5)->excludeOwnFilters();
$this->assertEquals(1, $facet->getPage());

$facet->page(5)->includeOwnFilters();
$this->assertEquals(1, $facet->getPage());
}

/**
* @test
*/
public function noPaginationResetsWhenDisabled()
{
$request = new SearchRequest;
$facet = $request->facet('someField')->disableAutomaticPageReset();

$facet->page(5)->sortByCount();
$this->assertEquals(5, $facet->getPage());

$facet->page(5)->sortByValue();
$this->assertEquals(5, $facet->getPage());

$facet->page(5)->setMinimumCount(5);
$this->assertEquals(5, $facet->getPage());

$facet->page(5)->excludeOwnFilters();
$this->assertEquals(5, $facet->getPage());

$facet->page(5)->includeOwnFilters();
$this->assertEquals(5, $facet->getPage());
}

/**
* @test
*/
public function paginationResetsWhenReenabled()
{
$request = new SearchRequest;
$facet = $request->facet('someField')->disableAutomaticPageReset()->enableAutomaticPageReset();

$facet->page(5)->sortByCount();
$this->assertEquals(1, $facet->getPage());
}

}
6 changes: 3 additions & 3 deletions tests/Json/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ protected function getExpectedRequest()
{
$request = new SearchRequest;

$request->page(5)->limit(50)
->term('search this')
$request->term('search this')
->select(['field1', 'field2'])
->addSort('something', 'asc')->addSort('otherThing', 'desc')
->groupBy('field')->groupBy('anotherField')
->where('fun', 'more')->orWhere(function($filterSet)
{
$filterSet->where('hats', '>', 'large')->where('butts', 'small');
})
->facet('something')->page(2)->limit(100)->sortByCount()->setSortDirection('desc')->setMinimumCount(5)->includeOwnFilters();
->page(5)->limit(50)
->facet('something')->sortByCount()->setSortDirection('desc')->setMinimumCount(5)->includeOwnFilters()->page(2)->limit(100);

return $request;
}
Expand Down
51 changes: 51 additions & 0 deletions tests/Pagination/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,55 @@ public function integerStrings()
$this->assertEquals(200, $request->getSkip());
}

/**
* @test
*/
public function resetsByDefault()
{
$request = new SearchRequest;

$request->page(5)->where('foo', true);
$this->assertEquals(1, $request->getPage());

$request->page(5)->sort('foo', 'desc');
$this->assertEquals(1, $request->getPage());

$request->page(5)->groupBy('foo');
$this->assertEquals(1, $request->getPage());

$request->page(5)->term('foo');
$this->assertEquals(1, $request->getPage());
}

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

$request->page(5)->where('foo', true);
$this->assertEquals(5, $request->getPage());

$request->page(5)->sort('foo', 'desc');
$this->assertEquals(5, $request->getPage());

$request->page(5)->groupBy('foo');
$this->assertEquals(5, $request->getPage());

$request->page(5)->term('foo');
$this->assertEquals(5, $request->getPage());
}

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

$request->page(5)->where('foo', true);
$this->assertEquals(1, $request->getPage());
}

}

0 comments on commit 70678ab

Please sign in to comment.