| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  | <?php namespace BookStack\Entities; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 23:58:03 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Class EntityProvider | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Provides access to the core entity models. | 
					
						
							|  |  |  |  * Wrapped up in this provider since they are often used together | 
					
						
							|  |  |  |  * so this is a neater alternative to injecting all in individually. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package BookStack\Entities | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  | class EntityProvider | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-25 23:58:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var Bookshelf | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-09-26 01:00:40 +08:00
										 |  |  |     public $bookshelf; | 
					
						
							| 
									
										
										
										
											2018-09-25 23:58:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var Book | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-09-26 01:00:40 +08:00
										 |  |  |     public $book; | 
					
						
							| 
									
										
										
										
											2018-09-25 23:58:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var Chapter | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-09-26 01:00:40 +08:00
										 |  |  |     public $chapter; | 
					
						
							| 
									
										
										
										
											2018-09-25 23:58:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var Page | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-09-26 01:00:40 +08:00
										 |  |  |     public $page; | 
					
						
							| 
									
										
										
										
											2018-09-25 23:58:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var PageRevision | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-09-26 01:00:40 +08:00
										 |  |  |     public $pageRevision; | 
					
						
							| 
									
										
										
										
											2018-09-25 23:58:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * EntityProvider constructor. | 
					
						
							|  |  |  |      * @param Bookshelf $bookshelf | 
					
						
							|  |  |  |      * @param Book $book | 
					
						
							|  |  |  |      * @param Chapter $chapter | 
					
						
							|  |  |  |      * @param Page $page | 
					
						
							|  |  |  |      * @param PageRevision $pageRevision | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct( | 
					
						
							|  |  |  |         Bookshelf $bookshelf, | 
					
						
							|  |  |  |         Book $book, | 
					
						
							|  |  |  |         Chapter $chapter, | 
					
						
							|  |  |  |         Page $page, | 
					
						
							|  |  |  |         PageRevision $pageRevision | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |         $this->bookshelf = $bookshelf; | 
					
						
							|  |  |  |         $this->book = $book; | 
					
						
							|  |  |  |         $this->chapter = $chapter; | 
					
						
							|  |  |  |         $this->page = $page; | 
					
						
							|  |  |  |         $this->pageRevision = $pageRevision; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Fetch all core entity types as an associated array | 
					
						
							|  |  |  |      * with their basic names as the keys. | 
					
						
							|  |  |  |      * @return Entity[] | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function all() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             'bookshelf' => $this->bookshelf, | 
					
						
							|  |  |  |             'book' => $this->book, | 
					
						
							|  |  |  |             'chapter' => $this->chapter, | 
					
						
							|  |  |  |             'page' => $this->page, | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-26 01:00:40 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get an entity instance by it's basic name. | 
					
						
							|  |  |  |      * @param string $type | 
					
						
							|  |  |  |      * @return Entity | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function get(string $type) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $type = strtolower($type); | 
					
						
							|  |  |  |         return $this->all()[$type]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-31 00:54:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the morph classes, as an array, for a single or multiple types. | 
					
						
							|  |  |  |      * @param string|array $types | 
					
						
							|  |  |  |      * @return array<string> | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getMorphClasses($types) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_string($types)) { | 
					
						
							|  |  |  |             $types = [$types]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $morphClasses = []; | 
					
						
							|  |  |  |         foreach ($types as $type) { | 
					
						
							|  |  |  |             $model = $this->get($type); | 
					
						
							|  |  |  |             $morphClasses[] = $model->getMorphClass(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $morphClasses; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-27 18:29:23 +08:00
										 |  |  | } |