3 namespace BookStack\Entities;
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;
13 * Class EntityProvider.
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.
21 public Bookshelf $bookshelf;
23 public Chapter $chapter;
25 public PageRevision $pageRevision;
27 public function __construct()
29 $this->bookshelf = new Bookshelf();
30 $this->book = new Book();
31 $this->chapter = new Chapter();
32 $this->page = new Page();
33 $this->pageRevision = new PageRevision();
37 * Fetch all core entity types as an associated array
38 * with their basic names as the keys.
40 * @return array<string, Entity>
42 public function all(): array
45 'bookshelf' => $this->bookshelf,
46 'book' => $this->book,
47 'chapter' => $this->chapter,
48 'page' => $this->page,
53 * Get an entity instance by its basic name.
55 public function get(string $type): Entity
57 $type = strtolower($type);
58 $instance = $this->all()[$type] ?? null;
60 if (is_null($instance)) {
61 throw new \InvalidArgumentException("Provided type \"{$type}\" is not a valid entity type");
68 * Get the morph classes, as an array, for a single or multiple types.
70 public function getMorphClasses(array $types): array
73 foreach ($types as $type) {
74 $model = $this->get($type);
75 $morphClasses[] = $model->getMorphClass();