1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HnrAzevedo\Datamanager; |
4
|
|
|
|
5
|
|
|
use HnrAzevedo\Datamanager\DatamanagerException; |
6
|
|
|
|
7
|
|
|
trait CheckTrait{ |
8
|
|
|
|
9
|
|
|
protected function check_where_array(array $where) |
10
|
|
|
{ |
11
|
|
|
if(count($where) != 3){ |
12
|
|
|
throw new DatamanagerException("Condition where set incorrectly: ".implode(' ',$where)); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
if(!array_key_exists($where[0],$this->data) && $this->full){ |
16
|
|
|
throw new DatamanagerException("{$where[0]} field does not exist in the table {$this->table}."); |
17
|
|
|
} |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
protected function isSettable(string $prop) |
21
|
|
|
{ |
22
|
|
|
if($this->full && !array_key_exists($prop,$this->data)){ |
23
|
|
|
throw new DatamanagerException("{$prop} field does not exist in the table {$this->table}."); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function checkLimit() |
28
|
|
|
{ |
29
|
|
|
if(is_null($this->limit)){ |
30
|
|
|
throw new DatamanagerException("The limit must be set before the offset."); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function checkMaxlength(string $field, $val , $max) |
35
|
|
|
{ |
36
|
|
|
if(strlen($val) > $max){ |
37
|
|
|
throw new DatamanagerException("The information provided for column {$field} of table {$this->table} exceeded that allowed."); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function upgradeable(string $field): bool |
42
|
|
|
{ |
43
|
|
|
return (($this->data[$field]['changed'] && $this->data[$field]['upgradeable']) || $this->primary === $field); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function isIncremented(string $field): bool |
47
|
|
|
{ |
48
|
|
|
return ( strstr($this->data[$field]['extra'],'auto_increment') && $field === $this->primary ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function checkForChanges(): bool |
52
|
|
|
{ |
53
|
|
|
$hasChanges = false; |
54
|
|
|
foreach($this->data as $data){ |
55
|
|
|
if($data['changed']){ |
56
|
|
|
$hasChanges = true; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
if(!$hasChanges){ |
60
|
|
|
throw new DatamanagerException('There were no changes to be saved in the database.'); |
61
|
|
|
} |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function checkUniques($data) |
66
|
|
|
{ |
67
|
|
|
foreach($this->data as $d => $dd){ |
68
|
|
|
if($dd['key'] === 'UNI'){ |
69
|
|
|
$exist = $this->find()->where([$d,'=',$data[$d]])->only('id')->execute()->getCount(); |
70
|
|
|
if($exist > 0){ |
71
|
|
|
throw new DatamanagerException("A record with the same {$this->getField($d)} already exists."); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|