-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWhereClause.php
More file actions
96 lines (82 loc) · 2.76 KB
/
WhereClause.php
File metadata and controls
96 lines (82 loc) · 2.76 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
<?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 InvalidArgumentException;
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\BooleanType;
use WikibaseSolutions\CypherDSL\Utils\CastUtils;
/**
* This class represents a WHERE clause.
*
* WHERE adds constraints to the patterns in a MATCH or OPTIONAL MATCH clause or filters the results of a WITH clause.
*
* @see https://neo4j.com/docs/cypher-manual/current/clauses/where/
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 83)
* @see Query::where() for a more convenient method to construct this class
*/
final class WhereClause extends Clause
{
public const AND = 'and';
public const OR = 'or';
public const XOR = 'xor';
/**
* @var null|BooleanType The expression to match
*/
private ?BooleanType $expression = null;
/**
* Add an expression to this WHERE clause.
*
* @param bool|BooleanType $expression The expression to add to the WHERE clause
* @param string $operator The operator to use to combine the given expression with the existing expression, should
* be one of WhereClause::AND, WhereClause::OR or WhereClause::XOR
*
* @return $this
*/
public function addExpression(BooleanType|bool $expression, string $operator = self::AND): self
{
$expression = CastUtils::toBooleanType($expression);
if ($operator !== self::AND && $operator !== self::OR && $operator !== self::XOR) {
throw new InvalidArgumentException('$operator must either be "and", "xor" or "or"');
}
if ($this->expression === null) {
$this->expression = $expression;
} elseif ($operator === self::AND) {
$this->expression = $this->expression->and($expression);
} elseif ($operator === self::OR) {
$this->expression = $this->expression->or($expression);
} elseif ($operator === self::XOR) {
$this->expression = $this->expression->xor($expression);
}
return $this;
}
/**
* Returns the expression to match.
*/
public function getExpression(): ?BooleanType
{
return $this->expression;
}
/**
* @inheritDoc
*/
protected function getClause(): string
{
return "WHERE";
}
/**
* @inheritDoc
*/
protected function getSubject(): string
{
if (!isset($this->expression)) {
return "";
}
return $this->expression->toQuery();
}
}