-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EZP-31107: Implemented sorting specification
- Loading branch information
Showing
36 changed files
with
1,602 additions
and
234 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
39 changes: 39 additions & 0 deletions
39
eZ/Bundle/EzPublishCoreBundle/Resources/config/sort_spec.yml
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,39 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
public: false | ||
|
||
eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortClauseParserDispatcher: | ||
arguments: | ||
$parsers: !tagged_iterator ezplatform.query_type.sort_clause_parser | ||
|
||
eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortClauseParser\FieldSortClauseParser: | ||
tags: | ||
- { name: ezplatform.query_type.sort_clause_parser } | ||
|
||
eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortClauseParser\MapDistanceSortClauseParser: | ||
tags: | ||
- { name: ezplatform.query_type.sort_clause_parser } | ||
|
||
eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortClauseParser\RandomSortClauseParser: | ||
tags: | ||
- { name: ezplatform.query_type.sort_clause_parser } | ||
|
||
eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortClauseParser\DefaultSortClauseParser: | ||
arguments: | ||
$valueObjectClassMap: | ||
content_id: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\ContentId | ||
content_name: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\ContentName | ||
date_modified: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\DateModified | ||
date_published: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\DatePublished | ||
section_identifier: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\SectionIdentifier | ||
section_name: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\SectionName | ||
location_depth: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\Location\Depth | ||
location_id: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\Location\Id | ||
location_is_main: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\Location\IsMainLocation | ||
location_path: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\Location\Path | ||
location_priority: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\Location\Priority | ||
location_visibility: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\Location\Visibility | ||
tags: | ||
- { name: ezplatform.query_type.sort_clause_parser } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\Core\QueryType\BuildIn; | ||
|
||
use eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortClauseParserInterface; | ||
use eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortSpecLexer; | ||
use eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortSpecParser; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class SortClausesFactory implements SortClausesFactoryInterface | ||
{ | ||
/** @var \eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortClauseParserInterface */ | ||
private $sortClauseParser; | ||
|
||
public function __construct(SortClauseParserInterface $sortClauseArgsParser) | ||
{ | ||
$this->sortClauseParser = $sortClauseArgsParser; | ||
} | ||
|
||
/** | ||
* @throws \eZ\Publish\Core\QueryType\BuildIn\SortSpec\Exception\SyntaxErrorException | ||
* | ||
* @return \eZ\Publish\API\Repository\Values\Content\Query\SortClause[] | ||
*/ | ||
public function createFromSpecification(string $specification): array | ||
{ | ||
$lexer = new SortSpecLexer(); | ||
$lexer->tokenize($specification); | ||
|
||
$parser = new SortSpecParser($this->sortClauseParser, $lexer); | ||
|
||
return $parser->parseSortClausesList(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
eZ/Publish/Core/QueryType/BuildIn/SortClausesFactoryInterface.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,22 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\Core\QueryType\BuildIn; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
interface SortClausesFactoryInterface | ||
{ | ||
/** | ||
* @return \eZ\Publish\API\Repository\Values\Content\Query\SortClause[] | ||
* | ||
* @throws \eZ\Publish\Core\QueryType\BuildIn\SortSpec\Exception\SyntaxErrorException | ||
*/ | ||
public function createFromSpecification(string $specification): array; | ||
} |
29 changes: 29 additions & 0 deletions
29
eZ/Publish/Core/QueryType/BuildIn/SortSpec/Exception/SyntaxErrorException.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,29 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\Core\QueryType\BuildIn\SortSpec\Exception; | ||
|
||
use eZ\Publish\Core\QueryType\BuildIn\SortSpec\Token; | ||
use RuntimeException; | ||
|
||
final class SyntaxErrorException extends RuntimeException | ||
{ | ||
public static function fromUnexpectedToken(string $input, Token $token, array $expectedTypes): self | ||
{ | ||
$message = sprintf( | ||
'Error while parsing sorting specification: "%s": Unexpected token %s (%s) at position %d. Expected one of the following tokens: %s', | ||
$input, | ||
$token->getValue(), | ||
$token->getType(), | ||
$token->getPosition(), | ||
implode(' ', $expectedTypes) | ||
); | ||
|
||
return new self($message); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
eZ/Publish/Core/QueryType/BuildIn/SortSpec/Exception/UnsupportedSortClauseException.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,27 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\Core\QueryType\BuildIn\SortSpec\Exception; | ||
|
||
use eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortClauseParserInterface; | ||
use RuntimeException; | ||
use Throwable; | ||
|
||
final class UnsupportedSortClauseException extends RuntimeException | ||
{ | ||
public function __construct(string $name, $code = 0, Throwable $previous = null) | ||
{ | ||
$message = sprintf( | ||
'Could not find %s for %s sort clause', | ||
SortClauseParserInterface::class, | ||
$name | ||
); | ||
|
||
parent::__construct($message, $code, $previous); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
eZ/Publish/Core/QueryType/BuildIn/SortSpec/SortClauseParser/DefaultSortClauseParser.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,44 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortClauseParser; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Query\SortClause; | ||
use eZ\Publish\Core\QueryType\BuildIn\SortSpec\Exception\UnsupportedSortClauseException; | ||
use eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortClauseParserInterface; | ||
use eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortSpecParserInterface; | ||
|
||
/** | ||
* Parser for sort clauses which expect only sort direction in constructor parameter. | ||
*/ | ||
final class DefaultSortClauseParser implements SortClauseParserInterface | ||
{ | ||
/** @var string[] */ | ||
private $valueObjectClassMap; | ||
|
||
public function __construct(array $valueObjectClassMap) | ||
{ | ||
$this->valueObjectClassMap = $valueObjectClassMap; | ||
} | ||
|
||
public function parse(SortSpecParserInterface $parser, string $name): SortClause | ||
{ | ||
if (isset($this->valueObjectClassMap[$name])) { | ||
$class = $this->valueObjectClassMap[$name]; | ||
|
||
return new $class($parser->parseSortDirection()); | ||
} | ||
|
||
throw new UnsupportedSortClauseException($name); | ||
} | ||
|
||
public function supports(string $name): bool | ||
{ | ||
return isset($this->valueObjectClassMap[$name]); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
eZ/Publish/Core/QueryType/BuildIn/SortSpec/SortClauseParser/FieldSortClauseParser.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,39 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortClauseParser; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Query\SortClause; | ||
use eZ\Publish\API\Repository\Values\Content\Query\SortClause\Field; | ||
use eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortClauseParserInterface; | ||
use eZ\Publish\Core\QueryType\BuildIn\SortSpec\SortSpecParserInterface; | ||
use eZ\Publish\Core\QueryType\BuildIn\SortSpec\Token; | ||
|
||
/** | ||
* Parser for \eZ\Publish\API\Repository\Values\Content\Query\SortClause\Field sort clause. | ||
*/ | ||
final class FieldSortClauseParser implements SortClauseParserInterface | ||
{ | ||
private const SUPPORTED_CLAUSE_NAME = 'field'; | ||
|
||
public function parse(SortSpecParserInterface $parser, string $name): SortClause | ||
{ | ||
$args = []; | ||
$args[] = $parser->match(Token::TYPE_ID)->getValue(); | ||
$parser->match(Token::TYPE_DOT); | ||
$args[] = $parser->match(Token::TYPE_ID)->getValue(); | ||
$args[] = $parser->parseSortDirection(); | ||
|
||
return new Field(...$args); | ||
} | ||
|
||
public function supports(string $name): bool | ||
{ | ||
return $name === self::SUPPORTED_CLAUSE_NAME; | ||
} | ||
} |
Oops, something went wrong.