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