1 | <?php |
||
37 | class Query |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * Checks if the property exists. |
||
42 | */ |
||
43 | const OP_EX = ''; |
||
44 | |||
45 | /** |
||
46 | * Checks if the property does not exist. |
||
47 | */ |
||
48 | const OP_NEX = '-'; |
||
49 | |||
50 | /** |
||
51 | * Checks if the property equals a certain value. |
||
52 | */ |
||
53 | const OP_EQ = '='; |
||
54 | |||
55 | /** |
||
56 | * Checks if the property is less than a certain value. |
||
57 | */ |
||
58 | const OP_LT = '<'; |
||
59 | |||
60 | /** |
||
61 | * Checks if the property is greater than a certain value. |
||
62 | */ |
||
63 | const OP_GT = '>'; |
||
64 | |||
65 | /** |
||
66 | * @var array<string,string|null>[] An array of the words forming the query. |
||
67 | * Each value is an array with the first member being the predicate |
||
68 | * (operator and name), and the second member being the value |
||
69 | * for the predicate. |
||
70 | */ |
||
71 | protected $words = array(); |
||
72 | |||
73 | /** |
||
74 | * This class is not to be instantiated normally, but by static methods |
||
75 | * instead. Use {@link static::where()} to create an instance of it. |
||
76 | */ |
||
77 | protected function __construct() |
||
81 | |||
82 | /** |
||
83 | * Sanitizes the operator of a condition. |
||
84 | * |
||
85 | * @param string $operator The operator to sanitize. |
||
86 | * |
||
87 | * @return string The sanitized operator. |
||
88 | */ |
||
89 | protected static function sanitizeOperator($operator) |
||
108 | |||
109 | /** |
||
110 | * Creates a new query with an initial condition. |
||
111 | * |
||
112 | * @param string $name The name of the property to test. |
||
113 | * @param string|resource|null $value Value of the property as a string |
||
114 | * or seekable stream. Not required for existence tests. |
||
115 | * If a seekable stream is provided, it is sent from its current |
||
116 | * position to its end, and the pointer is seeked back to its current |
||
117 | * position after sending. |
||
118 | * Non seekable streams, as well as all other types, are casted to a |
||
119 | * string. |
||
120 | * @param string $operator One of the OP_* constants. |
||
121 | * Describes the operation to perform. |
||
122 | * |
||
123 | * @return static A new query object. |
||
124 | */ |
||
125 | public static function where( |
||
133 | |||
134 | /** |
||
135 | * Negates the query. |
||
136 | * |
||
137 | * @return $this The query object. |
||
138 | */ |
||
139 | public function not() |
||
144 | |||
145 | /** |
||
146 | * Adds a condition as an alternative to the query. |
||
147 | * |
||
148 | * @param string $name The name of the property to test. |
||
149 | * @param string|resource|null $value Value of the property as a string |
||
150 | * or seekable stream. Not required for existence tests. |
||
151 | * If a seekable stream is provided, it is sent from its current |
||
152 | * position to its end, and the pointer is seeked back to its current |
||
153 | * position after sending. |
||
154 | * Non seekable streams, as well as all other types, are casted to a |
||
155 | * string. |
||
156 | * @param string $operator One of the OP_* constants. |
||
157 | * Describes the operation to perform. |
||
158 | * |
||
159 | * @return $this The query object. |
||
160 | */ |
||
161 | public function orWhere($name, $value = null, $operator = self::OP_EX) |
||
166 | |||
167 | /** |
||
168 | * Adds a condition in addition to the query. |
||
169 | * |
||
170 | * @param string $name The name of the property to test. |
||
171 | * @param string|resource|null $value Value of the property as a string |
||
172 | * or seekable stream. Not required for existence tests. |
||
173 | * If a seekable stream is provided, it is sent from its current |
||
174 | * position to its end, and the pointer is seeked back to its current |
||
175 | * position after sending. |
||
176 | * Non seekable streams, as well as all other types, are casted to a |
||
177 | * string. |
||
178 | * @param string $operator One of the OP_* constants. |
||
179 | * Describes the operation to perform. |
||
180 | * |
||
181 | * @return $this The query object. |
||
182 | */ |
||
183 | public function andWhere($name, $value = null, $operator = self::OP_EX) |
||
188 | |||
189 | /** |
||
190 | * Sends the query over a communicator. |
||
191 | * |
||
192 | * @param Communicator $com The communicator to send the query over. |
||
193 | * |
||
194 | * @return int The number of bytes sent. |
||
195 | */ |
||
196 | public function send(Communicator $com) |
||
206 | |||
207 | /** |
||
208 | * Sends the query over a communicator. |
||
209 | * |
||
210 | * The only difference with the non private equivalent is that this one does |
||
211 | * not do locking. |
||
212 | * |
||
213 | * @param Communicator $com The communicator to send the query over. |
||
214 | * |
||
215 | * @return int The number of bytes sent. |
||
216 | */ |
||
217 | private function _send(Communicator $com) |
||
242 | |||
243 | /** |
||
244 | * Verifies the query. |
||
245 | * |
||
246 | * Verifies the query against a communicator, i.e. whether the query |
||
247 | * could successfully be sent (assuming the connection is still opened). |
||
248 | * |
||
249 | * @param Communicator $com The Communicator to check against. |
||
250 | * |
||
251 | * @return $this The query object itself. |
||
252 | * |
||
253 | * @throws LengthException If the resulting length of an API word is not |
||
254 | * supported. |
||
255 | */ |
||
256 | public function verify(Communicator $com) |
||
275 | |||
276 | /** |
||
277 | * Adds a condition. |
||
278 | * |
||
279 | * @param string $name The name of the property to test. |
||
280 | * @param string|resource|null $value Value of the property as a string |
||
281 | * or seekable stream. Not required for existence tests. |
||
282 | * If a seekable stream is provided, it is sent from its current |
||
283 | * position to its end, and the pointer is seeked back to its current |
||
284 | * position after sending. |
||
285 | * Non seekable streams, as well as all other types, are casted to a |
||
286 | * string. |
||
287 | * @param string $operator One of the ACTION_* constants. |
||
288 | * Describes the operation to perform. |
||
289 | * |
||
290 | * @return $this The query object. |
||
291 | */ |
||
292 | protected function addWhere($name, $value, $operator) |
||
301 | } |
||
302 |