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