]> BookStack Code Mirror - bookstack/blob - app/Entities/Queries/EntityQueries.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Entities / Queries / EntityQueries.php
1 <?php
2
3 namespace BookStack\Entities\Queries;
4
5 use BookStack\Entities\Models\Entity;
6 use Illuminate\Database\Eloquent\Builder;
7 use InvalidArgumentException;
8
9 class EntityQueries
10 {
11     public function __construct(
12         public BookshelfQueries $shelves,
13         public BookQueries $books,
14         public ChapterQueries $chapters,
15         public PageQueries $pages,
16         public PageRevisionQueries $revisions,
17     ) {
18     }
19
20     /**
21      * Find an entity via an identifier string in the format:
22      * {type}:{id}
23      * Example: (book:5).
24      */
25     public function findVisibleByStringIdentifier(string $identifier): ?Entity
26     {
27         $explodedId = explode(':', $identifier);
28         $entityType = $explodedId[0];
29         $entityId = intval($explodedId[1]);
30         $queries = $this->getQueriesForType($entityType);
31
32         return $queries->findVisibleById($entityId);
33     }
34
35     /**
36      * Start a query of visible entities of the given type,
37      * suitable for listing display.
38      */
39     public function visibleForList(string $entityType): Builder
40     {
41         $queries = $this->getQueriesForType($entityType);
42         return $queries->visibleForList();
43     }
44
45     protected function getQueriesForType(string $type): ProvidesEntityQueries
46     {
47         /** @var ?ProvidesEntityQueries $queries */
48         $queries = match ($type) {
49             'page' => $this->pages,
50             'chapter' => $this->chapters,
51             'book' => $this->books,
52             'bookshelf' => $this->shelves,
53             default => null,
54         };
55
56         if (is_null($queries)) {
57             throw new InvalidArgumentException("No entity query class configured for {$type}");
58         }
59
60         return $queries;
61     }
62 }