trigger mirror
[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 string
9 */
10 protected $name;
11
12 /**
13 * @var mixed
14 */
15 protected $data;
16
17 /**
18 * @param string $name
19 * @param mixed $data
20 */
21 function __construct($name, $data) {
22 $this->name = $name;
23 $this->data = $data;
24 }
25
26 /**
27 * Get value as string
28 * @return string
29 */
30 function __toString() {
31 return (string) $this->data;
32 }
33
34 /**
35 * Test whether the value is an unevaluated expression
36 * @return bool
37 */
38 function isExpr() {
39 return $this->data instanceof Expr;
40 }
41
42 /**
43 * Get value
44 * @return mixed
45 */
46 function get() {
47 return $this->data;
48 }
49
50 /**
51 * Set the value
52 * @param mixed $data
53 * @return \pq\Query\Expressible
54 */
55 function set($data) {
56 $this->data = $data;
57 return $this;
58 }
59
60 /**
61 * Modify the data
62 * @param mixed $data
63 * @param string $op a specific operator
64 * @return \pq\Query\Expressible
65 */
66 function mod($data, $op = null) {
67 if (!($this->data instanceof Expr)) {
68 $this->data = new Expr($this->name);
69 }
70
71 if ($data instanceof Expr) {
72 $this->data->add($data);
73 } elseif (!isset($op) && is_numeric($data)) {
74 $this->data->add(new Expr("+ $data"));
75 } else {
76 $this->data->add(new Expr("%s %s", isset($op) ? $op : "||", $data));
77 }
78
79 return $this;
80 }
81 }