]> BookStack Code Mirror - bookstack/blob - app/Entities/EntityProvider.php
Checked over recycle bin parent/child flows
[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  * @package BookStack\Entities
11  */
12 class EntityProvider
13 {
14
15     /**
16      * @var Bookshelf
17      */
18     public $bookshelf;
19
20     /**
21      * @var Book
22      */
23     public $book;
24
25     /**
26      * @var Chapter
27      */
28     public $chapter;
29
30     /**
31      * @var Page
32      */
33     public $page;
34
35     /**
36      * @var PageRevision
37      */
38     public $pageRevision;
39
40     /**
41      * EntityProvider constructor.
42      */
43     public function __construct(
44         Bookshelf $bookshelf,
45         Book $book,
46         Chapter $chapter,
47         Page $page,
48         PageRevision $pageRevision
49     ) {
50         $this->bookshelf = $bookshelf;
51         $this->book = $book;
52         $this->chapter = $chapter;
53         $this->page = $page;
54         $this->pageRevision = $pageRevision;
55     }
56
57     /**
58      * Fetch all core entity types as an associated array
59      * with their basic names as the keys.
60      * @return [string => Entity]
61      */
62     public function all(): array
63     {
64         return [
65             'bookshelf' => $this->bookshelf,
66             'book' => $this->book,
67             'chapter' => $this->chapter,
68             'page' => $this->page,
69         ];
70     }
71
72     /**
73      * Get an entity instance by it's basic name.
74      */
75     public function get(string $type): Entity
76     {
77         $type = strtolower($type);
78         return $this->all()[$type];
79     }
80
81     /**
82      * Get the morph classes, as an array, for a single or multiple types.
83      */
84     public function getMorphClasses(array $types): array
85     {
86         $morphClasses = [];
87         foreach ($types as $type) {
88             $model = $this->get($type);
89             $morphClasses[] = $model->getMorphClass();
90         }
91         return $morphClasses;
92     }
93 }