|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Records\AbstractModels;
|
|
4
|
|
|
|
|
5
|
|
|
use Nip\HelperBroker;
|
|
6
|
|
|
use Nip\Logger\Exception;
|
|
7
|
|
|
use Nip\Utility\Traits\NameWorksTrait;
|
|
8
|
|
|
|
|
9
|
|
|
/**
|
|
10
|
|
|
* Class Row
|
|
11
|
|
|
* @package Nip\Records\_Abstract
|
|
12
|
|
|
*
|
|
13
|
|
|
* @method \Nip_Helper_Url URL()
|
|
14
|
|
|
*/
|
|
15
|
|
|
abstract class Record extends \Nip_Object
|
|
16
|
|
|
{
|
|
17
|
|
|
use NameWorksTrait;
|
|
18
|
|
|
|
|
19
|
|
|
protected $_name = null;
|
|
20
|
|
|
protected $_manager = null;
|
|
21
|
|
|
|
|
22
|
|
|
/**
|
|
23
|
|
|
* @var string
|
|
24
|
|
|
*/
|
|
25
|
|
|
protected $managerName = null;
|
|
26
|
|
|
|
|
27
|
|
|
protected $_dbData = [];
|
|
28
|
|
|
protected $_helpers = [];
|
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/**
|
|
32
|
|
|
* Overloads Ucfirst() helper
|
|
33
|
|
|
*
|
|
34
|
|
|
* @param string $name
|
|
35
|
|
|
* @param array $arguments
|
|
36
|
|
|
* @return mixed
|
|
37
|
|
|
*/
|
|
38
|
|
View Code Duplication |
public function __call($name, $arguments)
|
|
|
|
|
|
|
39
|
|
|
{
|
|
40
|
|
|
if ($name === ucfirst($name)) {
|
|
41
|
|
|
return $this->getHelper($name);
|
|
42
|
|
|
}
|
|
43
|
|
|
|
|
44
|
|
|
trigger_error("Call to undefined method $name", E_USER_ERROR);
|
|
45
|
|
|
return null;
|
|
46
|
|
|
}
|
|
47
|
|
|
|
|
48
|
|
|
/**
|
|
49
|
|
|
* @param $name
|
|
50
|
|
|
* @return \Nip\Helpers\AbstractHelper
|
|
51
|
|
|
*/
|
|
52
|
|
|
public function getHelper($name)
|
|
53
|
|
|
{
|
|
54
|
|
|
return HelperBroker::get($name);
|
|
55
|
|
|
}
|
|
56
|
|
|
|
|
57
|
|
|
/**
|
|
58
|
|
|
* @return mixed
|
|
59
|
|
|
*/
|
|
60
|
|
|
public function getName()
|
|
61
|
|
|
{
|
|
62
|
|
|
if ($this->_name == null) {
|
|
63
|
|
|
$this->_name = inflector()->unclassify(get_class($this));
|
|
64
|
|
|
}
|
|
65
|
|
|
return $this->_name;
|
|
66
|
|
|
}
|
|
67
|
|
|
|
|
68
|
|
|
/**
|
|
69
|
|
|
* @param mixed $name
|
|
70
|
|
|
*/
|
|
71
|
|
|
public function setName($name)
|
|
72
|
|
|
{
|
|
73
|
|
|
$this->_name = $name;
|
|
74
|
|
|
}
|
|
75
|
|
|
|
|
76
|
|
|
/**
|
|
77
|
|
|
* @param bool|array $data
|
|
78
|
|
|
*/
|
|
79
|
|
|
public function writeDBData($data = false)
|
|
80
|
|
|
{
|
|
81
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
|
|
|
82
|
|
|
$this->_dbData[$key] = $value;
|
|
83
|
|
|
}
|
|
84
|
|
|
}
|
|
85
|
|
|
|
|
86
|
|
|
/**
|
|
87
|
|
|
* @return array
|
|
88
|
|
|
*/
|
|
89
|
|
|
public function getDBData()
|
|
90
|
|
|
{
|
|
91
|
|
|
return $this->_dbData;
|
|
92
|
|
|
}
|
|
93
|
|
|
|
|
94
|
|
|
/**
|
|
95
|
|
|
* @return mixed
|
|
96
|
|
|
*/
|
|
97
|
|
|
public function getPrimaryKey()
|
|
98
|
|
|
{
|
|
99
|
|
|
$pk = $this->getManager()->getPrimaryKey();
|
|
100
|
|
|
|
|
101
|
|
|
return $this->{$pk};
|
|
102
|
|
|
}
|
|
103
|
|
|
|
|
104
|
|
|
/**
|
|
105
|
|
|
* @return \Nip\Records\RecordManager
|
|
106
|
|
|
*/
|
|
107
|
2 |
|
public function getManager()
|
|
108
|
|
|
{
|
|
109
|
2 |
|
if ($this->_manager == null) {
|
|
110
|
|
|
$this->initManager();
|
|
111
|
|
|
}
|
|
112
|
|
|
|
|
113
|
2 |
|
return $this->_manager;
|
|
114
|
|
|
}
|
|
115
|
|
|
|
|
116
|
|
|
/**
|
|
117
|
|
|
* @param RecordManager $manager
|
|
118
|
|
|
*/
|
|
119
|
4 |
|
public function setManager($manager)
|
|
120
|
|
|
{
|
|
121
|
4 |
|
$this->_manager = $manager;
|
|
122
|
4 |
|
}
|
|
123
|
|
|
|
|
124
|
|
|
protected function initManager()
|
|
125
|
|
|
{
|
|
126
|
|
|
$class = $this->getManagerName();
|
|
127
|
|
|
$manager = $this->getManagerInstance($class);
|
|
128
|
|
|
$this->setManager($manager);
|
|
129
|
|
|
}
|
|
130
|
|
|
|
|
131
|
|
|
/**
|
|
132
|
|
|
* @return null
|
|
133
|
|
|
*/
|
|
134
|
2 |
|
public function getManagerName()
|
|
135
|
|
|
{
|
|
136
|
2 |
|
if ($this->managerName === null) {
|
|
137
|
2 |
|
$this->initManagerName();
|
|
138
|
|
|
}
|
|
139
|
|
|
|
|
140
|
2 |
|
return $this->managerName;
|
|
141
|
|
|
}
|
|
142
|
|
|
|
|
143
|
|
|
/**
|
|
144
|
|
|
* @param string $managerName
|
|
145
|
|
|
*/
|
|
146
|
2 |
|
public function setManagerName($managerName)
|
|
147
|
|
|
{
|
|
148
|
2 |
|
$this->managerName = $managerName;
|
|
149
|
2 |
|
}
|
|
150
|
|
|
|
|
151
|
2 |
|
protected function initManagerName()
|
|
152
|
|
|
{
|
|
153
|
2 |
|
$this->setManagerName($this->inflectManagerName());
|
|
154
|
2 |
|
}
|
|
155
|
|
|
|
|
156
|
|
|
/**
|
|
157
|
|
|
* @return string
|
|
158
|
|
|
*/
|
|
159
|
2 |
|
protected function inflectManagerName()
|
|
160
|
|
|
{
|
|
161
|
2 |
|
return ucfirst(inflector()->pluralize($this->getClassName()));
|
|
162
|
|
|
}
|
|
163
|
|
|
|
|
164
|
|
|
/**
|
|
165
|
|
|
* @param string $class
|
|
166
|
|
|
* @return mixed
|
|
167
|
|
|
* @throws Exception
|
|
168
|
|
|
*/
|
|
169
|
|
|
protected function getManagerInstance($class)
|
|
170
|
|
|
{
|
|
171
|
|
|
if (class_exists($class)) {
|
|
172
|
|
|
return call_user_func([$class, 'instance']);
|
|
173
|
|
|
}
|
|
174
|
|
|
throw new Exception('invalid manager name [' . $class . ']');
|
|
175
|
|
|
}
|
|
176
|
|
|
|
|
177
|
|
|
/**
|
|
178
|
|
|
* @return bool
|
|
179
|
|
|
*/
|
|
180
|
|
|
public function insert()
|
|
181
|
|
|
{
|
|
182
|
|
|
$pk = $this->getManager()->getPrimaryKey();
|
|
183
|
|
|
$lastId = $this->getManager()->insert($this);
|
|
184
|
|
|
if ($pk == 'id') {
|
|
185
|
|
|
$this->{$pk} = $lastId;
|
|
186
|
|
|
}
|
|
187
|
|
|
|
|
188
|
|
|
return $lastId > 0;
|
|
189
|
|
|
}
|
|
190
|
|
|
|
|
191
|
|
|
/**
|
|
192
|
|
|
* @return bool|\Nip\Database\Result
|
|
193
|
|
|
*/
|
|
194
|
|
|
public function update()
|
|
195
|
|
|
{
|
|
196
|
|
|
$return = $this->getManager()->update($this);
|
|
197
|
|
|
return $return;
|
|
198
|
|
|
}
|
|
199
|
|
|
|
|
200
|
|
|
public function save()
|
|
201
|
|
|
{
|
|
202
|
|
|
$this->getManager()->save($this);
|
|
203
|
|
|
}
|
|
204
|
|
|
|
|
205
|
|
|
public function saveRecord()
|
|
206
|
|
|
{
|
|
207
|
|
|
$this->getManager()->save($this);
|
|
208
|
|
|
}
|
|
209
|
|
|
|
|
210
|
|
|
public function delete()
|
|
211
|
|
|
{
|
|
212
|
|
|
$this->getManager()->delete($this);
|
|
213
|
|
|
}
|
|
214
|
|
|
|
|
215
|
|
|
/**
|
|
216
|
|
|
* @return bool
|
|
217
|
|
|
*/
|
|
218
|
|
|
public function isInDB()
|
|
219
|
|
|
{
|
|
220
|
|
|
$pk = $this->getManager()->getPrimaryKey();
|
|
221
|
|
|
return $this->{$pk} > 0;
|
|
222
|
|
|
}
|
|
223
|
|
|
|
|
224
|
|
|
/**
|
|
225
|
|
|
* @return bool|false|Record
|
|
226
|
|
|
*/
|
|
227
|
|
|
public function exists()
|
|
228
|
|
|
{
|
|
229
|
|
|
return $this->getManager()->exists($this);
|
|
230
|
|
|
}
|
|
231
|
|
|
|
|
232
|
|
|
/**
|
|
233
|
|
|
* @return string
|
|
234
|
|
|
*/
|
|
235
|
|
|
public function toJSON()
|
|
236
|
|
|
{
|
|
237
|
|
|
return json_encode($this->toArray());
|
|
238
|
|
|
}
|
|
239
|
|
|
|
|
240
|
|
|
/**
|
|
241
|
|
|
* @return mixed
|
|
242
|
|
|
*/
|
|
243
|
|
|
public function toArray()
|
|
244
|
|
|
{
|
|
245
|
|
|
$vars = get_object_vars($this);
|
|
246
|
|
|
return $vars['_data'];
|
|
247
|
|
|
}
|
|
248
|
|
|
|
|
249
|
|
|
/**
|
|
250
|
|
|
* @return mixed
|
|
251
|
|
|
*/
|
|
252
|
|
|
public function toApiArray()
|
|
253
|
|
|
{
|
|
254
|
|
|
$data = $this->toArray();
|
|
255
|
|
|
return $data;
|
|
256
|
|
|
}
|
|
257
|
|
|
|
|
258
|
|
|
/**
|
|
259
|
|
|
* @return Record
|
|
260
|
|
|
*/
|
|
261
|
|
|
public function getCloneWithRelations()
|
|
262
|
|
|
{
|
|
263
|
|
|
$item = $this->getClone();
|
|
264
|
|
|
$item->cloneRelations($this);
|
|
265
|
|
|
|
|
266
|
|
|
return $item;
|
|
267
|
|
|
}
|
|
268
|
|
|
|
|
269
|
|
|
/**
|
|
270
|
|
|
* @return Record
|
|
271
|
|
|
*/
|
|
272
|
|
|
public function getClone()
|
|
273
|
|
|
{
|
|
274
|
|
|
$clone = $this->getManager()->getNew();
|
|
275
|
|
|
$clone->updateDataFromRecord($this);
|
|
276
|
|
|
|
|
277
|
|
|
unset($clone->{$this->getManager()->getPrimaryKey()}, $clone->created);
|
|
278
|
|
|
|
|
279
|
|
|
return $clone;
|
|
280
|
|
|
}
|
|
281
|
|
|
|
|
282
|
|
|
/**
|
|
283
|
|
|
* @param self $record
|
|
284
|
|
|
*/
|
|
285
|
|
|
public function updateDataFromRecord($record)
|
|
286
|
|
|
{
|
|
287
|
|
|
$data = $record->toArray();
|
|
288
|
|
|
$this->writeData($data);
|
|
289
|
|
|
|
|
290
|
|
|
unset($this->{$this->getManager()->getPrimaryKey()}, $this->created);
|
|
291
|
|
|
}
|
|
292
|
|
|
|
|
293
|
|
|
/**
|
|
294
|
|
|
* @param bool|array $data
|
|
295
|
|
|
*/
|
|
296
|
|
|
public function writeData($data = false)
|
|
297
|
|
|
{
|
|
298
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
|
|
|
299
|
|
|
$this->__set($key, $value);
|
|
300
|
|
|
}
|
|
301
|
|
|
}
|
|
302
|
|
|
|
|
303
|
|
|
/**
|
|
304
|
|
|
* Clone the relations records from a sibling
|
|
305
|
|
|
* @param self $from
|
|
306
|
|
|
* @return self
|
|
307
|
|
|
*/
|
|
308
|
|
|
public function cloneRelations($from)
|
|
309
|
|
|
{
|
|
310
|
|
|
return $this->getManager()->cloneRelations($from, $this);
|
|
311
|
|
|
}
|
|
312
|
|
|
|
|
313
|
|
|
/**
|
|
314
|
|
|
* @return \Nip\Request
|
|
315
|
|
|
*/
|
|
316
|
|
|
protected function getRequest()
|
|
317
|
|
|
{
|
|
318
|
|
|
return $this->getManager()->getRequest();
|
|
319
|
|
|
}
|
|
320
|
|
|
}
|
|
321
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.