]> BookStack Code Mirror - bookstack/blob - app/Entities/EntityProvider.php
Update passwords.php
[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      * @param Bookshelf $bookshelf
43      * @param Book $book
44      * @param Chapter $chapter
45      * @param Page $page
46      * @param PageRevision $pageRevision
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 Entity[]
66      */
67     public function all()
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      * @param string $type
80      * @return Entity
81      */
82     public function get(string $type)
83     {
84         $type = strtolower($type);
85         return $this->all()[$type];
86     }
87
88     /**
89      * Get the morph classes, as an array, for a single or multiple types.
90      * @param string|array $types
91      * @return array<string>
92      */
93     public function getMorphClasses($types)
94     {
95         if (is_string($types)) {
96             $types = [$types];
97         }
98
99         $morphClasses = [];
100         foreach ($types as $type) {
101             $model = $this->get($type);
102             $morphClasses[] = $model->getMorphClass();
103         }
104         return $morphClasses;
105     }
106 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.