forked from prolificinteractive/mabi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
485 lines (433 loc) · 12.9 KB
/
Model.php
File metadata and controls
485 lines (433 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
namespace MABI;
include_once __DIR__ . '/Inflector.php';
include_once __DIR__ . '/Utilities.php';
/**
* todo: docs
*/
class Model {
/**
* @var App
*/
protected $app;
/**
* @var string
*/
protected $modelClass;
/**
* @var string
*/
protected $connection = 'default';
/**
* @var string
*/
protected $idColumn;
/**
* @var string
*/
protected $idProperty;
/**
* @var string
*/
protected $table = NULL;
protected $readAccess;
protected $writeAccess;
/**
* @field system
* @var array
*/
public $_remainingReadResults;
/**
* @var array
*/
protected $readFields = array();
/**
* @param string $table
*/
public function setTable($table) {
$this->table = $table;
}
/**
* @return string
*/
protected function getTable() {
return $this->table;
}
public function getId() {
return $this->{$this->idProperty};
}
/**
* @return string
*/
public function getIdProperty() {
return $this->idProperty;
}
public static function initWithNewId($app) {
$newModelObj = self::init($app);
$newModelObj->{$newModelObj->idProperty} = $newModelObj->getNewId();
return $newModelObj;
}
/**
* todo: docs
*
* @param $app App
*
* @return Model
*/
public static function init($app) {
$modelClass = get_called_class();
/**
* @var $newModelObj Model
*/
$newModelObj = new $modelClass();
$newModelObj->modelClass = get_called_class();
$newModelObj->app = $app;
if (empty($newModelObj->table)) {
$newModelObj->table = strtolower(Inflector::pluralize(ReflectionHelper::stripClassName($modelClass)));
}
// Gets the default ID column on the DataConnection side
if (empty($newModelObj->idColumn)) {
$newModelObj->idColumn = $newModelObj->app->getDataConnection($newModelObj->connection)->getDefaultIdColumn();
}
// Allows overrides of idProperty for the name of the ID on the MABI side
if (empty($newModelObj->idProperty)) {
//
$rClass = new \ReflectionClass($newModelObj);
$rProperties = $rClass->getProperties(\ReflectionProperty::IS_PUBLIC);
foreach ($rProperties as $rProperty) {
/*
* Looks for the '@field id' directive to set as the id property
*/
if (in_array('id', ReflectionHelper::getDocDirective($rProperty->getDocComment(), 'field'))) {
$newModelObj->idProperty = $rProperty->getName();
break;
}
}
if (empty($newModelObj->idProperty)) {
$newModelObj->idProperty = 'id';
}
}
$newModelObj->{$newModelObj->idProperty} = NULL;
return $newModelObj;
}
public function getNewId() {
$dataConnection = $this->app->getDataConnection($this->connection);
return $dataConnection->getNewId();
}
/**
* todo: docs
*
* @return Model[]
*/
public function findAll() {
$dataConnection = $this->app->getDataConnection($this->connection);
$foundObjects = $dataConnection->findAll($this->table, $this->readFields);
$foundModels = array();
if (is_array($foundObjects)) {
foreach ($foundObjects as $foundObject) {
/**
* @var $model \MABI\Model
*/
$model = call_user_func($this->modelClass . '::init', $this->app);
$model->loadParameters($foundObject);
$foundModels[] = $model;
}
}
return $foundModels;
}
/**
* @param $query array
*
* @return Model[]
*/
public function query($query) {
$dataConnection = $this->app->getDataConnection($this->connection);
$foundObjects = $dataConnection->query($this->table, $query);
$foundModels = array();
foreach ($foundObjects as $foundObject) {
/**
* @var $model \MABI\Model
*/
$model = call_user_func($this->modelClass . '::init', $this->app);
$model->loadParameters($foundObject);
$foundModels[] = $model;
}
return $foundModels;
}
/**
* Sets a parameter based on its type
*
* @param $type
* @param $parameter
* @param $result
*
* @throws \Exception
*/
protected function loadParameter($type, &$parameter, $result) {
switch ($type) {
case 'string':
$parameter = $result;
break;
case 'int':
$parameter = intval($result);
break;
case 'bool':
$parameter = $result == TRUE;
break;
case 'float':
$parameter = floatval($result);
break;
case 'DateTime':
case '\DateTime':
if (empty($result)) {
$parameter = NULL;
}
else {
$parameter = new \DateTime('@' . $result);
}
break;
case '':
case 'array':
$parameter = $result;
break;
default:
try {
$rClass = new \ReflectionClass($type);
if ($rClass->isSubclassOf('\MABI\Model')) {
/**
* @var $model \MABI\Model
*/
$model = call_user_func($type . '::init', $this->app);
$model->loadParameters($result);
$parameter = $model;
}
else {
throw New \Exception('Class ' . $type . ' does not derive from \MABI\Model');
}
} catch (\ReflectionException $ex) {
$parameter = $result;
}
}
}
/**
* Loads parameters from a PHP database into the model object using reflection
*
* @param $resultArray array
* @param $forceId string
*
* @throws \Exception
*/
public function loadParameters($resultArray, $forceId = NULL) {
$rClass = new \ReflectionClass($this);
$rProperties = $rClass->getProperties(\ReflectionProperty::IS_PUBLIC);
if (!empty($resultArray[$this->idColumn])) {
$dataConnection = $this->app->getDataConnection($this->connection);
$this->{$this->idProperty} = $dataConnection->convertFromNativeId($resultArray[$this->idColumn]);
unset($resultArray[$this->idColumn]);
unset($resultArray[$this->idProperty]);
}
foreach ($rProperties as $rProperty) {
if (!array_key_exists($rProperty->name, $resultArray)) {
continue;
}
// Pulls out the type following the pattern @var <TYPE> from the doc comments of the property
$varDocs = ReflectionHelper::getDocDirective($rProperty->getDocComment(), 'var');
if (empty($varDocs)) {
$this->{$rProperty->getName()} = $resultArray[$rProperty->getName()];
}
else {
$type = $varDocs[0];
$matches = array();
if (preg_match('/(.*)\[\]/', $type, $matches)) {
// If the type follows the list of type pattern (<TYPE>[]), an array will be generated and filled
// with that type
$type = $matches[1];
$outArr = array();
if (!empty($resultArray[$rProperty->getName()])) {
foreach ($resultArray[$rProperty->getName()] as $listResult) {
$this->loadParameter($type, $parameter, $listResult);
$outArr[] = $parameter;
}
}
$this->{$rProperty->getName()} = $outArr;
}
else {
$this->loadParameter($type, $this->{$rProperty->getName()}, $resultArray[$rProperty->getName()]);
}
}
unset($resultArray[$rProperty->getName()]);
}
if (isset($forceId)) {
$this->{$this->idProperty} = $forceId;
}
$this->_remainingReadResults = $resultArray;
}
/**
* todo: docs
*
* @param $id
*
* @return bool
*/
public function findById($id) {
$dataConnection = $this->app->getDataConnection($this->connection);
$result = $dataConnection->findOneByField($this->idColumn, $dataConnection->convertToNativeId($id),
$this->table, $this->readFields);
if ($result == NULL) {
return FALSE;
}
$this->loadParameters($result);
return TRUE;
}
/**
* todo: docs
*
* @param $fieldName string
* @param $value string
*
* @return bool
*/
public function findByField($fieldName, $value) {
$dataConnection = $this->app->getDataConnection($this->connection);
if($fieldName == $this->idColumn) {
$value = $dataConnection->convertToNativeId($value);
}
$result = $dataConnection->findOneByField($fieldName, $value, $this->table, $this->readFields);
if ($result == NULL) {
return FALSE;
}
$this->loadParameters($result);
return TRUE;
}
protected function getPropertyArrayValue($value, $forOutput = FALSE) {
if (!is_object($value)) {
return $value;
}
else {
$propClass = new \ReflectionClass($value);
if ($propClass->isSubclassOf('\MABI\Model')) {
/**
* @var $subModel \MABI\Model
*/
$subModel = $value;
return $subModel->getPropertyArray($forOutput);
}
elseif ($propClass->name == 'DateTime' || $propClass == '\DateTime') {
/**
* @var $date \DateTime
*/
$date = $value;
return $date->getTimestamp();
}
}
return NULL;
}
public function getPropertyArray($forOutput = FALSE) {
$rClass = new \ReflectionClass($this);
$outArr = array();
$rProperties = $rClass->getProperties(\ReflectionProperty::IS_PUBLIC);
foreach ($rProperties as $rProperty) {
/*
* Ignores writing any model property with 'external' option
*/
if (!$forOutput && in_array('external', ReflectionHelper::getDocDirective($rProperty->getDocComment(), 'field'))) {
continue;
}
if ($forOutput && in_array('internal', ReflectionHelper::getDocDirective($rProperty->getDocComment(), 'field'))) {
continue;
}
if (in_array('system', ReflectionHelper::getDocDirective($rProperty->getDocComment(), 'field'))) {
continue;
}
if (is_array($this->{$rProperty->getName()})) {
foreach ($this->{$rProperty->getName()} as $k => $v) {
$outArr[$rProperty->getName()][$k] = $this->getPropertyArrayValue($v, $forOutput);
}
}
else {
$outArr[$rProperty->getName()] = $this->getPropertyArrayValue($this->{$rProperty->getName()}, $forOutput);
}
}
if (!empty($this->{$this->idProperty})) {
if (!$forOutput) {
$dataConnection = $this->app->getDataConnection($this->connection);
$outArr[$this->idColumn] = $dataConnection->convertToNativeId($this->{$this->idProperty});
}
else {
$outArr[$this->idProperty] = $this->{$this->idProperty};
}
}
if (!empty($this->_remainingReadResults) && !$forOutput) {
$outArr = array_merge($outArr, $this->_remainingReadResults);
}
return $outArr;
}
/**
* todo: docs
*/
public function insert() {
$dataConnection = $this->app->getDataConnection($this->connection);
$propArray = $dataConnection->insert($this->table, $this->getPropertyArray());
$this->loadParameters($propArray);
}
/**
* todo: docs
*/
public function save() {
$dataConnection = $this->app->getDataConnection($this->connection);
$propArray = $this->getPropertyArray();
$dataConnection->save($this->table, $propArray, $this->idColumn,
$dataConnection->convertToNativeId($this->{$this->idProperty}));
$this->loadParameters($propArray);
}
/**
* todo: docs
*/
public function delete() {
$dataConnection = $this->app->getDataConnection($this->connection);
$dataConnection->deleteByField($this->idColumn, $dataConnection->convertToNativeId($this->{$this->idProperty}),
$this->table);
}
/**
* todo: docs
*/
public function clearAll() {
$dataConnection = $this->app->getDataConnection($this->connection);
return $dataConnection->clearAll($this->table);
}
public function outputJSON() {
return json_encode($this->getPropertyArray(TRUE));
}
public function getDocOutput(Parser $parser) {
$fieldDocs = array();
$rClass = new \ReflectionClass($this);
$rProperties = $rClass->getProperties(\ReflectionProperty::IS_PUBLIC);
foreach ($rProperties as $rProperty) {
/*
* Ignores writing any model property with 'internal' or 'system' options
*/
if (in_array('internal', ReflectionHelper::getDocDirective($rProperty->getDocComment(), 'field')) ||
in_array('system', ReflectionHelper::getDocDirective($rProperty->getDocComment(), 'field'))
) {
continue;
}
$varType = 'unknown';
// Pulls out the type following the pattern @var <TYPE> from the doc comments of the property
$varDocs = ReflectionHelper::getDocDirective($rProperty->getDocComment(), 'var');
if (!empty($varDocs)) {
$varType = $varDocs[0];
}
$fieldDoc = array(
'name' => $rProperty->getName(),
'type' => $varType,
'doc' => $parser->parse(ReflectionHelper::getDocText($rProperty->getDocComment()))
);
$fieldDocs[ /* $rProperty->getName() */] = $fieldDoc;
}
return array(
'name' => get_called_class(),
'fielddocs' => $fieldDocs,
// todo: Add 'SampleJSON' so that it can be copied into requests
);
}
}