5use SteeveDroz\Fake\FakeDb;
32 private FakeDb $fakeDb;
37 private array $whereCriteria = [];
46 if (
null == $this->table) {
47 throw new \Exception(
'You must specify the table');
49 $this->fakeDb =
new FakeDb(
DotEnv::getEnv(
'FAKEDB_LOCATION') ??
'db');
54 return $this->fakeDb->findWhere($this->table, $this->whereCriteria);
57 public function find(
int $id): ?array
59 return $this->fakeDb->find($this->table, $id);
62 public function insert(array $data): void
64 if ($this->useTimestamps) {
68 $this->fakeDb->insert($this->table, $data);
69 $this->whereCriteria = [];
72 public function update(
int $id, array $data): void
74 $data[
'id'] = $id + 0;
75 $foundData = $this->fakeDb->find($this->table, $id);
76 $data[
'created_at'] = $foundData[
'created_at'];
78 $this->
where(
'id', $id)->fakeDb->update($this->table, $data, $this->whereCriteria);
79 $this->whereCriteria = [];
82 public function delete(
int $id):
void
84 $this->
where(
'id', $id)->fakeDb->delete($this->table, $this->whereCriteria);
85 $this->whereCriteria = [];
90 $this->whereCriteria[$key] = $value;
96 return $this->fakeDb->getLastInsertedId($this->table);
99 public function validate(array $data,
string $ruleset, array &$errors = []): bool
101 if (!array_key_exists($ruleset, $this->validation)) {
102 $errors[] = [
'code' =>
'missing_ruleset',
'variable' => $ruleset,
'message' =>
'Ruleset ' . $ruleset .
' does not exist'];
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'];
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'];
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];
134 return 0 == count($errors);
142 public static function now(): string
144 return date(
'Y-m-d H:i:s');
157 protected function min_length(
string $field, array $data,
string $params, &$error): bool
159 if (strlen($data[$field]) < $params) {
160 $error =
'The field ' . $field .
' must contain at least ' . $params .
' characters';
178 protected function max_length(
string $field, array $data,
string $params, &$error): bool
180 if (strlen($data[$field]) > $params) {
181 $error =
'The field ' . $field .
' must contain at most ' . $params .
' characters';
199 protected function valid_email(
string $field, array $data,
string $params, &$error): bool
201 if (!filter_var($data[$field], FILTER_VALIDATE_EMAIL)) {
202 $error =
'The field ' . $field .
' must contain a valid email address';
220 protected function match(
string $field, array $data,
string $params, &$error): bool
222 if ($data[$field] !== $data[$params]) {
223 $error =
'The field ' . $field .
' must be identical to the field ' . $params;
241 protected function required(
string $field, array $data,
string $params, &$error): bool
243 if (0 == strlen($data[$field])) {
244 $error =
'The field ' . $field .
' is required';
static getEnv(string $key)
Connects to the database.
match(string $field, array $data, string $params, &$error)
max_length(string $field, array $data, string $params, &$error)
valid_email(string $field, array $data, string $params, &$error)
where(string $key, mixed $value)
min_length(string $field, array $data, string $params, &$error)
update(int $id, array $data)
required(string $field, array $data, string $params, &$error)
validate(array $data, string $ruleset, array &$errors=[])
Interface that the user can implement in order to create a new Model, based on a real ODBC.