tests
[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, $arg = null) {
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 $string = $this->expression;
34 if ($this->next) {
35 $string .= " " . $this->next;
36 }
37 return (string) $string;
38 }
39
40 /**
41 * Check for NULL
42 * @return bool
43 */
44 function isNull() {
45 return !strcasecmp($this->expression, "null");
46 }
47
48 /**
49 * Append an expresssion
50 * @param \pq\Query\Expr $next
51 * @return \pq\Query\Expr $this
52 * @throws \UnexpectedValueException if any expr is NULL
53 */
54 function add(Expr $next) {
55 if ($this->isNull() || $next->isNull()) {
56 throw new \UnexpectedValueException("Cannot add anything to NULL");
57 }
58 for ($that = $this; $that->next; $that = $that->next);
59 $that->next = $next;
60 return $this;
61 }
62 }