]> BookStack Code Mirror - bookstack/blob - app/Entities/Queries/EntityQueries.php
39a21c9138cb684fa644ed93a41cdd6faa6b34c3
[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     ) {
15     }
16
17     /**
18      * Find an entity via an identifier string in the format:
19      * {type}:{id}
20      * Example: (book:5).
21      */
22     public function findVisibleByStringIdentifier(string $identifier): ?Entity
23     {
24         $explodedId = explode(':', $identifier);
25         $entityType = $explodedId[0];
26         $entityId = intval($explodedId[1]);
27
28         /** @var ?ProvidesEntityQueries $queries */
29         $queries = match ($entityType) {
30             'page' => $this->pages,
31             'chapter' => $this->chapters,
32             'book' => $this->books,
33             'bookshelf' => $this->shelves,
34             default => null,
35         };
36
37         if (is_null($queries)) {
38             return null;
39         }
40
41         return $queries->findVisibleById($entityId);
42     }
43 }