2024-02-05 01:35:16 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Queries;
|
|
|
|
|
2024-02-05 23:59:20 +08:00
|
|
|
use BookStack\Entities\Models\Entity;
|
2024-02-08 06:41:45 +08:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use InvalidArgumentException;
|
2024-02-05 23:59:20 +08:00
|
|
|
|
2024-02-05 01:35:16 +08:00
|
|
|
class EntityQueries
|
|
|
|
{
|
|
|
|
public function __construct(
|
2024-02-05 03:32:19 +08:00
|
|
|
public BookshelfQueries $shelves,
|
2024-02-05 01:35:16 +08:00
|
|
|
public BookQueries $books,
|
2024-02-05 23:59:20 +08:00
|
|
|
public ChapterQueries $chapters,
|
2024-02-05 01:35:16 +08:00
|
|
|
public PageQueries $pages,
|
2024-02-06 01:35:49 +08:00
|
|
|
public PageRevisionQueries $revisions,
|
2024-02-05 01:35:16 +08:00
|
|
|
) {
|
|
|
|
}
|
2024-02-05 23:59:20 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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]);
|
2024-02-08 06:41:45 +08:00
|
|
|
$queries = $this->getQueriesForType($entityType);
|
2024-02-05 23:59:20 +08:00
|
|
|
|
2024-02-08 06:41:45 +08:00
|
|
|
return $queries->findVisibleById($entityId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a query of visible entities of the given type,
|
|
|
|
* suitable for listing display.
|
|
|
|
*/
|
|
|
|
public function visibleForList(string $entityType): Builder
|
|
|
|
{
|
|
|
|
$queries = $this->getQueriesForType($entityType);
|
|
|
|
return $queries->visibleForList();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getQueriesForType(string $type): ProvidesEntityQueries
|
|
|
|
{
|
2024-02-05 23:59:20 +08:00
|
|
|
/** @var ?ProvidesEntityQueries $queries */
|
2024-02-08 06:41:45 +08:00
|
|
|
$queries = match ($type) {
|
2024-02-05 23:59:20 +08:00
|
|
|
'page' => $this->pages,
|
|
|
|
'chapter' => $this->chapters,
|
|
|
|
'book' => $this->books,
|
|
|
|
'bookshelf' => $this->shelves,
|
|
|
|
default => null,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (is_null($queries)) {
|
2024-02-08 06:41:45 +08:00
|
|
|
throw new InvalidArgumentException("No entity query class configured for {$type}");
|
2024-02-05 23:59:20 +08:00
|
|
|
}
|
|
|
|
|
2024-02-08 06:41:45 +08:00
|
|
|
return $queries;
|
2024-02-05 23:59:20 +08:00
|
|
|
}
|
2024-02-05 01:35:16 +08:00
|
|
|
}
|