vlw.se/src/Database/Models/Model.php
Victor Westerlund 56cf142e0d refactor: major refactor, design overhaul and merge of Reflect API and Vegvisir sources into the same root (#23)
The PR is a huge refactor of all Reflect and Vegvisir code. I've merged the API and "Front-end" codebases together into the same root, this will allow for both Reflect and Vegvisir to use the same resources. Not only that, but I've also added proper database modeling with actual OOP inheritance for database tables.

Reviewed-on: https://codeberg.org/vlw/vlw.se/pulls/23
2025-02-05 04:49:23 +00:00

43 lines
No EOL
1 KiB
PHP

<?php
namespace VLW\Database\Models;
use \VV;
use VLW\API\{Client, Endpoints};
require_once VV::root("src/API/API.php");
require_once VV::root("src/API/Endpoints.php");
abstract class Model {
abstract public static function all(array $params = []): array;
private array $row;
public function __construct(
public readonly ?Endpoints $endpoint = null,
private readonly array $params = []
) {
if ($this->endpoint) {
$this->assign(self::first(self::list($endpoint, $params)));
}
}
public static function first(array $array): array {
return $array && is_array(array_values($array)[0]) ? $array[0] : $array;
}
public static function list(Endpoints $endpoint, array $params = []): array {
$resp = (new Client())->call($endpoint->value)->params($params)->get();
return $resp->ok ? $resp->json() : [];
}
public function assign(array $row): self {
$this->row = $row;
return $this;
}
public function get(string $key): mixed {
return $this->row[$key] ?? null;
}
}