-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPostgresOrderByProcessor.php
More file actions
66 lines (51 loc) · 1.95 KB
/
PostgresOrderByProcessor.php
File metadata and controls
66 lines (51 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* This file is part of the event-engine/php-postgres-document-store.
* (c) 2019-2021 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace EventEngine\DocumentStore\Postgres\OrderBy;
use EventEngine\DocumentStore;
use EventEngine\DocumentStore\OrderBy\OrderBy;
final class PostgresOrderByProcessor implements OrderByProcessor
{
/**
* @var bool
*/
private $useMetadataColumns;
public function __construct(bool $useMetadataColumns = false)
{
$this->useMetadataColumns = $useMetadataColumns;
}
public function process(OrderBy $orderBy): OrderByClause
{
[$orderByClause, $args] = $this->processOrderBy($orderBy);
return new OrderByClause($orderByClause, $args);
}
private function processOrderBy(OrderBy $orderBy): array
{
if($orderBy instanceof DocumentStore\OrderBy\AndOrder) {
[$sortA, $sortAArgs] = $this->processOrderBy($orderBy->a());
[$sortB, $sortBArgs] = $this->processOrderBy($orderBy->b());
return ["$sortA, $sortB", array_merge($sortAArgs, $sortBArgs)];
}
if ($orderBy instanceof DocumentStore\OrderBy\DocId) {
$direction = $orderBy->direction();
return ["id $direction", []];
}
/** @var DocumentStore\OrderBy\Asc|DocumentStore\OrderBy\Desc $orderBy */
$direction = $orderBy instanceof DocumentStore\OrderBy\Asc ? 'ASC' : 'DESC';
$prop = $this->propToJsonPath($orderBy->prop());
return ["{$prop} $direction", []];
}
private function propToJsonPath(string $field): string
{
if($this->useMetadataColumns && strpos($field, 'metadata.') === 0) {
return str_replace('metadata.', '', $field);
}
return "doc->'" . str_replace('.', "'->'", $field) . "'";
}
}