Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly apply given default sort #241

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/Compat/CompatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlString;
use ipl\Html\ValidHtml;
use ipl\Orm\Common\SortUtil;
use ipl\Orm\Query;
use ipl\Stdlib\Contract\Paginatable;
use ipl\Web\Control\LimitControl;
Expand Down Expand Up @@ -294,15 +295,25 @@

$this->params->shift($sortControl->getSortParam());

$sortControl->handleRequest($this->getServerRequest());

$defaultSort = null;

if (func_num_args() === 3) {
$defaultSort = func_get_args()[2];
}

return $sortControl->apply($query, $defaultSort);
$default = $defaultSort ?? $query->getModel()->getDefaultSort();
if (! empty($default)) {
$sortControl->setDefault(SortUtil::normalizeSortSpec($default));

Check failure on line 305 in src/Compat/CompatController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $sort of static method ipl\Orm\Common\SortUtil::normalizeSortSpec() expects array|string, mixed given.

Check failure on line 305 in src/Compat/CompatController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $sort of static method ipl\Orm\Common\SortUtil::normalizeSortSpec() expects array|string, mixed given.

$columns = $sortControl->getColumns();
$default = $sortControl->getDefault();
if (! empty($defaultSort) && ! isset($columns[$default])) {
throw new InvalidArgumentException(sprintf('Invalid default sort "%s" given', $default));
}
}

$sortControl->handleRequest($this->getServerRequest());

return $sortControl->apply($query);
}

/**
Expand Down
8 changes: 1 addition & 7 deletions src/Control/SortControl.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in src/Control/SortControl.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Ignored error pattern #^Method ipl\\Web\\Control\\SortControl\:\:apply\(\) has parameter \$defaultSort with no type specified\.$# in path /home/runner/work/ipl-web/ipl-web/src/Control/SortControl.php was not matched in reported errors.

Check failure on line 1 in src/Control/SortControl.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Ignored error pattern #^PHPDoc tag @param has invalid value \(\?array\|string \$defaultSort\)\: Unexpected token "\|", expected variable at offset 111$# in path /home/runner/work/ipl-web/ipl-web/src/Control/SortControl.php was not matched in reported errors.

Check failure on line 1 in src/Control/SortControl.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$str of function strtolower expects string, mixed given\.$# in path /home/runner/work/ipl-web/ipl-web/src/Control/SortControl.php was not matched in reported errors.

Check failure on line 1 in src/Control/SortControl.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Ignored error pattern #^Part \$column \(mixed\) of encapsed string cannot be cast to string\.$# in path /home/runner/work/ipl-web/ipl-web/src/Control/SortControl.php was not matched in reported errors.

Check failure on line 1 in src/Control/SortControl.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Ignored error pattern #^Method ipl\\Web\\Control\\SortControl\:\:apply\(\) has parameter \$defaultSort with no type specified\.$# in path /home/runner/work/ipl-web/ipl-web/src/Control/SortControl.php was not matched in reported errors.

Check failure on line 1 in src/Control/SortControl.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Ignored error pattern #^PHPDoc tag @param has invalid value \(\?array\|string \$defaultSort\)\: Unexpected token "\|", expected variable at offset 111$# in path /home/runner/work/ipl-web/ipl-web/src/Control/SortControl.php was not matched in reported errors.

Check failure on line 1 in src/Control/SortControl.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$str of function strtolower expects string, mixed given\.$# in path /home/runner/work/ipl-web/ipl-web/src/Control/SortControl.php was not matched in reported errors.

Check failure on line 1 in src/Control/SortControl.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Ignored error pattern #^Part \$column \(mixed\) of encapsed string cannot be cast to string\.$# in path /home/runner/work/ipl-web/ipl-web/src/Control/SortControl.php was not matched in reported errors.

namespace ipl\Web\Control;

Expand Down Expand Up @@ -197,23 +197,17 @@
* Sort the given query according to the request
*
* @param Query $query
* @param ?array|string $defaultSort
*
* @return $this
*/
public function apply(Query $query, $defaultSort = null): self
public function apply(Query $query): self
{
if ($this->getRequest() === null) {
// handleRequest() has not been called yet
// TODO: Remove this once everything using this requires ipl v0.12.0
$this->handleRequest(ServerRequest::fromGlobals());
}

$default = $defaultSort ?? (array) $query->getModel()->getDefaultSort();
if (! empty($default)) {
$this->setDefault(SortUtil::normalizeSortSpec($default));
}

$sort = $this->getSort();
if (! empty($sort)) {
$query->orderBy(SortUtil::createOrderBy($sort));
Expand Down
Loading