Skip to content

Commit

Permalink
Merge pull request #133 from getherbie/2.x-develop-remove-order-callback
Browse files Browse the repository at this point in the history
fix: remove not working ordering by a callback
  • Loading branch information
tbreuss authored Jan 14, 2023
2 parents 08f1c93 + 0cc9f61 commit 045f7cc
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 18 deletions.
2 changes: 1 addition & 1 deletion plugins/twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ protected function createLink(string $route, string $label, array $attribs = [])
*/
public function menuList(
array|string $where = '',
callable|string $order = '',
string $order = '',
bool $shuffle = false,
int $limit = 10,
string $template = '@snippet/menu_list.twig'
Expand Down
9 changes: 2 additions & 7 deletions system/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ final class QueryBuilder implements IteratorAggregate
private array $where;
private int $limit;
private int $offset;
/** @var callable|string $order */
private $order;
private string $order;
private array $data;
private array $processed;

Expand Down Expand Up @@ -160,7 +159,7 @@ public function offset(int $offset): self
return $this;
}

public function order(callable|string $order): self
public function order(string $order): self
{
$this->order = $order;
return $this;
Expand Down Expand Up @@ -204,10 +203,6 @@ private function processData(): void

private function sort(): bool
{
if (is_callable($this->order)) {
return uasort($this->data, $this->order);
}

if (trim($this->order, '-+') === '') {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/website/WebsiteCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ public function testDocIndepthQueryBuilder(AcceptanceTester $I)
$I->see('Query Builder', 'h1');
$I->see('On this page', '.toc-title');
$I->seeNumberOfElements('.content>h2', 7);
$I->seeNumberOfElements('.content>p', 31);
$I->seeNumberOfElements('.content>pre', 21);
$I->seeNumberOfElements('.content>p', 30);
$I->seeNumberOfElements('.content>pre', 20);
$I->seeNumberOfElements('.content>table', 1);
}

Expand Down
10 changes: 2 additions & 8 deletions website/site/pages/2-doc/5-indepth/5-query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ The AND/OR where clause operators can be nested:
## Order

The results can be ordered using the `order` method.
The argument can be a callback or a field name preceded by a plus or minus sign, where plus means ascending and minus means descending.
The argument can be a field name preceded by a plus or minus sign, where plus means ascending and minus means descending.
If the plus or minus sign is omitted, the order is ascending by default.
Here are examples of it's usage.
Here are examples of its usage.

Ordered by title descending:

Expand All @@ -182,12 +182,6 @@ Ordered by title ascending:
{{ '{{' }} query(data).order("+title") {{ '}}' }}
~~~

Ordered by title using a callback:

~~~twig
{{ '{{' }} query(data).order((a, b) => a.title <=> b.title) {{ '}}' }}
~~~

## Limit

You may limit the results by using the `limit` method:
Expand Down

0 comments on commit 045f7cc

Please sign in to comment.