]> BookStack Code Mirror - bookstack/blob - app/Entities/Queries/EntityQueries.php
Queries: Extracted PageRepo queries to own class
[bookstack] / app / Entities / Queries / EntityQueries.php
1 <?php
2
3 namespace BookStack\Entities\Queries;
4
5 use BookStack\Entities\Models\Entity;
6
7 class EntityQueries
8 {
9     public function __construct(
10         public BookshelfQueries $shelves,
11         public BookQueries $books,
12         public ChapterQueries $chapters,
13         public PageQueries $pages,
14         public PageRevisionQueries $revisions,
15     ) {
16     }
17
18     /**
19      * Find an entity via an identifier string in the format:
20      * {type}:{id}
21      * Example: (book:5).
22      */
23     public function findVisibleByStringIdentifier(string $identifier): ?Entity
24     {
25         $explodedId = explode(':', $identifier);
26         $entityType = $explodedId[0];
27         $entityId = intval($explodedId[1]);
28
29         /** @var ?ProvidesEntityQueries $queries */
30         $queries = match ($entityType) {
31             'page' => $this->pages,
32             'chapter' => $this->chapters,
33             'book' => $this->books,
34             'bookshelf' => $this->shelves,
35             default => null,
36         };
37
38         if (is_null($queries)) {
39             return null;
40         }
41
42         return $queries->findVisibleById($entityId);
43     }
44 }