tests
[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 * @var bool
26 */
27 protected $dirty;
28
29 /**
30 * @param \pq\Gateway\Row $row
31 * @param string $name
32 * @param mixed $data
33 * @param bool $dirty
34 */
35 function __construct(Row $row, $name, $data, $dirty = false) {
36 $this->row = $row;
37 $this->name = $name;
38 $this->data = $data;
39 $this->dirty = $dirty;
40 }
41
42 /**
43 * Get value as string
44 * @return string
45 */
46 function __toString() {
47 return (string) $this->data;
48 }
49
50 /**
51 * Test whether the value is an unevaluated expression
52 * @return bool
53 */
54 function isExpr() {
55 return $this->data instanceof Expr;
56 }
57
58 /**
59 * Check whether the cell has been modified
60 * @return bool
61 */
62 function isDirty() {
63 return $this->dirty;
64 }
65
66 /**
67 * Get value
68 * @return mixed
69 */
70 function get() {
71 return $this->data;
72 }
73
74 /**
75 * Modify the value in this cell
76 * @param mixed $data
77 * @param string $op a specific operator
78 * @return \pq\Gateway\Cell
79 */
80 function mod($data, $op = null) {
81 if (!($this->data instanceof Expr)) {
82 $this->data = new Expr($this->name);
83 }
84
85 if ($data instanceof Expr) {
86 $this->data->add($data);
87 } elseif (!isset($op) && is_numeric($data)) {
88 $this->data->add(new Expr("+ $data"));
89 } else {
90 $data = $this->row->getTable()->getConnection()->quote($data);
91 $this->data->add(new Expr("%s %s", isset($op) ? $op : "||", $data));
92 }
93
94 $this->dirty = true;
95
96 return $this;
97 }
98
99 /**
100 * Set the value in this cell
101 * @param mixed $data
102 * @return \pq\Gateway\Cell
103 */
104 function set($data) {
105 $this->data = $data;
106 $this->dirty = true;
107 return $this;
108 }
109 }