]> BookStack Code Mirror - bookstack/blob - app/Entities/EntityProvider.php
do some cleanup and add doc
[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     /**
22      * @var Bookshelf
23      */
24     public $bookshelf;
25
26     /**
27      * @var Book
28      */
29     public $book;
30
31     /**
32      * @var Chapter
33      */
34     public $chapter;
35
36     /**
37      * @var Page
38      */
39     public $page;
40
41     /**
42      * @var PageRevision
43      */
44     public $pageRevision;
45
46     public function __construct()
47     {
48         $this->bookshelf = new Bookshelf();
49         $this->book = new Book();
50         $this->chapter = new Chapter();
51         $this->page = new Page();
52         $this->pageRevision = new PageRevision();
53     }
54
55     /**
56      * Fetch all core entity types as an associated array
57      * with their basic names as the keys.
58      *
59      * @return array<Entity>
60      */
61     public function all(): array
62     {
63         return [
64             'bookshelf' => $this->bookshelf,
65             'book'      => $this->book,
66             'chapter'   => $this->chapter,
67             'page'      => $this->page,
68         ];
69     }
70
71     /**
72      * Get an entity instance by it's basic name.
73      */
74     public function get(string $type): Entity
75     {
76         $type = strtolower($type);
77
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
92         return $morphClasses;
93     }
94 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.