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 $this->data = new Expr($this->name);
68 /*
69 if (!isset($this->data)) {
70 $this->data = new Expr($this->name);
71 } elseif (is_numeric($this->data)) {
72 $this->data = new Expr($this->data);
73 } else {
74 $this->data = new Expr("%s", $this->row->getTable()->getConnection()->quote($this->data));
75 }
76 */
77 }
78
79 if ($data instanceof Expr) {
80 $this->data->add($data);
81 } elseif (!isset($op) && is_numeric($data)) {
82 $this->data->add(new Expr("+ $data"));
83 } else {
84 $data = $this->row->getTable()->getConnection()->quote($data);
85 $this->data->add(new Expr("%s %s"), isset($op) ? $op : "||", $data);
86 }
87 return $this;
88 }
89
90 /**
91 * Set the value in this cell
92 * @param mixed $data
93 * @return \pq\Gateway\Cell
94 */
95 function set($data) {
96 $this->data = $data;
97 return $this;
98 }
99 }