diff --git a/README.md b/README.md index 7df6280..6fa80ce 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A front-end control for sorting SilverStripe lists easily. The aim of this modul ## Requirements - * SilverStripe 4+ or 5+ + * SilverStripe ^6 ## Usage @@ -21,10 +21,10 @@ public function getSorter(){ 'Title', //DB field name only 'Popularity' => 'Popularity DESC', //map title to sort sql 'Price' => ['BasePrice' => 'ASC'], //map title to data list sort - ListSorter_Option::create('Age', ['Created' => 'DESC'], //object - ListSorter_Option::create('Age', ['Created' => 'ASC']) //reverse - ) - ; + ListSorterOption::create('Age', ['Created' => 'DESC']), //object + ListSorterOption::create('Age', ['Created' => 'ASC']) //reverse + ]; + return ListSorter::create($this->request,$sorts); } ``` diff --git a/composer.json b/composer.json index 3508691..7d0e75f 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ } ], "require": { - "silverstripe/framework": "^4.0 | ^5.0" + "silverstripe/framework": "^6" }, "extra": { "branch-alias": { diff --git a/src/ListSorter.php b/src/ListSorter.php index b10a734..e3b010b 100644 --- a/src/ListSorter.php +++ b/src/ListSorter.php @@ -1,19 +1,23 @@ sortOptions = []; foreach ($options as $key => $value) { if (is_numeric($key)) { $key = $value; } + if ($value instanceof ListSorterOption) { $this->addSortOption($value); } else { $this->addSortOption( - new ListSorterOption($key, $value) + ListSorterOption::create($key, $value) ); } } @@ -48,16 +53,15 @@ public function setSortOptions($options) /** * Add sort option, and set according to sort request param. - * - * @param ListSorterOption $option */ - public function addSortOption(ListSorterOption $option) + public function addSortOption(ListSorterOption $option): void { $this->sortOptions[(string)$option] = $option; $requestparam = $this->request->getVar('sort'); if ((string)$option === $requestparam) { $this->current = $option; } + if ((string)$option->getReverseOption() === $requestparam) { $this->current = $option->getReverseOption(); } @@ -78,12 +82,12 @@ protected function getCurrentOption() * * @param $option */ - public function setCurrentOption(ListSorterOption $option) + public function setCurrentOption(ListSorterOption $option): void { $this->current = $option; } - protected function isCurrent(ListSorterOption $option) + protected function isCurrent(ListSorterOption $option): bool { return $option === $this->getCurrentOption(); } @@ -99,8 +103,10 @@ public function getSorts() if ($option->isReversable()) { $option = $option->getReverseOption(); } + $option = $option->customise(['IsCurrent' => true]); } + $sorts->push($option); } @@ -116,7 +122,7 @@ public function getSorts() public function sortList($list) { if ($current = $this->getCurrentOption()) { - $list = $list->sort($current->getSortSet()); + return $list->sort($current->getSortSet()); } return $list; diff --git a/src/ListSorterOption.php b/src/ListSorterOption.php index 30c785b..616011e 100644 --- a/src/ListSorterOption.php +++ b/src/ListSorterOption.php @@ -1,19 +1,24 @@ title = $title; $this->setID($title); $this->sortSet = $sortset; - if ($reverseOption) { + if ($reverseOption instanceof \SilverShop\ListSorter\ListSorterOption) { $this->setReverseOption($reverseOption); } } @@ -31,7 +36,7 @@ public function getTitle() return $this->title; } - public function setTitle($title) + public function setTitle($title): static { $this->title = $title; return $this; @@ -42,15 +47,17 @@ public function getSortSet() return $this->sortSet; } - public function setReverseOption(ListSorterOption $option) + public function setReverseOption(ListSorterOption $option): static { $this->reverseOption = $option; if (!$option->isReversable()) { if ($this->getID() === $option->getID()) { - $option->setID((string)$option . "_rev"); + $option->setID($option . "_rev"); } + $option->setReverseOption($this); } + return $this; } @@ -59,12 +66,12 @@ public function getReverseOption() return $this->reverseOption; } - public function isReversable() + public function isReversable(): bool { return (bool)$this->reverseOption; } - public function setID($id) + public function setID($id): static { $this->id = strtolower(trim($id)); return $this; @@ -75,7 +82,7 @@ public function getID() return $this->id; } - public function __toString() + public function __toString(): string { return $this->id; } @@ -93,9 +100,8 @@ public function getLink() */ private function generateLink($id) { - $url = Http::setGetVar('sort', $id, null, '&'); //TODO: strip "start" pagination parameter, //as most users won't want to remain on paginated page when sorting - return $url; + return Http::setGetVar('sort', $id, null, '&'); } } diff --git a/tests/php/ListSorterTest.php b/tests/php/ListSorterTest.php index 5ce3288..8412b51 100644 --- a/tests/php/ListSorterTest.php +++ b/tests/php/ListSorterTest.php @@ -1,5 +1,7 @@ assertEquals('age title', $option->getID()); $this->assertEquals('age title', (string)$option); $this->assertTrue($option->isReversable()); - $this->assertEquals('/?colors=always&url=%2F&sort=age+title', $option->getLink()); + $this->assertEquals('/?url=%2F&sort=age+title', $option->getLink()); $reverse = $option->getReverseOption(); $this->assertEquals('Age Title', $reverse->getTitle()); $this->assertEquals('age title_rev', $reverse->getID()); $this->assertEquals('age title_rev', (string)$reverse); $this->assertTrue($reverse->isReversable()); - $this->assertEquals('/?colors=always&url=%2F&sort=age+title_rev', $reverse->getLink()); + $this->assertEquals('/?url=%2F&sort=age+title_rev', $reverse->getLink()); } } diff --git a/tests/php/Stubs/ListSorterPerson.php b/tests/php/Stubs/ListSorterPerson.php index e939029..6c1e7a2 100644 --- a/tests/php/Stubs/ListSorterPerson.php +++ b/tests/php/Stubs/ListSorterPerson.php @@ -1,5 +1,7 @@ 'Varchar', 'Age' => 'Int' ];