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