|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of Aura for PHP. |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Aura\SqlQuery\Common; |
|
10
|
|
|
|
|
11
|
|
|
use Aura\SqlQuery\Exception; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* |
|
15
|
|
|
* Common code for WHERE clauses. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Aura.SqlQuery |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
trait WhereTrait |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* Adds a WHERE condition to the query by AND. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $cond The WHERE condition. |
|
27
|
|
|
* |
|
28
|
|
|
* @param array $bind Values to be bound to placeholders |
|
29
|
|
|
* |
|
30
|
|
|
* @return $this |
|
31
|
|
|
* |
|
32
|
|
|
*/ |
|
33
|
65 |
|
public function where($cond, array $bind = []) |
|
34
|
|
|
{ |
|
35
|
65 |
|
$this->addClauseCondWithBind('where', 'AND', $cond, $bind); |
|
|
|
|
|
|
36
|
65 |
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* |
|
41
|
|
|
* Adds a WHERE condition to the query by OR. If the condition has |
|
42
|
|
|
* ?-placeholders, additional arguments to the method will be bound to |
|
43
|
|
|
* those placeholders sequentially. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $cond The WHERE condition. |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $bind Values to be bound to placeholders |
|
48
|
|
|
* |
|
49
|
|
|
* @return $this |
|
50
|
|
|
* |
|
51
|
|
|
* @see where() |
|
52
|
|
|
* |
|
53
|
|
|
*/ |
|
54
|
34 |
|
public function orWhere($cond, array $bind = []) |
|
55
|
|
|
{ |
|
56
|
34 |
|
$this->addClauseCondWithBind('where', 'OR', $cond, $bind); |
|
|
|
|
|
|
57
|
34 |
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* |
|
62
|
|
|
* Adds a WHERE condition with a prepared statement placeholder and value to the query by AND. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $cond the first part of the WHERE condition without the placeholder. e.g. "name = " or "name IN" |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $placeholder the placeholder as a string. e.g. ":NAME" or "(:NAMES)" |
|
67
|
|
|
* |
|
68
|
|
|
* @param (string|int|float|array) $value the value to be bound to the placeholder. e.g. "John" or ["John", "Eric", "Michael", "Terry"] |
|
69
|
|
|
* |
|
70
|
|
|
* @return $this |
|
71
|
|
|
* |
|
72
|
|
|
* @throws Exception |
|
73
|
|
|
* |
|
74
|
|
|
*/ |
|
75
|
5 |
|
public function whereBoundValue($cond, $placeholder, $value) |
|
76
|
|
|
{ |
|
77
|
5 |
|
$name = $this->extractNameOrThrow($placeholder); |
|
78
|
5 |
|
$this->addClauseCondWithBind('where', 'AND', $cond.$placeholder, [ $name => $value ] ); |
|
|
|
|
|
|
79
|
5 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* |
|
84
|
|
|
* Adds a WHERE condition with a prepared statement placeholder and value to the query by OR. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $cond the first part of the WHERE condition without the placeholder. e.g. "name = " or "name IN" |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $placeholder the placeholder as a string. e.g. ":NAME" or "(:NAMES)" |
|
89
|
|
|
* |
|
90
|
|
|
* @param (string|int|float|array) $value the value to be bound to the placeholder. e.g. "John" or ["John", "Eric", "Michael", "Terry"] |
|
91
|
|
|
* |
|
92
|
|
|
* @return $this |
|
93
|
|
|
* |
|
94
|
|
|
* @throws Exception |
|
95
|
|
|
* |
|
96
|
|
|
* @see whereBoundValue() |
|
97
|
|
|
* |
|
98
|
|
|
*/ |
|
99
|
|
|
public function orWhereBoundValue($cond, $placeholder, $value) |
|
100
|
|
|
{ |
|
101
|
|
|
$name = $this->extractNameOrThrow($placeholder); |
|
102
|
|
|
$this->addClauseCondWithBind('where', 'OR', $cond.$placeholder, [ $name => $value ] ); |
|
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Extract the name of PDO placeholders (e.g. "P") of the form ":P" for simple values and "(:P)" for array values. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $placeholder the placeholder specification |
|
110
|
|
|
* |
|
111
|
|
|
* @return the placeholder name as string |
|
112
|
|
|
* |
|
113
|
|
|
* @throws Exception |
|
114
|
|
|
* |
|
115
|
|
|
*/ |
|
116
|
5 |
|
protected static function extractNameOrThrow($placeholder) { |
|
117
|
5 |
|
$name = preg_replace( '/^\(?:([^\)]+)\)?$/', '\1', $placeholder); |
|
118
|
|
|
// XXX add type checks |
|
119
|
5 |
|
if (strlen($name)===strlen($placeholder)) { |
|
120
|
|
|
throw new Exception("Bad placeholder \"$name\""); |
|
121
|
|
|
} |
|
122
|
5 |
|
return $name; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.