|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Reduce\Db; |
|
4
|
|
|
|
|
5
|
|
|
use Iterator; |
|
6
|
|
|
use ArrayAccess; |
|
7
|
|
|
use Countable; |
|
8
|
|
|
use JsonSerializable; |
|
9
|
|
|
use Reduce\Db\ResultSet; |
|
10
|
|
|
|
|
11
|
|
|
class Row implements Iterator, ArrayAccess, Countable, JsonSerializable |
|
12
|
|
|
{ |
|
13
|
|
|
protected $data; |
|
14
|
|
|
protected $resultSet; |
|
15
|
|
|
|
|
16
|
1 |
|
public function __construct(ResultSet $resultSet, $data = []) |
|
17
|
|
|
{ |
|
18
|
1 |
|
$this->resultSet = $resultSet; |
|
19
|
1 |
|
$this->data = $data; |
|
20
|
1 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function __get($name) |
|
23
|
|
|
{ |
|
24
|
|
|
if(isset($this[$name . '_id'])) { |
|
25
|
|
|
$field = $this[$name . '_id']; |
|
26
|
|
|
|
|
27
|
|
|
return $this->getConnection()->{$name}[$field]; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
trigger_error('Undefined field: ' . $name . '_id on table ' . $this->getTableName()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function __call($name, $args) |
|
34
|
|
|
{ |
|
35
|
|
|
$resultSet = call_user_func_array([$this->getConnection(), $name], $args); |
|
36
|
|
|
$condition = sprintf("%s.%s_id = ?", $name, $this->getTableName()); |
|
37
|
|
|
|
|
38
|
|
|
$resultSet->where($condition, $this['id']); |
|
39
|
|
|
|
|
40
|
|
|
return $resultSet; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getConnection() |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->resultSet->getQueryBuilder()->getConnection(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getTableName() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->resultSet->getTableName(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function execute() |
|
54
|
|
|
{ |
|
55
|
|
|
$data = $this->resultSet->toArray(); |
|
56
|
|
|
|
|
57
|
|
|
if (count($data)) { |
|
58
|
|
|
$this->data = $data[0]; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function offsetGet($key) |
|
63
|
|
|
{ |
|
64
|
|
|
if (! count($this->data)) { |
|
65
|
|
|
$this->execute(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->data[$key]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function offsetSet($key, $value) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->data[$key] = $value; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function offsetUnset($key) |
|
77
|
|
|
{ |
|
78
|
|
|
unset($this->data[$key]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function offsetExists($key) |
|
82
|
|
|
{ |
|
83
|
|
|
return !! $this->offsetGet($key); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function count() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->execute(); |
|
89
|
|
|
return count($this->data); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function rewind() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->execute(); |
|
95
|
|
|
reset($this->data); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function current() |
|
99
|
|
|
{ |
|
100
|
|
|
return current($this->data); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function key() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->execute(); |
|
106
|
|
|
return key($this->data); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function next() |
|
110
|
|
|
{ |
|
111
|
|
|
next($this->data); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function valid() |
|
115
|
|
|
{ |
|
116
|
|
|
$key = key($this->data); |
|
117
|
|
|
return ($key !== null); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function jsonSerialize() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->execute(); |
|
123
|
|
|
return $this->data; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function __toString() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->resultSet->getQueryBuilder()->__toString(); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|