web-base/Core/API/Search.class.php

46 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2022-08-20 22:17:17 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\API;
2022-08-20 22:17:17 +02:00
2022-11-18 18:06:46 +01:00
use Core\API\Parameter\StringType;
use Core\Objects\Context;
use Core\Objects\Search\Searchable;
use Core\Objects\Search\SearchQuery;
2022-08-20 22:17:17 +02:00
2024-04-23 12:14:28 +02:00
// TODO: rework this entirely
2022-08-20 22:17:17 +02:00
class Search extends Request {
2024-03-24 17:36:16 +01:00
public function __construct(Context $context, bool $externalCall = false) {
2022-08-20 22:17:17 +02:00
parent::__construct($context, $externalCall, [
"text" => new StringType("text", 32)
]);
}
protected function _execute(): bool {
$query = new SearchQuery(trim($this->getParam("text")));
if (strlen($query->getQuery()) < 3) {
return $this->createError("You have to type at least 3 characters to search for");
}
$router = $this->context->router;
if ($router === null) {
return $this->createError("There is currently no router configured. Error during installation?");
}
$this->result["results"] = [];
foreach ($router->getRoutes(false) as $route) {
if(in_array(Searchable::class, array_keys((new \ReflectionClass($route))->getTraits()))) {
foreach ($route->doSearch($this->context, $query) as $searchResult) {
$this->result["results"][] = $searchResult->jsonSerialize();
}
}
}
return true;
}
2024-04-23 12:14:28 +02:00
public static function getDescription(): string {
return "Searches the site documents and returns a list of matching routes";
}
2022-08-20 22:17:17 +02:00
}