1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HnrAzevedo\Datamanager; |
4
|
|
|
|
5
|
|
|
trait DataTrait{ |
6
|
|
|
use CrudTrait, CheckTrait; |
7
|
|
|
|
8
|
|
|
protected ?string $table = null; |
9
|
|
|
protected ?string $primary = null; |
10
|
|
|
protected array $data = []; |
11
|
|
|
protected bool $full = false; |
12
|
|
|
|
13
|
|
|
protected array $result = []; |
14
|
|
|
protected ?string $clause = null; |
15
|
|
|
|
16
|
|
|
protected ?string $order = null; |
17
|
|
|
protected ?string $limit = null; |
18
|
|
|
protected ?int $offset = null; |
19
|
|
|
protected array $excepts = []; |
20
|
|
|
protected int $count = 0; |
21
|
|
|
protected array $select = []; |
22
|
|
|
protected ?string $query = null; |
23
|
|
|
|
24
|
|
|
protected function mountRemove(): array |
25
|
|
|
{ |
26
|
|
|
$return = ['data' => '', 'where' => '']; |
27
|
|
|
foreach($this->where as $clause => $condition){ |
28
|
|
|
if(strlen($clause) === 0){ |
29
|
|
|
$return['where'] .= " {$clause} {$condition[0]} {$condition[1]} :q_{$condition[0]} "; |
30
|
|
|
$return['data'] .= "q_{$condition[0]}={$condition[2]}&"; |
31
|
|
|
continue; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
foreach($condition as $value){ |
35
|
|
|
$return['where'] .= " {$clause} {$value[0]} {$value[1]} :q_{$value[0]} "; |
36
|
|
|
$return['data'] .= "q_{$value[0]}={$value[2]}&"; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
return $return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function mountSave(): array |
43
|
|
|
{ |
44
|
|
|
$return = ['data' => []]; |
45
|
|
|
|
46
|
|
|
foreach ($this->data as $key => $value) { |
47
|
|
|
if($this->upgradeable($key) && !$this->isIncremented($key)){ |
48
|
|
|
$return['data'][$key] = $this->data[$key]['value']; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function mountWhereExec(): array |
56
|
|
|
{ |
57
|
|
|
$return = ['where' => '', 'data' => []]; |
58
|
|
|
foreach ($this->where as $key => $value) { |
59
|
|
|
|
60
|
|
|
$key = (!$key) ? '' : " {$key} "; |
61
|
|
|
|
62
|
|
|
if(!is_array($value[0])){ |
63
|
|
|
$return['where'] .= " {$key} {$value[0]} {$value[1]} :q_{$value[0]} "; |
64
|
|
|
$return['data']["q_{$value[0]}"] = $value[2]; |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
for($i = 0; $i < count($value); $i++){ |
|
|
|
|
69
|
|
|
|
70
|
|
|
if(!is_array($value[$i][2])){ |
71
|
|
|
$return['where'] .= " {$key} {$value[$i][0]} {$value[$i][1]} :q_{$value[$i][0]} "; |
72
|
|
|
$return['data']["q_{$value[$i][0]}"] = $value[$i][2]; |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$return['where'] .= " {$key} {$value[$i][0]} {$value[$i][1]} ("; |
77
|
|
|
|
78
|
|
|
foreach($value[$i][2] as $v => $valu){ |
79
|
|
|
$return['where'] .= " :q_{$value[$i][0]}_{$v},"; |
80
|
|
|
$return['data']["q_{$value[$i][0]}_{$v}"] = $valu; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$return['where'] = substr($return['where'],0,-1) .') '; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
return $return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function mountBetweenExec(): array |
90
|
|
|
{ |
91
|
|
|
$return = ['where' => '', 'data' => []]; |
92
|
|
|
|
93
|
|
|
foreach($this->between as $field => $value){ |
94
|
|
|
$condition = (count(explode(' ',$field)) > 2) ? ' '.explode(' ',$field)[0].' ' : ' AND '; |
95
|
|
|
$field = str_replace(['AND','OR',' '],'',$field); |
96
|
|
|
$return['where'] .= " {$condition} {$field} BETWEEN :q_1{$field} AND :q_2{$field} "; |
97
|
|
|
$return['data'] = [ |
98
|
|
|
"q_1{$field}" => (date_format( date_create_from_format(DATAMANAGER_CONFIG['dateformat'],$value[0]) , 'Y-m-d')), |
99
|
|
|
"q_2{$field}" => (date_format( date_create_from_format(DATAMANAGER_CONFIG['dateformat'],$value[1]) , 'Y-m-d')) |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
return $return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function mountSelect() |
106
|
|
|
{ |
107
|
|
|
$select = implode(',',array_keys($this->select)); |
108
|
|
|
|
109
|
|
|
$this->query = str_replace('*', $select,$this->query); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function mountLimit() |
113
|
|
|
{ |
114
|
|
|
if(!is_null($this->limit)){ |
115
|
|
|
$this->query .= " LIMIT {$this->limit}"; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function mountOffset() |
120
|
|
|
{ |
121
|
|
|
if(!is_null($this->offset)){ |
122
|
|
|
$this->query .= " OFFSET {$this->offset}"; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: