mirror of
https://codeberg.org/vlw/vlw.se.git
synced 2025-09-13 21:13:40 +02:00
71 lines
No EOL
2.3 KiB
PHP
71 lines
No EOL
2.3 KiB
PHP
<?php
|
|
|
|
namespace VLW\Database\Models\Search;
|
|
|
|
use \VV;
|
|
use \vlw\MySQL\Operators;
|
|
|
|
use VLW\Helpers\UUID;
|
|
use VLW\Database\Database;
|
|
use VLW\Database\Models\Model;
|
|
use VLW\Database\Tables\Search\{Search as SearchTable, SearchTypeEnum};
|
|
|
|
require_once VV::root("src/Helpers/UUID.php");
|
|
require_once VV::root("src/Database/Database.php");
|
|
require_once VV::root("src/Database/Models/Model.php");
|
|
require_once VV::root("src/Database/Tables/Search/Search.php");
|
|
|
|
class Search extends Model {
|
|
final public static function new(string $query, SearchTypeEnum $type, string $title): self {
|
|
$id = UUID::v4();
|
|
|
|
if (!parent::create(SearchTable::TABLE, [
|
|
SearchTable::ID->value => $id,
|
|
SearchTable::QUERY->value => $query,
|
|
SearchTable::TYPE->value => $type->name,
|
|
SearchTable::TITLE->value => $title,
|
|
SearchTable::TEXT->value => null,
|
|
SearchTable::HREF->value => null
|
|
])) { throw new Exception("Failed to create Search entity"); }
|
|
|
|
return new Search($id);
|
|
}
|
|
|
|
final public static function query(string $query, ?int $limit = null): array {
|
|
return array_map(fn(array $search): Search => new Search($search[SearchTable::ID->value]), new Database()
|
|
->from(SearchTable::TABLE)
|
|
->where([SearchTable::QUERY->value => [
|
|
Operators::LIKE->value => "%{$query}%"
|
|
]])
|
|
->limit($limit)
|
|
->select(SearchTable::ID->value)
|
|
->fetch_all(MYSQLI_ASSOC)
|
|
);
|
|
}
|
|
|
|
public function __construct(public readonly string $id) {
|
|
parent::__construct(SearchTable::TABLE, SearchTable::values(), [
|
|
SearchTable::ID->value => $this->id
|
|
]);
|
|
}
|
|
|
|
final public string $title {
|
|
get => $this->get(SearchTable::TITLE->value);
|
|
set (string $title) => $this->set(SearchTable::TITLE->value, $title);
|
|
}
|
|
|
|
final public ?string $text {
|
|
get => $this->get(SearchTable::TEXT->value);
|
|
set (?string $text) => $this->set(SearchTable::TEXT->value, $text);
|
|
}
|
|
|
|
final public SearchTypeEnum $type {
|
|
get => SearchTypeEnum::fromName($this->get(SearchTable::TYPE->value));
|
|
set (SearchTypeEnum $type) => $this->set(SearchTable::TYPE->value, $type->name);
|
|
}
|
|
|
|
final public string $href {
|
|
get => $this->get(SearchTable::HREF->value);
|
|
set (string $href) => $this->set(SearchTable::HREF->value, $href);
|
|
}
|
|
} |