-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model.php
554 lines (504 loc) · 16.5 KB
/
Model.php
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<?php
/**
* Created by IntelliJ IDEA.
* User: TingSong-Syu
* Date: 2016/1/4
* Time: 2:37 pm
*/
/**
* Class Model
* @property-read medoo $medoo
* @property-read array $properties
* @property-read string $table_name
* @property array $exported
* @property int $id
*/
abstract class Model implements ArrayAccess, IteratorAggregate, JsonSerializable {
/**
* @var medoo
*/
private static $_medoo;
/**
* @var array
*/
protected $_properties = [];
/**
* @var array
*/
protected $_modified = [];
/**
* @var array
*/
protected static $_meta = [];
/**
* @var array
*/
protected static $_validate = [];
/**
* @var array
*/
protected $_exported = [];
/**
* @var bool
*/
protected $_trace = true;
/**
* @var bool
*/
protected $_new = true;
/**
* @param string $name
* @return Model
*/
public static function getInstance($name) {
$className = "{$name}Model";
$model = new $className();
return $model;
}
/**
* @param medoo $medoo
*/
public static function setMedoo(medoo $medoo) {
self::$_medoo = $medoo;
}
public function __construct($trace_modify = true) {
$this->_trace = $trace_modify;
}
public function setup($template) {
$this->_trace = false;
foreach ($template as $name => $value) {
$this->prop_set($name, $value);
}
$this->type_correct();
$this->_trace = true;
$this->_new = false;
return $this;
}
private function type_correct() {
foreach (static::$_meta as $name => $type) {
if (is_null($this->_properties[$name]))
continue;
switch ($type) {
case 'str':
case 'string':
$this->_properties[$name] = strval($this->_properties[$name]);
break;
case 'int':
case 'integer':
$this->_properties[$name] = intval($this->_properties[$name]);
break;
case 'flt':
case 'dbl':
case 'real':
case 'float':
case 'double':
$this->_properties[$name] = floatval($this->_properties[$name]);
break;
case 'bool':
case 'boolean':
$this->_properties[$name] = boolval($this->_properties[$name]);
break;
}
}
}
public function load($id, $clone=false) {
if ($clone) {
$r = $this->medoo->fetch_class(get_class($this), [false])
->get($this->get_table_name(), '*', ['id' => $id]);
if ($r instanceof Model) {
$r->type_correct();
$r->_trace = true;
$r->_new = false;
return $r;
}
} else {
$r = $this->medoo->get($this->get_table_name(), '*', ['id' => $id]);
if (!empty($r)) {
$this->setup($r);
return $this;
}
}
return null;
}
public function one($where = null, $clone = false) {
if ($clone) {
$r = $this->medoo->fetch_class(get_class($this), [false])
->get($this->get_table_name(), '*', $where);
if ($r instanceof Model) {
$r->type_correct();
$r->_trace = true;
$r->_new = false;
return $r;
}
} else {
$r = $this->medoo->get($this->get_table_name(), '*', $where);
if (!empty($r)) {
$this->setup($r);
return $this;
}
}
return null;
}
public function many($where = null) {
$r = $this->medoo->fetch_class(get_class($this), [false])
->select($this->get_table_name(), '*', $where);
if (!empty($r)) {
/** @var Model $o */
foreach ($r as &$o) {
$o->type_correct();
$o->_trace = true;
$o->_new = false;
}
return $r;
}
return [];
}
public function count($where = null) {
return $this->medoo->count($this->get_table_name(), '*', $where);
}
public function sql($sql) {
/** @var PDOStatement $stmt */
$stmt = $this->medoo->query($sql);
$r = $stmt->fetchAll(PDO::FETCH_CLASS, get_class($this), [$this->db, false]);
if (!empty($r)) {
/** @var Model $o */
foreach ($r as &$o) {
$o->type_correct();
$o->_trace = true;
$o->_new = false;
}
return $r;
}
return [];
}
public function save() {
if ($this->_new) {
$id = $this->medoo->insert($this->get_table_name(), $this->_properties);
if (empty($this->_properties['id']))
$this->_properties['id'] = $id;
} else {
if (empty($this->_modified))
return $this;
$this->medoo->update($this->get_table_name(), $this->_modified, [
'id' => $this->_properties['id']
]);
}
$this->_modified = [];
$this->_new = false;
return $this;
}
public function remove($id = null) {
if (empty($id)) {
$id = $this->id;
}
$this->medoo->delete($this->get_table_name(), ['id' => $id]);
return $this;
}
public function get_table_name() {
return substr( // remove the Model suffix
basename( // remove the namespaces
strtolower(get_class($this)) // use the lower case class name as table name
), 0, -5
);
}
public function get_properties() {
return $this->_properties;
}
/**
* @return medoo
*/
private function get_medoo() {
return self::$_medoo;
}
public function prop_set($name, $value) {
if ($this->_trace
&& array_key_exists($name, $this->_properties)
&& $this->_properties[$name] !== $value
) {
$this->_modified[$name] = $value;
}
$this->_properties[$name] = $value;
return $this;
}
protected function export() {
$exported = array_keys($this->_properties);
$plus = [];
$diff = [];
foreach ($this->_exported as $act) {
if ($act[0] == '+') {
$plus[] = trim(substr($act, 1));
} elseif ($act[0] == '-') {
$diff[] = trim(substr($act, 1));
}
}
return array_diff(array_merge($exported, $plus), $diff);
}
/**
* @param string $name
* @return mixed
*/
public function __get($name) {
$getter = "get_$name";
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (array_key_exists($name, $this->_properties)) {
return $this->_properties[$name];
} else {
return null;
}
}
public function __set($name, $value) {
$setter = "set_$name";
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (array_key_exists($name, $this->_properties)) {
$this->prop_set($name, $value);
}
}
public function __isset($name) {
$getter = "get_$name";
return
method_exists($this, $getter) ||
array_key_exists($name, $this->_properties) ||
isset($this->$name);
}
public function __sleep() {
return ['_properties', '_modified', '_new', '_trace', '_exported'];
}
public function __toString() {
return $this->table_name . '_' . $this->id;
}
/// ArrayAccess methods
/**
* Whether a offset exists
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @param mixed $offset <p>
* An offset to check for.
* </p>
* @return boolean true on success or false on failure.
* </p>
* <p>
* The return value will be casted to boolean if non-boolean was returned.
* @since 5.0.0
*/
public function offsetExists($offset) {
return $this->__isset($offset);
}
/**
* Offset to retrieve
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @param mixed $offset <p>
* The offset to retrieve.
* </p>
* @return mixed Can return all value types.
* @since 5.0.0
*/
public function offsetGet($offset) {
return $this->__get($offset);
}
/**
* Offset to set
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @param mixed $offset <p>
* The offset to assign the value to.
* </p>
* @param mixed $value <p>
* The value to set.
* </p>
* @return void
* @since 5.0.0
*/
public function offsetSet($offset, $value) {
$this->__set($offset, $value);
}
/**
* Offset to unset
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset <p>
* The offset to unset.
* </p>
* @return void
* @since 5.0.0
*/
public function offsetUnset($offset) {
// Nothing to do
}
/// IteratorAggregate methods
/**
* Retrieve an external iterator
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
* @return Traversable An instance of an object implementing <b>Iterator</b> or
* <b>Traversable</b>
* @since 5.0.0
*/
public function getIterator() {
$export = $this->export();
foreach ($export as $name) {
yield $name => $this->$name;
}
}
/// JsonSerializable methods
/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize() {
$result = [];
foreach ($this->getIterator() as $name => $value) {
$result[$name] = $value;
}
return $result;
}
public static function validate(&$data, $validates=null) {
$defaultSetting = [
'type' => 'string',
'default' => null,
'canBeNull' => true, // if false, null value would be convert automatically
'hash' => false,
'trim' => true, // trim before checking
'emptyCheck' => 'empty', // callable or false to disable empty check
'onEmpty' => 'set',// set/return define onEmptyReturn if use return
'onEmptySet' => '__DEFAULT__', //set as default value
// an array if enable error check
// first element can be 'match', 'contain', 'is', 'not', 'eq', 'ne', 'lt', 'gt', 'el', 'eg', 'call'
// second element should be:
// regular expression for match
// comparable value for is/not/eq/ne/lt/gt/el/eg
// callable for call, pass current checking value , entire $data and current checking $key, expecting bool return.
'errorCheck' => false,
// if errorCheck is enabled, onError can be set or return
// onErrorSet and onErrorReturn would be use.
];
if (empty($validates))
$validates = static::$_validate;
foreach ($validates as $name => $vDef) {
if ($name == '__DEFAULT_RETURN__') continue;
if (!array_key_exists($name, $data)) continue;
$validate = array_merge($defaultSetting, $vDef);
$value = &$data[$name];
if (is_string($value) && $validate['trim']) {
$value = trim($value);
}
if (!is_null($value) || !$validate['canBeNull']) {
switch ($validate['type']) {
case 'str':
case 'string':
$value = strval($value);
break;
case 'int':
case 'integer':
$value = intval($value);
break;
case 'flt':
case 'dbl':
case 'real':
case 'float':
case 'double':
$value = floatval($value);
break;
case 'bool':
case 'boolean':
$value = boolval($value);
break;
}
}
if ($validate['emptyCheck'] === 'empty') {
$isEmpty = empty($value);
} elseif (is_callable($validate['emptyCheck'])) {
$isEmpty = call_user_func($validate['emptyCheck'], $value);
} else {
$isEmpty = false;
}
if ($isEmpty) {
$onEmpty = strtolower($validate['onEmpty']);
if ($onEmpty === 'set') {
$value = $validate['onEmptySet'] === '__DEFAULT__' ?
$validate['default'] : $validate['onEmptySet'];
continue; //skip error check
} elseif ($onEmpty === 'return') {
return $validate['onEmptyReturn'];
} else {
throw new Exception('Unknown onEmpty action for validating ' . $name);
}
}
if (is_array($validate['errorCheck'])) {
list($operation, $param) = $validate['errorCheck'];
switch ($operation) {
case 'match':
$noError = preg_match($param, $value) ? true : false;
break;
case 'contain':
$noError = strpos($value, $param) !== false;
break;
case 'call':
if (is_callable($param))
$noError = call_user_func($param, $value, $data, $name) !== false;
else
throw new Exception('Error check function for ' . $name . ' is not callable');
break;
case 'is':
case '===':
$noError = $value === $param;
break;
case 'not':
case '!==':
$noError = $value !== $param;
break;
case 'eq':
case '==':
$noError = $value == $param;
break;
case 'ne':
case '!=':
$noError = $value != $param;
break;
case 'between':
case '<>':
$min = min($param);
$max = max($param);
$noError = $value >= $min && $value <= $max;
break;
case 'gt':
case '>':
$noError = $value > $param;
break;
case 'lt':
case '<':
$noError = $value < $param;
break;
case 'eg':
case '>=':
$noError = $value >= $param;
break;
case 'el':
case '<=':
$noError = $value <= $param;
break;
default:
throw new Exception("Unknown check operation for $name");
}
} else {
$noError = true;
}
if (!$noError) {
$onError = strtolower($validate['onError']);
if ($onError === 'set') {
$value = $validate['onErrorSet'] === '__DEFAULT__' ?
$validate['default'] : $validate['onErrorSet'];
} elseif ($onError === 'return') {
return $validate['onErrorReturn'];
}
}
}
return $validates['__DEFAULT_RETURN__'] ?? true;
}
public function set_exported(array $exp) {
$this->_exported = $exp;
}
public function get_exported():array {
return $this->_exported;
}
}