]> BookStack Code Mirror - bookstack/blob - app/Entities/EntityProvider.php
Moved models to folder, renamed managers to tools
[bookstack] / app / Entities / EntityProvider.php
1 <?php namespace BookStack\Entities;
2
3 /**
4  * Class EntityProvider
5  *
6  * Provides access to the core entity models.
7  * Wrapped up in this provider since they are often used together
8  * so this is a neater alternative to injecting all in individually.
9  */
10 class EntityProvider
11 {
12
13     /**
14      * @var Bookshelf
15      */
16     public $bookshelf;
17
18     /**
19      * @var Book
20      */
21     public $book;
22
23     /**
24      * @var Chapter
25      */
26     public $chapter;
27
28     /**
29      * @var Page
30      */
31     public $page;
32
33     /**
34      * @var PageRevision
35      */
36     public $pageRevision;
37
38     /**
39      * EntityProvider constructor.
40      */
41     public function __construct(
42         Bookshelf $bookshelf,
43         Book $book,
44         Chapter $chapter,
45         Page $page,
46         PageRevision $pageRevision
47     ) {
48         $this->bookshelf = $bookshelf;
49         $this->book = $book;
50         $this->chapter = $chapter;
51         $this->page = $page;
52         $this->pageRevision = $pageRevision;
53     }
54
55     /**
56      * Fetch all core entity types as an associated array
57      * with their basic names as the keys.
58      * @return [string => Entity]
59      */
60     public function all(): array
61     {
62         return [
63             'bookshelf' => $this->bookshelf,
64             'book' => $this->book,
65             'chapter' => $this->chapter,
66             'page' => $this->page,
67         ];
68     }
69
70     /**
71      * Get an entity instance by it's basic name.
72      */
73     public function get(string $type): Entity
74     {
75         $type = strtolower($type);
76         return $this->all()[$type];
77     }
78
79     /**
80      * Get the morph classes, as an array, for a single or multiple types.
81      */
82     public function getMorphClasses(array $types): array
83     {
84         $morphClasses = [];
85         foreach ($types as $type) {
86             $model = $this->get($type);
87             $morphClasses[] = $model->getMorphClass();
88         }
89         return $morphClasses;
90     }
91 }