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 | * An array of the words forming the query. |
||
67 | * |
||
68 | * Each value is an array with the first member being the predicate |
||
69 | * (operator and name), and the second member being the value |
||
70 | * for the predicate. |
||
71 | * |
||
72 | * @var array<string,string|null>[] |
||
73 | */ |
||
74 | protected $words = array(); |
||
75 | |||
76 | /** |
||
77 | * This class is not to be instantiated normally, but by static methods |
||
78 | * instead. Use {@link static::where()} to create an instance of it. |
||
79 | */ |
||
80 | protected function __construct() |
||
84 | |||
85 | /** |
||
86 | * Sanitizes the operator of a condition. |
||
87 | * |
||
88 | * @param string $operator The operator to sanitize. |
||
89 | * |
||
90 | * @return string The sanitized operator. |
||
91 | */ |
||
92 | protected static function sanitizeOperator($operator) |
||
111 | |||
112 | /** |
||
113 | * Creates a new query with an initial condition. |
||
114 | * |
||
115 | * @param string $name The name of the property to test. |
||
116 | * @param string|resource|null $value Value of the property as a string |
||
117 | * or seekable stream. Not required for existence tests. |
||
118 | * If a seekable stream is provided, it is sent from its current |
||
119 | * position to its end, and the pointer is seeked back to its current |
||
120 | * position after sending. |
||
121 | * Non seekable streams, as well as all other types, are casted to a |
||
122 | * string. |
||
123 | * @param string $operator One of the OP_* constants. |
||
124 | * Describes the operation to perform. |
||
125 | * |
||
126 | * @return static A new query object. |
||
127 | */ |
||
128 | public static function where( |
||
136 | |||
137 | /** |
||
138 | * Negates the query. |
||
139 | * |
||
140 | * @return $this The query object. |
||
141 | */ |
||
142 | public function not() |
||
147 | |||
148 | /** |
||
149 | * Adds a condition as an alternative to the query. |
||
150 | * |
||
151 | * @param string $name The name of the property to test. |
||
152 | * @param string|resource|null $value Value of the property as a string |
||
153 | * or seekable stream. Not required for existence tests. |
||
154 | * If a seekable stream is provided, it is sent from its current |
||
155 | * position to its end, and the pointer is seeked back to its current |
||
156 | * position after sending. |
||
157 | * Non seekable streams, as well as all other types, are casted to a |
||
158 | * string. |
||
159 | * @param string $operator One of the OP_* constants. |
||
160 | * Describes the operation to perform. |
||
161 | * |
||
162 | * @return $this The query object. |
||
163 | */ |
||
164 | public function orWhere($name, $value = null, $operator = self::OP_EX) |
||
169 | |||
170 | /** |
||
171 | * Adds a condition in addition to the query. |
||
172 | * |
||
173 | * @param string $name The name of the property to test. |
||
174 | * @param string|resource|null $value Value of the property as a string |
||
175 | * or seekable stream. Not required for existence tests. |
||
176 | * If a seekable stream is provided, it is sent from its current |
||
177 | * position to its end, and the pointer is seeked back to its current |
||
178 | * position after sending. |
||
179 | * Non seekable streams, as well as all other types, are casted to a |
||
180 | * string. |
||
181 | * @param string $operator One of the OP_* constants. |
||
182 | * Describes the operation to perform. |
||
183 | * |
||
184 | * @return $this The query object. |
||
185 | */ |
||
186 | public function andWhere($name, $value = null, $operator = self::OP_EX) |
||
191 | |||
192 | /** |
||
193 | * Sends the query over a communicator. |
||
194 | * |
||
195 | * @param Communicator $com The communicator to send the query over. |
||
196 | * |
||
197 | * @return int The number of bytes sent. |
||
198 | */ |
||
199 | public function send(Communicator $com) |
||
209 | |||
210 | /** |
||
211 | * Sends the query over a communicator. |
||
212 | * |
||
213 | * The only difference with the non private equivalent is that this one does |
||
214 | * not do locking. |
||
215 | * |
||
216 | * @param Communicator $com The communicator to send the query over. |
||
217 | * |
||
218 | * @return int The number of bytes sent. |
||
219 | */ |
||
220 | private function _send(Communicator $com) |
||
241 | |||
242 | /** |
||
243 | * Verifies the query. |
||
244 | * |
||
245 | * Verifies the query against a communicator, i.e. whether the query |
||
246 | * could successfully be sent (assuming the connection is still opened). |
||
247 | * |
||
248 | * @param Communicator $com The Communicator to check against. |
||
249 | * |
||
250 | * @return $this The query object itself. |
||
251 | * |
||
252 | * @throws LengthException If the resulting length of an API word is not |
||
253 | * supported. |
||
254 | */ |
||
255 | public function verify(Communicator $com) |
||
274 | |||
275 | /** |
||
276 | * Adds a condition. |
||
277 | * |
||
278 | * @param string $name The name of the property to test. |
||
279 | * @param string|resource|null $value Value of the property as a string |
||
280 | * or seekable stream. Not required for existence tests. |
||
281 | * If a seekable stream is provided, it is sent from its current |
||
282 | * position to its end, and the pointer is seeked back to its current |
||
283 | * position after sending. |
||
284 | * Non seekable streams, as well as all other types, are casted to a |
||
285 | * string. |
||
286 | * @param string $operator One of the ACTION_* constants. |
||
287 | * Describes the operation to perform. |
||
288 | * |
||
289 | * @return $this The query object. |
||
290 | */ |
||
291 | protected function addWhere($name, $value, $operator) |
||
300 | } |
||
301 |