ae87c1f92abe4b4bdd4d20f1cc6d5c2d9f9b6ce0
[m6w6/pq-gateway] / lib / pq / Query / Expr.php
1 <?php
2
3 namespace pq\Query;
4
5 class Expr
6 {
7 /**
8 * @var string
9 */
10 protected $expression;
11
12 /**
13 * @var \pq\Query\Expr
14 */
15 protected $next;
16
17 /**
18 * @param string $e the expression or a format string followed by arguments
19 * @param string ...
20 */
21 function __construct($e) {
22 if (func_num_args() > 1) {
23 $e = call_user_func_array("sprintf", func_get_args());
24 }
25 $this->expression = trim($e);
26 }
27
28 /**
29 * Get the string expression
30 * @return string
31 */
32 function __toString() {
33 return (string) $this->expression . $this->next;
34 }
35
36 /**
37 * Check for NULL
38 * @return bool
39 */
40 function isNull() {
41 return !strcasecmp($this->expression, "null");
42 }
43
44 /**
45 * Append an expresssion
46 * @param \pq\Query\Expr $next
47 * @return \pq\Query\Expr $this
48 * @throws \UnexpectedValueException if any expr is NULL
49 */
50 function add(Expr $next) {
51 if ($this->isNull() || $next->isNull()) {
52 throw new \UnexpectedValueException("Cannot add anything to NULL");
53 }
54 for ($that = $this; $that->next; $that = $that->next);
55 $that->next = $next;
56 return $this;
57 }
58 }