BookStack/app/Entities/Queries/EntityQueries.php
Dan Brown 222c665018
Queries: Extracted PageRepo queries to own class
Started new class for PageRevisions too as part of these changes
2024-02-05 17:35:49 +00:00

45 lines
1.1 KiB
PHP

<?php
namespace BookStack\Entities\Queries;
use BookStack\Entities\Models\Entity;
class EntityQueries
{
public function __construct(
public BookshelfQueries $shelves,
public BookQueries $books,
public ChapterQueries $chapters,
public PageQueries $pages,
public PageRevisionQueries $revisions,
) {
}
/**
* Find an entity via an identifier string in the format:
* {type}:{id}
* Example: (book:5).
*/
public function findVisibleByStringIdentifier(string $identifier): ?Entity
{
$explodedId = explode(':', $identifier);
$entityType = $explodedId[0];
$entityId = intval($explodedId[1]);
/** @var ?ProvidesEntityQueries $queries */
$queries = match ($entityType) {
'page' => $this->pages,
'chapter' => $this->chapters,
'book' => $this->books,
'bookshelf' => $this->shelves,
default => null,
};
if (is_null($queries)) {
return null;
}
return $queries->findVisibleById($entityId);
}
}