vlw.se/src/entities/Entity.php
Victor Westerlund 608f775f24
refactor: streamlined all API endpoints and remove local API packages (#36)
* wip: 2024-06-13T07:32:13+0200 (1718256733)

* wip: 2024-06-16T13:05:34+0200 (1718535934)

* wip: 2024-06-16T15:07:31+0200 (1718543251)
2024-06-16 14:02:34 +00:00

57 lines
No EOL
1.3 KiB
PHP

<?php
namespace VLW\Entities;
use Vegvisir\Path;
use Reflect\Response;
use VLW\Client\API;
use VLW\API\Endpoints;
require_once Path::root("src/client/API.php");
require_once Path::root("api/src/Endpoints.php");
interface EntityInterface {}
abstract class Entity implements EntityInterface {
public const ENTITY_ID = "id";
public readonly ?string $id;
public readonly object $entity;
public readonly Response $response;
protected readonly Api $api;
protected readonly Endpoints $endpoint;
public function __construct(Endpoints $endpoint, ?string $id) {
$this->id = $id;
$this->api = new Api();
$this->endpoint = $endpoint;
$this->resolve_entity_by_id();
}
private function resolve_entity_by_id() {
// Bail out wit a dummy Response if no id was provided
if (!$this->id) {
$this->response = new Response("", 404);
return;
}
$this->response = $this->api
->call($this->endpoint->value)
->params([self::ENTITY_ID => $this->id])
->get();
// Load response into entity object if successful
if ($this->response->ok) {
$this->entity = (object) $this->response->json()[0];
}
}
public function resolve(Endpoints $endpoint, array $params): array {
$response = $this->api->call($endpoint->value)->params($params)->get();
return $response->ok ? $response->json() : [];
}
}