1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SimpleMapper; |
5
|
|
|
|
6
|
|
|
use Nette\Database\Table\Selection as NetteDatabaseSelection; |
7
|
|
|
use Nette\Database\Table\ActiveRow as NetteDatabaseActiveRow; |
8
|
|
|
use ArrayIterator; |
9
|
|
|
use IteratorAggregate; |
10
|
|
|
use Nette\Database\Table\IRow; |
11
|
|
|
use Nette\DeprecatedException; |
12
|
|
|
use SimpleMapper\Exception\ActiveRowException; |
13
|
|
|
use SimpleMapper\Structure\Structure; |
14
|
|
|
|
15
|
|
|
class ActiveRow implements IteratorAggregate, IRow |
16
|
|
|
{ |
17
|
|
|
/** @var NetteDatabaseActiveRow */ |
18
|
|
|
protected $record; |
19
|
|
|
|
20
|
|
|
/** @var Structure */ |
21
|
|
|
protected $structure; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param NetteDatabaseActiveRow $record |
25
|
|
|
* @param Structure $structure |
26
|
|
|
*/ |
27
|
50 |
|
public function __construct(NetteDatabaseActiveRow $record, Structure $structure) |
28
|
|
|
{ |
29
|
50 |
|
$this->record = $record; |
30
|
50 |
|
$this->structure = $structure; |
31
|
50 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $name |
35
|
|
|
* @return mixed|ActiveRow |
36
|
|
|
*/ |
37
|
16 |
|
public function __get($name) |
38
|
|
|
{ |
39
|
16 |
|
$result = $this->record->$name; |
40
|
14 |
|
return $result instanceof IRow ? $this->prepareRecord($result) : $result; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return NetteDatabaseActiveRow |
45
|
|
|
*/ |
46
|
2 |
|
public function getRecord(): NetteDatabaseActiveRow |
47
|
|
|
{ |
48
|
2 |
|
return $this->record; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return NetteDatabaseSelection |
53
|
|
|
*/ |
54
|
2 |
|
public function getTable(): NetteDatabaseSelection |
55
|
|
|
{ |
56
|
2 |
|
return $this->record->getTable(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param NetteDatabaseSelection $selection |
61
|
|
|
* @throws ActiveRowException |
62
|
|
|
*/ |
63
|
2 |
|
public function setTable(NetteDatabaseSelection $selection): void |
64
|
|
|
{ |
65
|
2 |
|
throw new ActiveRowException('Internal IRow interface method'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/**********************************************************************\ |
69
|
|
|
* Wrapper function |
70
|
|
|
\**********************************************************************/ |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
2 |
|
public function __toString(): string |
76
|
|
|
{ |
77
|
2 |
|
return (string) $this->record; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
6 |
|
public function toArray(): array |
84
|
|
|
{ |
85
|
6 |
|
return $this->record->toArray(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param bool|true $need |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
2 |
|
public function getPrimary($need = true) |
93
|
|
|
{ |
94
|
2 |
|
return $this->record->getPrimary($need); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param bool|true $need |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
2 |
|
public function getSignature($need = true): string |
102
|
|
|
{ |
103
|
2 |
|
return $this->record->getSignature($need); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array|\Traversable $data |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
6 |
|
public function update($data): bool |
111
|
|
|
{ |
112
|
6 |
|
return $this->record->update($data); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
4 |
|
public function delete(): int |
119
|
|
|
{ |
120
|
4 |
|
return $this->record->delete(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns referenced row |
125
|
|
|
* @param string $key |
126
|
|
|
* @param string $throughColumn |
127
|
|
|
* @return ActiveRow|null |
128
|
|
|
*/ |
129
|
6 |
|
public function ref($key, $throughColumn = null): ?ActiveRow |
130
|
|
|
{ |
131
|
6 |
|
$row = $this->record->ref($key, $throughColumn); |
132
|
6 |
|
return $row instanceof IRow ? $this->prepareRecord($row) : null; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns referencing rows |
138
|
|
|
* @param string $key |
139
|
|
|
* @param string $throughColumn |
140
|
|
|
* @return Selection |
141
|
|
|
*/ |
142
|
4 |
|
public function related($key, $throughColumn = null): ?Selection |
143
|
|
|
{ |
144
|
4 |
|
$selection = $this->record->related($key, $throughColumn); |
145
|
4 |
|
return $selection instanceof NetteDatabaseSelection ? $this->prepareSelection($selection) : null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/**********************************************************************\ |
149
|
|
|
* IteratorAggregate interface |
150
|
|
|
\**********************************************************************/ |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return ArrayIterator |
154
|
|
|
*/ |
155
|
2 |
|
public function getIterator(): ArrayIterator |
156
|
|
|
{ |
157
|
2 |
|
return $this->record->getIterator(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/**********************************************************************\ |
161
|
|
|
* ArrayAccess interface |
162
|
|
|
\**********************************************************************/ |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns value of column |
166
|
|
|
* @param string $key column name |
167
|
|
|
* @return mixed |
168
|
|
|
*/ |
169
|
6 |
|
public function offsetGet($key) |
170
|
|
|
{ |
171
|
6 |
|
return $this->record->offsetGet($key); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Tests if column exists |
176
|
|
|
* @param string $key column name |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
2 |
|
public function offsetExists($key): bool |
180
|
|
|
{ |
181
|
2 |
|
return $this->record->offsetExists($key); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Stores value in column |
186
|
|
|
* @param string $key column name |
187
|
|
|
* @param string $value |
188
|
|
|
* @throws DeprecatedException |
189
|
|
|
*/ |
190
|
2 |
|
public function offsetSet($key, $value): void |
191
|
|
|
{ |
192
|
2 |
|
$this->record->offsetSet($key, $value); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Removes column from data |
197
|
|
|
* @param string $key column name |
198
|
|
|
* @throws DeprecatedException |
199
|
|
|
*/ |
200
|
2 |
|
public function offsetUnset($key): void |
201
|
|
|
{ |
202
|
2 |
|
$this->record->offsetUnset($key); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/**********************************************************************\ |
206
|
|
|
* Extend methods |
207
|
|
|
\**********************************************************************/ |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Returns mm referencing rows |
211
|
|
|
* @param Selection $selection |
212
|
|
|
* @param string $ref |
213
|
|
|
* @param string $refPrimary |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
4 |
|
protected function mmRelated(Selection $selection, string $ref, string $refPrimary = 'id'): array |
217
|
|
|
{ |
218
|
4 |
|
$result = []; |
219
|
4 |
|
foreach ($selection as $row) { |
220
|
4 |
|
$result[$row->ref($ref)->$refPrimary] = $row->ref($ref); |
221
|
|
|
} |
222
|
4 |
|
return $result; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/**********************************************************************\ |
226
|
|
|
* Build methods |
227
|
|
|
\**********************************************************************/ |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Prepare one record |
231
|
|
|
* @param IRow $row |
232
|
|
|
* @return ActiveRow |
233
|
|
|
*/ |
234
|
8 |
|
protected function prepareRecord(IRow $row): ActiveRow |
235
|
|
|
{ |
236
|
8 |
|
$recordClass = $this->structure->getActiveRowClass($row->getTable()->getName()); |
237
|
8 |
|
return new $recordClass($row, $this->structure); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Prepare selection |
242
|
|
|
* @param NetteDatabaseSelection $selection |
243
|
|
|
* @return Selection |
244
|
|
|
*/ |
245
|
4 |
|
protected function prepareSelection(NetteDatabaseSelection $selection): Selection |
246
|
|
|
{ |
247
|
4 |
|
$selectionClass = $this->structure->getSelectionClass($selection->getName()); |
248
|
4 |
|
return new $selectionClass($selection, $this->structure); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|