]> BookStack Code Mirror - bookstack/blob - app/Entities/EntityProvider.php
Updated translator & dependency attribution before release v25.05.1
[bookstack] / app / Entities / EntityProvider.php
1 <?php
2
3 namespace BookStack\Entities;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Entities\Models\PageRevision;
11
12 /**
13  * Class EntityProvider.
14  *
15  * Provides access to the core entity models.
16  * Wrapped up in this provider since they are often used together
17  * so this is a neater alternative to injecting all in individually.
18  */
19 class EntityProvider
20 {
21     public Bookshelf $bookshelf;
22     public Book $book;
23     public Chapter $chapter;
24     public Page $page;
25     public PageRevision $pageRevision;
26
27     public function __construct()
28     {
29         $this->bookshelf = new Bookshelf();
30         $this->book = new Book();
31         $this->chapter = new Chapter();
32         $this->page = new Page();
33         $this->pageRevision = new PageRevision();
34     }
35
36     /**
37      * Fetch all core entity types as an associated array
38      * with their basic names as the keys.
39      *
40      * @return array<string, Entity>
41      */
42     public function all(): array
43     {
44         return [
45             'bookshelf' => $this->bookshelf,
46             'book'      => $this->book,
47             'chapter'   => $this->chapter,
48             'page'      => $this->page,
49         ];
50     }
51
52     /**
53      * Get an entity instance by its basic name.
54      */
55     public function get(string $type): Entity
56     {
57         $type = strtolower($type);
58         $instance = $this->all()[$type] ?? null;
59
60         if (is_null($instance)) {
61             throw new \InvalidArgumentException("Provided type \"{$type}\" is not a valid entity type");
62         }
63
64         return $instance;
65     }
66
67     /**
68      * Get the morph classes, as an array, for a single or multiple types.
69      */
70     public function getMorphClasses(array $types): array
71     {
72         $morphClasses = [];
73         foreach ($types as $type) {
74             $model = $this->get($type);
75             $morphClasses[] = $model->getMorphClass();
76         }
77
78         return $morphClasses;
79     }
80 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.