X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/3886aedf54873a1bf90cfdaa9cafc6f22c1d03fd..refs/pull/5681/head:/app/Entities/Queries/EntityQueries.php diff --git a/app/Entities/Queries/EntityQueries.php b/app/Entities/Queries/EntityQueries.php index b1973b0ea..36dc6c0bc 100644 --- a/app/Entities/Queries/EntityQueries.php +++ b/app/Entities/Queries/EntityQueries.php @@ -2,12 +2,61 @@ namespace BookStack\Entities\Queries; +use BookStack\Entities\Models\Entity; +use Illuminate\Database\Eloquent\Builder; +use InvalidArgumentException; + 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]); + $queries = $this->getQueriesForType($entityType); + + 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 + { + /** @var ?ProvidesEntityQueries $queries */ + $queries = match ($type) { + 'page' => $this->pages, + 'chapter' => $this->chapters, + 'book' => $this->books, + 'bookshelf' => $this->shelves, + default => null, + }; + + if (is_null($queries)) { + throw new InvalidArgumentException("No entity query class configured for {$type}"); + } + + return $queries; + } }