vlw.se/api/endpoints/battlestation/GET.php
Victor Westerlund 99e9996e93
feat: add "battlestation" (#40)
* wip: 2024-06-19T13:31:53+0200 (1718796713)

* fix: final touchups with bugfixes

* fix: typo in widlcardsearch function

* wip: 2024-07-05T14:14:12+0200 (1720181652)

* wip: 2024-07-07T11:22:34+0200 (1720344154)

* wip: 2024-07-07T11:22:34+0200 (1720344154)
2024-07-07 10:37:17 +00:00

63 lines
No EOL
1.6 KiB
PHP

<?php
use Reflect\Path;
use Reflect\Response;
use ReflectRules\Type;
use ReflectRules\Rules;
use ReflectRules\Ruleset;
use VLW\API\Databases\VLWdb\{
VLWdb,
Databases
};
use VLW\API\Databases\VLWdb\Models\Battlestation\Config\ConfigModel;
require_once Path::root("src/databases/VLWdb.php");
require_once Path::root("src/databases/models/Battlestation/Config/Config.php");
class GET_Battlestation extends VLWdb {
protected Ruleset $ruleset;
private array $query;
private array $results = [];
public function __construct() {
$this->ruleset = new Ruleset(strict: true);
$this->ruleset->GET([
(new Rules(ConfigModel::REF_MB_ID->value))
->type(Type::STRING)
->min(parent::UUID_LENGTH)
->max(parent::UUID_LENGTH),
(new Rules(ConfigModel::FRIENDLY_NAME->value))
->type(Type::STRING)
->min(1)
->max(parent::MYSQL_VARCHAR_MAX_LENGTH)
]);
parent::__construct(Databases::BATTLESTATION, $this->ruleset);
// Use a copy of search parameters
$this->query = $_GET;
}
private function get_config(): array {
return $this->results = $this->db
->for(ConfigModel::TABLE)
->where($this->query)
->order([ConfigModel::DATE_BUILT->value => "DESC"])
->select(ConfigModel::values())
->fetch_all(MYSQLI_ASSOC);
}
public function main(): Response {
// Set properties as "searchable"
parent::make_wildcard_search(ConfigModel::FRIENDLY_NAME->value, $this->query);
$this->get_config();
// Return 404 Not Found if response array is empty
return new Response($this->results, $this->results ? 200 : 404);
}
}