-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathOrderByClause.php
More file actions
102 lines (88 loc) · 2.43 KB
/
OrderByClause.php
File metadata and controls
102 lines (88 loc) · 2.43 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php declare(strict_types=1);
/*
* This file is part of php-cypher-dsl.
*
* Copyright (C) Wikibase Solutions
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace WikibaseSolutions\CypherDSL\Clauses;
use WikibaseSolutions\CypherDSL\Expressions\Property;
use WikibaseSolutions\CypherDSL\Query;
/**
* This class represents an ORDER BY sub-clause.
*
* ORDER BY is a sub-clause following RETURN or WITH, and it specifies that the output should be sorted
* and how.
*
* TODO: Allow order modifier to be applied for each property (see #39).
*
* @see https://neo4j.com/docs/cypher-manual/current/clauses/order-by/
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 93)
* @see Query::orderBy() for a more convenient method to construct this class
*/
final class OrderByClause extends Clause
{
/**
* @var Property[] The expressions to include in the clause
*/
private array $properties = [];
/**
* @var bool Whether to add the DESC modifier to the clause
*/
private bool $descending = false;
/**
* Add one or more properties to sort on.
*
* @param Property ...$property The additional property to sort on
*/
public function addProperty(Property ...$property): self
{
$this->properties = array_merge($this->properties, $property);
return $this;
}
/**
* Set to sort in a DESCENDING order.
*/
public function setDescending(bool $descending = true): self
{
$this->descending = $descending;
return $this;
}
/**
* Returns the properties to order.
*
* @return Property[]
*/
public function getProperties(): array
{
return $this->properties;
}
/**
* Returns whether the ordering is in descending order.
*/
public function isDescending(): bool
{
return $this->descending;
}
/**
* @inheritDoc
*/
protected function getClause(): string
{
return "ORDER BY";
}
/**
* @inheritDoc
*/
protected function getSubject(): string
{
$properties = array_map(static fn (Property $property): string => $property->toQuery(), $this->properties);
$subject = implode(", ", $properties);
if ($this->descending) {
$subject .= ' DESCENDING';
}
return $subject;
}
}