-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #310 [Core] Fix ordering not properly handled (sstok)
This PR was merged into the 2.0-dev branch. Discussion ---------- | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tickets | | License | MIT - Ordering direction was not included in exported conditions - String (view format) didn't support localized labels - The OrderStructureBuilder didn't use the correct transformer when changing fields; as this is configurable per field **IMPORTANT** ```PHP $fieldSet->add('@Date', OrderFieldType::class, ['case' => OrderTransformer::CASE_LOWERCASE, 'alias' => ['up' => 'ASC', 'down' => 'DESC'], 'default' => 'down']); $fieldSet->add('@id', OrderFieldType::class, ['default' => 'ASC']); ``` Previously the `@id` would contain the same transformer as the `@date` field (due to a bug in the `OrderStructureBuilder`), but this is incorrect behavior as each field has it's own configuration. If your current configuration stops working this is why. Commits ------- 7371077 [Core] Fix ordering not properly handled
- Loading branch information
Showing
19 changed files
with
525 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
lib/Core/Extension/Core/DataTransformer/OrderToLocalizedTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the RollerworksSearch package. | ||
* | ||
* (c) Sebastiaan Stok <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Rollerworks\Component\Search\Extension\Core\DataTransformer; | ||
|
||
use Rollerworks\Component\Search\DataTransformer; | ||
use Rollerworks\Component\Search\Exception\TransformationFailedException; | ||
|
||
final class OrderToLocalizedTransformer implements DataTransformer | ||
{ | ||
private array $alias; | ||
private array $viewLabel; | ||
private string $case; | ||
|
||
public function __construct(array $alias, array $viewLabel, string $case = OrderTransformer::CASE_UPPERCASE) | ||
{ | ||
$this->case = $case; | ||
$this->alias = $alias; | ||
$this->viewLabel = $viewLabel; | ||
} | ||
|
||
public function transform($value) | ||
{ | ||
if ($value === null) { | ||
return ''; | ||
} | ||
|
||
if (! \is_string($value)) { | ||
throw new TransformationFailedException('Expected a string or null.'); | ||
} | ||
|
||
switch ($this->case) { | ||
case OrderTransformer::CASE_LOWERCASE: | ||
$value = mb_strtolower($value); | ||
|
||
break; | ||
|
||
case OrderTransformer::CASE_UPPERCASE: | ||
$value = mb_strtoupper($value); | ||
|
||
break; | ||
} | ||
|
||
if (! isset($this->viewLabel[$value])) { | ||
throw new TransformationFailedException(sprintf('No localized label configured for "%s".', $value)); | ||
} | ||
|
||
return $this->viewLabel[$value]; | ||
} | ||
|
||
public function reverseTransform($value) | ||
{ | ||
if ($value !== null && ! \is_string($value)) { | ||
throw new TransformationFailedException('Expected a string or null.'); | ||
} | ||
|
||
if ($value === '') { | ||
return null; | ||
} | ||
|
||
switch ($this->case) { | ||
case OrderTransformer::CASE_LOWERCASE: | ||
$value = mb_strtolower($value); | ||
|
||
break; | ||
|
||
case OrderTransformer::CASE_UPPERCASE: | ||
$value = mb_strtoupper($value); | ||
|
||
break; | ||
} | ||
|
||
if (! isset($this->alias[$value])) { | ||
throw new TransformationFailedException( | ||
sprintf( | ||
'Invalid sort direction "%1$s" specified, expected one of: "%2$s"', | ||
$value, | ||
implode('", "', array_keys($this->alias)) | ||
), | ||
0, | ||
null, | ||
'This value is not a valid sorting direction. Accepted directions are "{{ directions }}".', | ||
['{{ directions }}' => mb_strtolower(implode('", "', array_unique(array_keys($this->alias))))] | ||
); | ||
} | ||
|
||
return $this->alias[$value]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.