1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimplePHP\Model; |
4
|
|
|
|
5
|
|
|
use SimplePHP\Root\Connection; |
6
|
|
|
use PDO; |
7
|
|
|
use PDOException; |
8
|
|
|
use Exception; |
9
|
|
|
use stdClass; |
10
|
|
|
use SimplePHP\Model\CRUD as Actions; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class SimplePHP |
14
|
|
|
* @package NicollasSilva\SimplePHP |
15
|
|
|
*/ |
16
|
|
|
class SimplePHP extends Connection { |
17
|
|
|
use Actions; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $sentence = ''; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $offset; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $order; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
protected $params = '*'; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
protected $where; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
protected $limit; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
protected $table; |
39
|
|
|
|
40
|
|
|
/** @var object|null */ |
41
|
|
|
protected $data; |
42
|
|
|
|
43
|
|
|
/** @var bool */ |
44
|
|
|
protected $type; |
45
|
|
|
|
46
|
|
|
/** @var array */ |
47
|
|
|
protected $excepts = []; |
48
|
|
|
|
49
|
|
|
/** @var string */ |
50
|
|
|
protected $primary; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get tablename of children model |
54
|
|
|
* @param string|null $tableName |
55
|
|
|
*/ |
56
|
|
|
function __construct(?string $tableName, ?string $primaryKey) |
57
|
|
|
{ |
58
|
|
|
parent::__construct(); $this->table = $tableName; $this->primary = $primaryKey; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param int|null $id |
63
|
|
|
* @return SimplePHP |
64
|
|
|
*/ |
65
|
|
|
public function find(?int $id = null): SimplePHP |
66
|
|
|
{ |
67
|
|
|
is_int($id) ? self::where('id', $id) : null; |
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $condition |
73
|
|
|
* @param string $value |
74
|
|
|
* @return SimplePHP|null |
75
|
|
|
*/ |
76
|
|
|
public function where(string $condition, string $value): SimplePHP |
77
|
|
|
{ |
78
|
|
|
$this->where = "WHERE " . (mb_strlen($this->where > 6) ? "&& {$condition} = '{$value}'" : "{$condition} = '{$value}'"); |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $params |
84
|
|
|
* @return SimplePHP|null |
85
|
|
|
*/ |
86
|
|
|
public function only(array $params): SimplePHP |
87
|
|
|
{ |
88
|
|
|
$params !== null ? $this->params = implode($params, ',') : $this->params = '*'; |
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param int $limit |
94
|
|
|
* @return SimplePHP|null |
95
|
|
|
*/ |
96
|
|
|
public function limit(int $limit): SimplePHP |
97
|
|
|
{ |
98
|
|
|
$this->limit = "LIMIT $limit"; |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param int $offset |
104
|
|
|
* @return SimplePHP|null |
105
|
|
|
*/ |
106
|
|
|
public function offset(int $offset): SimplePHP |
107
|
|
|
{ |
108
|
|
|
$this->offset = "OFFSET $offset"; |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $order |
114
|
|
|
* @return SimplePHP|null |
115
|
|
|
*/ |
116
|
|
|
public function orderBy(string $order): SimplePHP |
117
|
|
|
{ |
118
|
|
|
$this->order = "ORDER BY {$order}"; |
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $name |
124
|
|
|
* @param mixed $arguments |
125
|
|
|
* @return string error |
126
|
|
|
*/ |
127
|
|
|
public function __call(string $name, $arguments) |
128
|
|
|
{ |
129
|
|
|
return "This method does not exist at the SimplePHP: \"<b>{$name}</b>\"."; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param bool $bool |
134
|
|
|
* @return SimplePHP |
135
|
|
|
*/ |
136
|
|
|
public function asAttribute(bool $bool = false): SimplePHP |
137
|
|
|
{ |
138
|
|
|
$this->type = $bool; |
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param array $deniable |
144
|
|
|
* @return SimplePHP |
145
|
|
|
*/ |
146
|
|
|
public function except(array $deniable) |
147
|
|
|
{ |
148
|
|
|
$this->excepts = $deniable; |
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function __set($prop, $value) |
153
|
|
|
{ |
154
|
|
|
if (empty($this->data)) { |
155
|
|
|
$this->data = new stdClass(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$this->data->$prop = $value; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param $name |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
public function __isset($attribute): bool |
166
|
|
|
{ |
167
|
|
|
return isset($this->data->$attribute); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Method to destroy @except method |
172
|
|
|
*/ |
173
|
|
|
private function deny() |
174
|
|
|
{ |
175
|
|
|
if(!empty($this->excepts)) { |
176
|
|
|
switch (!is_object($this->data) && $count = count($this->data)) { |
177
|
|
|
case (!isset($this->data[0]) && !empty($this->data)): |
178
|
|
|
foreach($this->excepts as $except) { |
179
|
|
|
if(isset($this->data[$except])) unset($this->data[$except]); |
180
|
|
|
} |
181
|
|
|
break; |
182
|
|
|
case ($count >= 2 && isset($this->data[0])): |
183
|
|
|
foreach($this->excepts as $except) { |
184
|
|
|
for($i = 0; $i < $count; $i++) { |
185
|
|
|
if(isset($this->data[$i][$except])) unset($this->data[$i][$except]); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
break; |
189
|
|
|
default: |
190
|
|
|
return []; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return array|mixed |
197
|
|
|
*/ |
198
|
|
|
public function execute() |
199
|
|
|
{ |
200
|
|
|
try { |
201
|
|
|
$execute = $this->conn->query("SELECT {$this->params} FROM {$this->table} {$this->where} {$this->order} {$this->limit} {$this->offset}"); |
|
|
|
|
202
|
|
|
$execute->rowCount() > 1 ? |
203
|
|
|
$this->data = ($this->type ? $execute->fetchAll(PDO::FETCH_CLASS, static::class) : $execute->fetchAll(PDO::FETCH_ASSOC)) : |
204
|
|
|
$this->data = ($this->type ? $execute->fetchObject(static::class) : $execute->fetch(PDO::FETCH_ASSOC)); |
205
|
|
|
$this->deny(); |
206
|
|
|
return $this->data; |
207
|
|
|
} catch(PDOException $exc) { |
208
|
|
|
return $exc->getMessage(); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return Exception|bool |
214
|
|
|
*/ |
215
|
|
|
public function destroy() { |
216
|
|
|
$primary = $this->primary; |
217
|
|
|
if(!isset($this->data->$primary)) { |
218
|
|
|
throw new Exception("|| Índice primário não encontrado: {$primary} ||"); |
219
|
|
|
} |
220
|
|
|
return $this->delete($this->data->$primary); |
221
|
|
|
} |
222
|
|
|
} |