flush
[m6w6/pq-gateway] / lib / pq / Query / Expressible.php
1 <?php
2
3 namespace pq\Query;
4
5 class Expressible implements ExpressibleInterface
6 {
7 /**
8 * @var mixed
9 */
10 protected $data;
11
12 function __construct($data) {
13 $this->data = $data;
14 }
15
16 /**
17 * Get value as string
18 * @return string
19 */
20 function __toString() {
21 return (string) $this->data;
22 }
23
24 /**
25 * Test whether the value is an unevaluated expression
26 * @return bool
27 */
28 function isExpr() {
29 return $this->data instanceof Expr;
30 }
31
32 /**
33 * Get value
34 * @return mixed
35 */
36 function get() {
37 return $this->data;
38 }
39
40 /**
41 * Set the value
42 * @param mixed $data
43 * @return \pq\Query\Expressible
44 */
45 function set($data) {
46 $this->data = $data;
47 return $this;
48 }
49
50 /**
51 * Modify the data
52 * @param mixed $data
53 * @param string $op a specific operator
54 * @return \pq\Query\Expressible
55 */
56 function mod($data, $op = null) {
57 if (!($this->data instanceof Expr)) {
58 $this->data = new Expr($this->name);
59 }
60
61 if ($data instanceof Expr) {
62 $this->data->add($data);
63 } elseif (!isset($op) && is_numeric($data)) {
64 $this->data->add(new Expr("+ $data"));
65 } else {
66 $data = $this->row->getTable()->getConnection()->quote($data);
67 $this->data->add(new Expr("%s %s", isset($op) ? $op : "||", $data));
68 }
69
70 return $this;
71 }
72 }