/
Record.class.php
executable file
·1185 lines (876 loc) · 32 KB
/
Record.class.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Disco\classes;
/**
* Abstract Record class. Treats an object as a table row (Active Record/ORM). Use the Disco CLI tool to generate your records.
*
* Records:
* - protects against missing ids
* - making sure fields conform to the data type set forth by the table
* - Protect against attempting to update fields that don't exist
* - only updating changed fields to prevent redundancy and database load
*
*
* Records throw
* - {@link \Disco\exceptions\Record} All record exceptions extend this exception, general exception.
* - {@link \Disco\exceptions\RecordValidation} Validation failed.
* - {@link \Disco\exceptions\RecordId} Primary Key Ids are missing or null.
* - {@link \Disco\exceptions\RecordNonExistent} When the record doesn't exist.
*/
abstract class Record implements \ArrayAccess {
/**
* @var int VALIDATION_STRICT Field types must be supplied in a way that would not cause loss of data when
* being inserted.
*/
const VALIDATION_STRICT = 2;
/**
* @var int VALIDATION_EMIT Error will be emiited if inserting will cause data loss.
*/
const VALIDATION_EMIT = 1;
/**
* @var int VALIDATION_LOOSE Columns must only adhere to base type with no regard for whether inserting would
* cause data loss.
*/
const VALIDATION_LOOSE = 0;
/**
* @var int $validationLevel The level of validation to apply to columns when perfomring data manipulation.
*/
private static $validationLevel = 0;
/**
* @var string $model The model that owns the record.
*/
protected $model;
/**
* @var array $fieldDefintions The fields defintions for the record.
*/
protected $fieldDefinitions = Array();
/**
* @var boolean|string $autoIncrementField The autoincrement field name.
*/
protected $autoIncrementField = false;
/**
* @var array $fields The fields of the record.
*/
protected $fields = Array();
/**
* @var array $cache A Cache of the fields of the record.
*/
private $cache = Array();
/**
* @var boolean|array $cache The primary keys that were present when the record was constructed. Only used when
* allowKeyUpdates is set to true and a record is updated and some of the records primary keys have changed.
* This prevents the record from attempting to update its self with the new primary key values, which will
* result in the updates where statement using conditions pointing to a record that doesn't exist, or is wrong
* all togethor.
*/
private $initial_primary_keys = false;
/**
* @var boolean $allowKeyUpdates Allow primary keys of the record to be updated?
*/
private $allowKeyUpdates = false;
/**
* @var array $numericalRanges Store mins and maxes for SQL column types. Used when validating fields of
* records.
*/
private static $numericalRanges = Array(
'tinyint' => Array(
'signed' => Array(
'min' => -128,
'max' => 127,
),
'unsigned' => Array(
'min' => 0,
'max' => 255,
),
),
'smallint' => Array(
'signed' => Array(
'min' => -32768,
'max' => 32767,
),
'unsigned' => Array(
'min' => 0,
'max' => 65535,
),
),
'mediumint' => Array(
'signed' => Array(
'min' => -8388608,
'max' => 8388607,
),
'unsigned' => Array(
'min' => 0,
'max' => 16777215,
),
),
'int' => Array(
'signed' => Array(
'min' => -2147483648,
'max' => 2147483647,
),
'unsigned' => Array(
'min' => 0,
'max' => 4294967295,
),
),
'bigint' => Array(
'signed' => Array(
'min' => -9223372036854775808,
'max' => 9223372036854775807,
),
'unsigned' => Array(
'min' => 0,
'max' => 18446744073709551615,
),
),
'tinytext' => 255,
'text' => 65535,
'mediumtext' => 16777215,
'longtext' => 4294967295,
'tinyblob' => 255,
'blob' => 65535,
'mediumblob' => 16777215,
'longblob' => 4294967295,
);
/**
* @var array $regexHelpers Helpers for validating date and time.
*/
private $regexHelpers = Array(
'date' => '\d{4}-\d{2}-\d{2}',
'time' => '\d{2}:\d{2}:\d{2}',
);
/**
* Set the intital fields of the record. Performs an array intersection to only take fields that exist on the
* model from the passed fields.
*
*
* @param array $fields Fields of the record.
* @param boolean $cache Whether to cache the initial passed fields.
*
* @return void
*/
public function __construct($fields = Array(), $cache = false){
if(is_object($fields)){
$fields = (array) $fields;
}//if
$this->fields = array_intersect_key($fields,array_flip($this->getFieldNames()));
try {
$this->initial_primary_keys = $this->primaryKeysWithValidation();
} catch(\Disco\exceptions\RecordId $e){
$this->initial_primary_keys = false;
}//catch
if($cache === true){
$this->cache = $this->fields;
}//if
}//__construct
/**
* Set the level of validation applied to record columns, based on one of the class constants:
* - VALIDATION_LOOSE
* - VALIDATION_EMIT
* - VALIDATION_STRICT
*
* @param int $lvl The validation level.
*/
public static function setValidationLevel($lvl){
if($lvl === self::VALIDATION_LOOSE){
self::$validationLevel = self::VALIDATION_LOOSE;
} else if($lvl === self::VALIDATION_EMIT){
self::$validationLevel = self::VALIDATION_EMIT;
} else if($lvl === self::VALIDATION_STRICT){
self::$validationLevel = self::VALIDATION_STRICT;
}//elif
}//setValidationLevel
/**
* Get a field of the record using object syntax.
*
*
* @param string $key The key.
*
* @return mixed
*/
public function __get($key){
return $this->fields[$key];
}//__get
/**
* Set a field of the record using object syntax.
*
*
* @param string $key The key.
* @param mixed $value The value.
*/
public function __set($key,$value){
$this->fields[$key] = $value;
}//__set
/**
* Determine whether a field exists in the record.
*
*
* @param string $key The key.
*
* @return boolean
*/
public function __isset($key){
return isset($this->fields[$key]);
}//__isset
/**
* Delete a field from the record (Set it to null).
*
*
* @param string $key The key.
*/
public function __unset($key){
if(isset($this->fields[$key])){
$this->fields[$key] = null;
}//if
}//__unset
/**
* Get a field of the record using array access syntax.
*
*
* @param string $key The key.
*
* @return mixed
*/
public function offsetGet($key){
return $this->offsetExists($key) ? $this->fields[$key] : null;
}//offsetGet
/**
* Set a field of the record using array access syntax.
*
*
* @param string $key The key.
* @param mixed $value The value.
*/
public function offsetSet($key,$value){
$this->fields[$key] = $value;
}//offsetSet
/**
* Determine whether a field exists in the record.
*
*
* @param string $key The key.
*
* @return boolean
*/
public function offsetExists($key){
return isset($this->fields[$key]);
}//offsetExists
/**
* Delete a field from the record (Set it to null).
*
*
* @param string $key The key.
*/
public function offsetUnset($key){
if($this->offsetExists($key)){
unset($this->fields[$key]);
}//if
}//offsetUnset
/**
* Allow primary keys of the record to be updated. This is by default false, and will be made false again after
* every update.
*
*
* @param boolean $allow
* @return void
*/
public function allowKeyUpdates($allow = true){
$this->allowKeyUpdates = $allow;
}//allowKeyUpdates
/**
* Update the changed fields in the record.
*
*
* @param boolean $using_only_auto_increment_key Perform the update using only the auto-increment key,
* regardless of other primary keys specified by the record.
*
* @return boolean
*
* @throws \Disco\exceptions\RecordId When primary keys are null.
* @throws \Disco\exceptions\RecordValidation When a field fails validation.
*/
public function update($using_only_auto_increment_key = false){
$this->validateFields();
$update = $this->diff();
if(!count($update)){
return true;
}//if
$ids = $this->primaryKeysWithValidation();
//without ids
if(!$this->allowKeyUpdates){
$update = array_diff_key($update,$ids);
}//if
else {
$initial_keys = $this->initial_primary_keys;
if($initial_keys === false){
$class = get_called_class();
throw new \Disco\exceptions\RecordId("Record `{$class}` cannot update with `allowKeyUpdates` due to initial primary keys missing/not validating during record instantiation");
}//if
$this->initial_primary_keys = $ids;
$ids = $initial_keys;
$this->allowKeyUpdates(false);
}//el
if($using_only_auto_increment_key === true){
$ai = $this->autoIncrementField();
if(!$ai){
$class = get_called_class();
throw new \Disco\exceptions\RecordId("Record `{$class}` cannot update using only auto increment key as none are defined");
}//if
$ids = Array($ai => $ids[$ai]);
}//if
$res = \App::with($this->model)->update($update)->where($ids)->finalize();
if($res){
$this->cache = array_merge($this->cache,$update);
}//if
return $res;
}//update
/**
* Delete the record.
*
*
* @param boolean $using_only_auto_increment_key Perform the delete using only the auto-increment key,
* regardless of other primary keys specified by the record.
*
* @return boolean
*
* @throws \Disco\exceptions\RecordId When primary keys are null.
*/
public function delete($using_only_auto_increment_key = false){
$ids = $this->primaryKeysWithValidation();
if($using_only_auto_increment_key === true){
$ai = $this->autoIncrementField();
if(!$ai){
$class = get_called_class();
throw new \Disco\exceptions\RecordId("Record `{$class}` cannot update using only auto increment key as none are defined");
}//if
$ids = Array($ai => $ids[$ai]);
}//if
return \App::with($this->model)->delete($ids);
}//delete
/**
* Create the record. If the record has an Auto Increment Primary Key it will be set on the record with the
* newly created id.
*
*
* @return int
*
* @throws \Disco\exceptions\RecordValidation When a field fails validation or is required and missing.
*/
public function insert(){
$ai = $this->autoIncrementField();
if($ai && array_key_exists($ai,$this->fields) && $this->fields[$ai] === null){
unset($this->fields[$ai]);
}//if
$this->validateFields();
$missing = array_diff($this->getRequiredFieldNames(), array_keys($this->fields));
if(count($missing)){
$missing = implode(', ',$missing);
$class = get_called_class();
throw new \Disco\exceptions\RecordValidation("Record `{$class}` insert error: fields `{$missing}` cannot be null");
}//if
$id = \App::with($this->model)->insert($this->fields);
if($id){
if($ai){
$this->fields[$ai] = $id;
}//if
$this->cache = array_merge($this->cache,$this->fields);
}//if
return $id;
}//insert
/**
* INSERT or UPDATE the record based on the presense or lack there of the records primary keys.
*
* @return boolean|int
*
* @throws \Disco\exceptions\Record When required fields are null.
*/
public function upsert(){
if(!$this->exists()){
return $this->insert();
}//if
return $this->update();
}//upsert
/**
* Get all the set fields of the record.
*
*
* @return array An assoc array of the fields and their values.
*/
public function getFields(){
return $this->fields;
}//getFields
/**
* Get and set all the fields of the record that are not currently set. Must have the primary keys defined.
*
*
* @return boolean|array False if no missing fields. An assoc array of the missing fields and their values otherwise.
*
* @throws \Disco\exceptions\RecordId When ids are null.
*/
public function fetchMissing(){
$missing = array_diff($this->getFieldNames(), array_keys($this->fields));
if(count($missing)){
$ids = $this->primaryKeys();
if(!count($ids) || in_array(null,array_values($ids))){
throw new \Disco\exceptions\RecordId("Record cannot fetch missing fields when there is a NULL id.");
}//if
$result = \App::with($this->model)
->select($missing)
->where($ids)
->data();
if($result->rowCount() === 0){
throw new \Disco\exceptions\RecordNonExistent('Record does not exist : ' . var_export($ids,true));
} else if($result->rowCount() > 1){
throw new \Disco\exceptions\Record('Multiple Records returned with ids : ' . var_export($ids,true));
}//elif
$fields = $result->fetch();
$this->cache = array_merge($this->cache,$fields);
$this->fields = array_merge($this->fields,$fields);
return $fields;
}//if
return false;
}//fetchMissing
/**
* Get and set fields of the record. Must have the primary keys defined.
*
*
* @param null|string|array $fields The fields to fetch and set on the record. If null is passed all fields
* will be fetched.
*
* @return array An assoc array of the records field and their values.
*
* @throws \Disco\exceptions\RecordId When ids are null.
* @throws \Disco\exceptions\RecordNonExistent When the record doesn't exist.
* @throws \Disco\exceptions\Record When multiple records are returned.
*/
public function fetch($fields = Array()){
$ids = $this->primaryKeysWithValidation();
$passed_string = false;
if(is_array($fields) && !count($fields)){
$fields = $this->getFieldNames();
} else {
if(is_string($fields)){
$passed_string = true;
$fields = Array($fields);
}//if
$diff = array_diff(array_values($fields), $this->getFieldNames());
if(count($diff)){
$fields = implode(', ',$diff);
throw new \Disco\exceptions\RecordValidation("Record field(s) `{$fields}` do not exist!");
}//if
}//el
$result = \App::with($this->model)
->select($fields)
->where($ids)
->data();
if($result->rowCount() === 0){
throw new \Disco\exceptions\RecordNonExistent('Record does not exist : ' . var_export($ids,true));
} else if($result->rowCount() > 1){
throw new \Disco\exceptions\Record('Multiple Records returned with ids : ' . var_export($ids,true));
}//elif
$row = $result->fetch();
$this->cache = array_merge($this->cache,$row);
$this->fields = array_merge($this->fields,$row);
//only wanted to fetch single field? return it.
if($passed_string){
return $row[$fields[0]];
}//if
return $row;
}//fetch
/**
* Get the primary keys and their current values from the fields.
*
*
* @return array The primary keys.
*/
public function primaryKeys(){
$ids = \App::with($this->model)->ids;
if(!is_array($ids)){
$ids = Array($ids);
}//if
$ids = array_intersect_key($this->fields,array_flip($ids));
return $ids;
}//primaryKeys
/**
* Get the primary keys and their current values from the fields, validating each one. If not all keys are
* present but the auto increment key is present and valid return just that.
*
*
* @return array The primary keys.
*
* @throws \Disco\exceptions\RecordId When any of the primary keys are null.
*/
public function primaryKeysWithValidation(){
$ids = $this->primaryKeys();
if(!count($ids) || in_array(null,array_values($ids))){
$ai = $this->autoIncrementField();
if(!$ai || !isset($this->fields[$ai]) || !$this->validate($ai,$this->fields[$ai])){
$class = get_called_class();
throw new \Disco\exceptions\RecordId("Record `{$class}` use/modification attempted with null id(s)!");
}//if
return Array($ai => $this->fields[$ai]);
}//if
return $ids;
}//primaryKeysWithValidation
/**
* Validate the current fields of the record.
*
*
* @return void
*
* @throws \Disco\exceptions\RecordValidation When a field doesn't exist or a field fails validation.
*/
public function validateFields(){
$diff = array_diff(array_keys($this->fields), $this->getFieldNames());
if(count($diff)){
$fields = implode(', ',$diff);
$class = get_called_class();
throw new \Disco\exceptions\RecordValidation("Record `{$class}` field(s) `{$fields}` do not exist!");
}//if
$errors = Array();
foreach($this->fields as $field => $value){
if(!$this->validate($field,$value)){
$errors[$field] = $value;
}//if
}//foreach
if(count($errors)){
$errorMsg = '';
foreach($errors as $k => $v){
$errorMsg .= "`{$k}` : `{$v}` | ";
}//foreach
$errorMsg = rtrim($errorMsg,' | ');
$class = get_called_class();
throw (new \Disco\exceptions\RecordValidation("Record validation error - {$class} - {$errorMsg}"))->setData($errors);
}//if
}//validateFields
/**
* Validate a single field of the record.
*
*
* @param string $key The field name to validate.
*
* @return boolean The field passed validation.
*/
public function validateField($key){
return $this->validate($key,$this[$key]);
}//validateField
/**
* Get the fields that have changed since the instantiation of the record or the last update or insert.
*
*
* @return array The fields that have changed.
*/
public function diff(){
$diff = Array();
foreach($this->fields as $field => $value){
if(!isset($this->cache[$field]) || $this->cache[$field] !== $value){
$diff[$field] = $value;
}//if
}//foreach
return $diff;
}//diff
/**
* Determine whether a record exists based on its current primary keys.
*
*
* @return boolean The record exists
*/
public function exists(){
try {
$ids = $this->primaryKeysWithValidation();
} catch(\Disco\exceptions\Record $e){
return false;
}//catch
$result = \App::with($this->model)
->select(array_keys($ids)[0])
->where($ids)
->limit(1)
->data();
if(!$result->rowCount()){
return false;
}//if
return true;
}//exists
/**
* Convert a single field, an array of fields, or all fields which are empty strings to null values.
*
*
* @param null|string|array $fields An optional subset of fields or single field to convert.
*
* @return void
*
* @throws \Disco\exceptions\RecordValidation If a passed field fails to exist.
*/
public function convertEmptyStringsToNull($fields = Array()){
if(is_string($fields)){
$fields = Array($fields);
}//if
if(!count($fields)){
$fields = $this->fields;
}//if
foreach($fields as $key => $value){
if(!isset($this->fields[$key])){
$class = get_called_class();
throw new \Disco\exceptions\RecordValidation("{$class}::convertEmptyStringsToNull() - Record field `{$key}` does not exist!");
}//if
if($value === ''){
$this->fields[$key] = null;
}//if
}//foreach
}//convertEmptyStringsToNull
/**
* Find the first record that matches the where condition.
*
*
* @param array $where The conditions used to find the record.
* @param null|string|array $select The fields to select from the record.
*
* @return boolean|mixed Return false if the record wasn't found, otherwise return an instance of the record.
*/
public static function find($where, $select = null){
$class = get_called_class();
if(!is_array($where)){
throw new \InvalidArguementException("{$class}::find() Paramater 1 `\$where` must be an array");
}//if
$record = new $class;
if($select === null){
$select = $record->getFieldNames();
} elseif(is_string($select)){
$select = Array($select);
}//elif
$result = \App::with($record->model)
->select($select)
->where($where)
->limit(1)
->data();
if(!$result->rowCount()){
return false;
}//if
return new $class($result->fetch());
}//find
/**
* Find the all record that match the where condition.
*
*
* @param array $where The conditions used to find the records.
* @param null|string|array $select The fields to select from the records.
*
* @return array An array of the records found.
*/
public static function findAll($where, $select = null){
$class = get_called_class();
if(!is_array($where)){
throw new \InvalidArguementException("{$class}::findAll() Paramater 1 `\$where` must be an array");
}//if
$record = new $class;
if($select === null){
$select = $record->getFieldNames();
} elseif(is_string($select)){
$select = Array($select);
}//elif
$result = \App::with($record->model)
->select($select)
->where($where)
->data();
$records = Array();
while($row = $result->fetch()){
$records[] = new $class($row);
}//while
return $records;
}//findAll
/**
* Determine if a value is valid for a records particular field.
*
*
* @param string \$field The field of the record.
* @param string \$v The value to test against the field.
*
* @return boolean Did it validate?
*/
public function validate($field, $v){
if(!array_key_exists($field,$this->fieldDefinitions)){
throw new \Disco\exceptions\Record("Record validation exception, record does not have a field `{$field}` to validate against");
}//if
$definition = $this->fieldDefinitions[$field];
if($v === null){
if(!$definition['null']){
return false;
}//if
return true;
}//if
//raw type?
if(is_array($v) && count($v) == 1 && isset($v['raw'])){
return true;
}//if
if(stripos($definition['type'],'int') !== false){
if(!is_numeric($v)){
return false;
}//if
if(self::$validationLevel !== self::VALIDATION_LOOSE){
if(array_key_exists('unsigned',$definition) && $definition['unsigned'] === true){
$min = self::$numericalRanges[$definition['type']]['unsigned']['min'];
$max = self::$numericalRanges[$definition['type']]['unsigned']['max'];
} else {
$min = self::$numericalRanges[$definition['type']]['signed']['min'];
$max = self::$numericalRanges[$definition['type']]['signed']['max'];
}//el
if($v < $min || $v > $max){
if(self::$validationLevel === self::VALIDATION_STRICT){
return false;
} else {
\App::log("Record VALIDATION_EMIT message : field `{$field}` ({$definition['type']}) with value `{$v}` should be between `{$min}` and `{$max}`, data will be truncated upon insert/update to conform");
}//el
}//if
}//if
return true;
}//if
else if($definition['type'] === 'float' || $definition['type'] === 'double'){
if(!is_numeric($v)){
return false;
}//if
if(self::$validationLevel !== self::VALIDATION_LOOSE){
//unsigned must be positive!
if(array_key_exists('unsigned',$definition) && $definition['unsigned'] === true && $v > 0){
if(self::$validationLevel === self::VALIDATION_STRICT){
return false;
} else {
\App::log("Record VALIDATION_EMIT message : field `{$field}` ({$definition['type']} unsigned) with value `{$v}` should be a positive number, data will be truncated upon insert/update to conform");
}//el
}//if
}//if
return true;
}//elif
else if($definition['type'] === 'decimal'){
if(!is_numeric($v)){
return false;
}//if
if(self::$validationLevel !== self::VALIDATION_LOOSE){
$max = str_repeat('9',$definition['wholeLength']) . '.' . str_repeat('9',$definition['decimalLength']);
$min = '-' . $max;
if(array_key_exists('unsigned',$definition) && $definition['unsigned'] === true){
$min = 0;
}//if
if($v < $min || $v > $max){