NanoFramework 1.4.0
A tiny PHP Framework
Model.php
Go to the documentation of this file.
1<?php
2
4
5use SteeveDroz\Fake\FakeDb;
6
12abstract class Model implements ModelInterface
13{
17 protected $table;
18
22 protected $validation;
23
27 protected $useTimestamps = true;
28
32 private FakeDb $fakeDb;
33
37 private array $whereCriteria = [];
38
44 public function __construct()
45 {
46 if (null == $this->table) {
47 throw new \Exception('You must specify the table');
48 }
49 $this->fakeDb = new FakeDb(DotEnv::getEnv('FAKEDB_LOCATION') ?? 'db');
50 }
51
52 public function findAll(): array
53 {
54 return $this->fakeDb->findWhere($this->table, $this->whereCriteria);
55 }
56
57 public function find(int $id): ?array
58 {
59 return $this->fakeDb->find($this->table, $id);
60 }
61
62 public function insert(array $data): void
63 {
64 if ($this->useTimestamps) {
65 $data['created_at'] = self::now();
66 $data['updated_at'] = self::now();
67 }
68 $this->fakeDb->insert($this->table, $data);
69 $this->whereCriteria = [];
70 }
71
72 public function update(int $id, array $data): void
73 {
74 $data['id'] = $id + 0;
75 $foundData = $this->fakeDb->find($this->table, $id);
76 $data['created_at'] = $foundData['created_at'];
77 $data['updated_at'] = self::now();
78 $this->where('id', $id)->fakeDb->update($this->table, $data, $this->whereCriteria);
79 $this->whereCriteria = [];
80 }
81
82 public function delete(int $id): void
83 {
84 $this->where('id', $id)->fakeDb->delete($this->table, $this->whereCriteria);
85 $this->whereCriteria = [];
86 }
87
88 public function where(string $key, mixed $value): ModelInterface
89 {
90 $this->whereCriteria[$key] = $value;
91 return $this;
92 }
93
94 public function getLastInsertedId(): int
95 {
96 return $this->fakeDb->getLastInsertedId($this->table);
97 }
98
99 public function validate(array $data, string $ruleset, array &$errors = []): bool
100 {
101 if (!array_key_exists($ruleset, $this->validation)) {
102 $errors[] = ['code' => 'missing_ruleset', 'variable' => $ruleset, 'message' => 'Ruleset ' . $ruleset . ' does not exist'];
103
104 return false;
105 }
106
107 foreach ($this->validation[$ruleset] as $field => $rules) {
108 if (!array_key_exists($field, $data)) {
109 $errors[] = ['code' => 'missing', 'variable' => $field, 'message' => 'Field ' . $field . ' is missing'];
110
111 continue;
112 }
113
114 $rulesArray = explode('|', $rules);
115 foreach ($rulesArray as $rule) {
116 $ruleParts = explode(':', $rule);
117 $name = $ruleParts[0];
118 $variables = $ruleParts[1] ?? '';
119 if (!method_exists($this, $name)) {
120 $errors[] = ['code' => 'unknown_rule', 'variable' => $name, 'message' => $name . ' is not a known validation rule'];
121
122 break;
123 }
124
125 $error = 'There was a problem with field ' . $field;
126 if (!call_user_func_array([$this, $name], [$field, $data, $variables, &$error])) {
127 $errors[] = ['code' => $name, 'variable' => $field, 'message' => $error];
128
129 break;
130 }
131 }
132 }
133
134 return 0 == count($errors);
135 }
136
142 public static function now(): string
143 {
144 return date('Y-m-d H:i:s');
145 }
146
157 protected function min_length(string $field, array $data, string $params, &$error): bool
158 {
159 if (strlen($data[$field]) < $params) {
160 $error = 'The field ' . $field . ' must contain at least ' . $params . ' characters';
161
162 return false;
163 }
164
165 return true;
166 }
167
178 protected function max_length(string $field, array $data, string $params, &$error): bool
179 {
180 if (strlen($data[$field]) > $params) {
181 $error = 'The field ' . $field . ' must contain at most ' . $params . ' characters';
182
183 return false;
184 }
185
186 return true;
187 }
188
199 protected function valid_email(string $field, array $data, string $params, &$error): bool
200 {
201 if (!filter_var($data[$field], FILTER_VALIDATE_EMAIL)) {
202 $error = 'The field ' . $field . ' must contain a valid email address';
203
204 return false;
205 }
206
207 return true;
208 }
209
220 protected function match(string $field, array $data, string $params, &$error): bool
221 {
222 if ($data[$field] !== $data[$params]) {
223 $error = 'The field ' . $field . ' must be identical to the field ' . $params;
224
225 return false;
226 }
227
228 return true;
229 }
230
241 protected function required(string $field, array $data, string $params, &$error): bool
242 {
243 if (0 == strlen($data[$field])) {
244 $error = 'The field ' . $field . ' is required';
245
246 return false;
247 }
248
249 return true;
250 }
251}
static getEnv(string $key)
Definition: DotEnv.php:74
Connects to the database.
Definition: Model.php:13
match(string $field, array $data, string $params, &$error)
Definition: Model.php:220
max_length(string $field, array $data, string $params, &$error)
Definition: Model.php:178
valid_email(string $field, array $data, string $params, &$error)
Definition: Model.php:199
where(string $key, mixed $value)
Definition: Model.php:88
min_length(string $field, array $data, string $params, &$error)
Definition: Model.php:157
update(int $id, array $data)
Definition: Model.php:72
required(string $field, array $data, string $params, &$error)
Definition: Model.php:241
validate(array $data, string $ruleset, array &$errors=[])
Definition: Model.php:99
Interface that the user can implement in order to create a new Model, based on a real ODBC.