-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionalQueryBuilder.php
More file actions
116 lines (100 loc) · 3.63 KB
/
ConditionalQueryBuilder.php
File metadata and controls
116 lines (100 loc) · 3.63 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/*
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2021 David Young
* @license https://github.com/opulencephp/Opulence/blob/1.2/LICENSE.md
*/
namespace Opulence\QueryBuilders;
/**
* Builds parts of a query that can use a "WHERE" clause
*/
class ConditionalQueryBuilder
{
/** @var array The list of WHERE expressions */
protected $whereConditions = [];
/**
* Adds a condition to a clause
*
* @param array $clauseConditions The list of conditions that already belong to the clause
* @param string $operation Either "AND" or "OR", indicating how this condition is being added to the list of conditions
* @param string ...$conditions A variable list of conditions to be met
* @return array The input array with the condition added
*/
public function addConditionToClause(array $clauseConditions, string $operation, string ...$conditions) : array
{
foreach ($conditions as $condition) {
$clauseConditions[] = ['operation' => $operation, 'condition' => $condition];
}
return $clauseConditions;
}
/**
* Adds to a "WHERE" condition that will be "AND"ed with other conditions
*
* @param string ...$condition A variable list of conditions to be met
* @return self For method chaining
*/
public function andWhere(string ...$condition) : self
{
$this->whereConditions = $this->addConditionToClause(
$this->whereConditions, 'AND', ...$condition);
return $this;
}
/**
* Gets the SQL that makes up a clause that permits boolean operations ie "AND" and "OR"
*
* @param string $conditionType The name of the condition type ie "WHERE"
* @param array $clauseConditions The array of condition data whose SQL we want
* @return string The SQL that makes up the input clause(s)
*/
public function getClauseConditionSql(string $conditionType, array $clauseConditions) : string
{
if (count($clauseConditions) === 0) {
return '';
}
$sql = ' ' . strtoupper($conditionType);
// This will help us keep track of whether or not we've added at least one clause
$haveAddedAClause = false;
foreach ($clauseConditions as $conditionData) {
$sql .= ($haveAddedAClause ? ' ' . strtoupper($conditionData['operation']) : '')
. " ({$conditionData['condition']})";
$haveAddedAClause = true;
}
return $sql;
}
/**
* @return array
*/
public function getWhereConditions() : array
{
return $this->whereConditions;
}
/**
* Adds to a "WHERE" condition that will be "OR"ed with other conditions
*
* @param string ...$condition A variable list of conditions to be met
* @return self For method chaining
*/
public function orWhere(string ...$condition) : self
{
$this->whereConditions = $this->addConditionToClause(
$this->whereConditions, 'OR', ...$condition);
return $this;
}
/**
* Starts a "WHERE" condition
* Only call this method once per query because it will overwrite any previously-set "WHERE" expressions
*
* @param string ...$condition A variable list of conditions to be met
* @return self For method chaining
*/
public function where(string ...$condition) : self
{
// We want to wipe out anything already in the condition list
$this->whereConditions = [];
$this->whereConditions = $this->addConditionToClause(
$this->whereConditions, 'AND', ...$condition);
return $this;
}
}