Passed
Push — master ( 7a1768...367980 )
by Henri
06:54
created

CheckTrait::checkForChanges()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 12
rs 10
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