initial
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
use Reservair\Database\Db;
|
||||
use Reservair\Logger\Logger;
|
||||
|
||||
class RsvFormDefinitionRepository {
|
||||
private string $table;
|
||||
|
||||
public function __construct() {
|
||||
$this->table = Db::prefix() . 'rsv_form_definition';
|
||||
}
|
||||
|
||||
public function add(string $name, array $definition): int {
|
||||
return Db::insert($this->table, [
|
||||
'name' => $name,
|
||||
'definition' => json_encode($definition),
|
||||
]);
|
||||
}
|
||||
|
||||
public function get_all(?int $limit = null, int $skip = 0): array {
|
||||
if ($limit === null) {
|
||||
return Db::get_results(
|
||||
"SELECT form_id, name FROM {$this->table} ORDER BY form_id ASC",
|
||||
[],
|
||||
ARRAY_A
|
||||
);
|
||||
}
|
||||
return Db::get_results(
|
||||
"SELECT form_id, name FROM {$this->table} ORDER BY form_id ASC LIMIT %d OFFSET %d",
|
||||
[$limit, $skip],
|
||||
ARRAY_A
|
||||
);
|
||||
}
|
||||
|
||||
public function count_all(): int {
|
||||
return (int) Db::get_var("SELECT COUNT(*) FROM {$this->table}");
|
||||
}
|
||||
|
||||
public function update(int $id, string $name, array $definition): void {
|
||||
Db::update(
|
||||
$this->table,
|
||||
['name' => $name, 'definition' => json_encode($definition)],
|
||||
['form_id' => $id]
|
||||
);
|
||||
}
|
||||
|
||||
public function delete(int $id): void {
|
||||
Db::delete($this->table, ['form_id' => $id]);
|
||||
}
|
||||
|
||||
public function get(int $id): ?array {
|
||||
$row = Db::get_row(
|
||||
"SELECT * FROM {$this->table} WHERE form_id = %d",
|
||||
[$id],
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
if ($row === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$row['definition'] = json_decode($row['definition'], true);
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user