From 70678ab9ee38a87fd469050b78f6aac1bf46c68a Mon Sep 17 00:00:00 2001 From: Jan Hartigan Date: Fri, 1 Dec 2017 10:53:23 -0800 Subject: [PATCH] Version 5.4.0 (#21) * pagination resets * version bump --- README.md | 2 +- changelog.md | 3 ++ src/Facet.php | 47 +++++++++++++++++++++- src/SearchRequest.php | 51 ++++++++++++++++++++++-- tests/DeepClone/DeepCloneTest.php | 6 +-- tests/Facet/FacetTest.php | 62 ++++++++++++++++++++++++++++- tests/Json/JsonTest.php | 6 +-- tests/Pagination/PaginationTest.php | 51 ++++++++++++++++++++++++ 8 files changed, 215 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 94f24ca..759ef21 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/changelog.md b/changelog.md index 2cae058..7bb0877 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/src/Facet.php b/src/Facet.php index 7f2daf9..e80ccca 100644 --- a/src/Facet.php +++ b/src/Facet.php @@ -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 */ @@ -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']); } /** @@ -174,6 +181,9 @@ public function setSortType($type) $this->sortType = $type; + if ($this->pageShouldAutomaticallyReset) + $this->page(1); + return $this; } @@ -189,6 +199,9 @@ public function setSortDirection($direction) $this->sortDirection = $direction; + if ($this->pageShouldAutomaticallyReset) + $this->page(1); + return $this; } @@ -268,6 +281,9 @@ public function setMinimumCount($minimumCount) $this->minimumCount = (int) $minimumCount; + if ($this->pageShouldAutomaticallyReset) + $this->page(1); + return $this; } @@ -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; } diff --git a/src/SearchRequest.php b/src/SearchRequest.php index 2d0900f..585b0da 100644 --- a/src/SearchRequest.php +++ b/src/SearchRequest.php @@ -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 */ @@ -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']; } /** @@ -195,6 +202,9 @@ public function term($term) $this->term = $term; + if ($this->pageShouldAutomaticallyReset) + $this->page(1); + return $this; } @@ -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; } @@ -235,6 +247,9 @@ public function addSort($field, $direction = 'asc') { $this->sorts[] = new Sort($field, $direction); + if ($this->pageShouldAutomaticallyReset) + $this->page(1); + return $this; } @@ -346,6 +361,9 @@ public function groupBy($field) $this->groups = array_merge($this->groups, (array) $field); + if ($this->pageShouldAutomaticallyReset) + $this->page(1); + return $this; } @@ -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 * @@ -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; } diff --git a/tests/DeepClone/DeepCloneTest.php b/tests/DeepClone/DeepCloneTest.php index e16e8ba..6ca0e93 100644 --- a/tests/DeepClone/DeepCloneTest.php +++ b/tests/DeepClone/DeepCloneTest.php @@ -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); diff --git a/tests/Facet/FacetTest.php b/tests/Facet/FacetTest.php index 74527ae..8feba26 100644 --- a/tests/Facet/FacetTest.php +++ b/tests/Facet/FacetTest.php @@ -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()); + } + } \ No newline at end of file diff --git a/tests/Json/JsonTest.php b/tests/Json/JsonTest.php index 46e0827..0ce72e3 100644 --- a/tests/Json/JsonTest.php +++ b/tests/Json/JsonTest.php @@ -34,8 +34,7 @@ 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') @@ -43,7 +42,8 @@ protected function getExpectedRequest() { $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; } diff --git a/tests/Pagination/PaginationTest.php b/tests/Pagination/PaginationTest.php index 9938177..99bb4eb 100644 --- a/tests/Pagination/PaginationTest.php +++ b/tests/Pagination/PaginationTest.php @@ -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()); + } + } \ No newline at end of file