2018-09-25 13:30:50 +02:00
|
|
|
<?php namespace BookStack\Entities;
|
|
|
|
|
2020-11-22 01:17:45 +01:00
|
|
|
use BookStack\Entities\Models\Book;
|
|
|
|
use BookStack\Entities\Models\Bookshelf;
|
|
|
|
use BookStack\Entities\Models\Chapter;
|
|
|
|
use BookStack\Entities\Models\Entity;
|
|
|
|
use BookStack\Entities\Models\Page;
|
|
|
|
use BookStack\Entities\Models\PageRevision;
|
|
|
|
|
2018-09-25 17:58:03 +02: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.
|
|
|
|
*/
|
2018-09-25 13:30:50 +02:00
|
|
|
class EntityProvider
|
|
|
|
{
|
2018-09-25 17:58:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Bookshelf
|
|
|
|
*/
|
2018-09-25 19:00:40 +02:00
|
|
|
public $bookshelf;
|
2018-09-25 17:58:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Book
|
|
|
|
*/
|
2018-09-25 19:00:40 +02:00
|
|
|
public $book;
|
2018-09-25 17:58:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Chapter
|
|
|
|
*/
|
2018-09-25 19:00:40 +02:00
|
|
|
public $chapter;
|
2018-09-25 17:58:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Page
|
|
|
|
*/
|
2018-09-25 19:00:40 +02:00
|
|
|
public $page;
|
2018-09-25 17:58:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PageRevision
|
|
|
|
*/
|
2018-09-25 19:00:40 +02:00
|
|
|
public $pageRevision;
|
2018-09-25 17:58:03 +02:00
|
|
|
|
2020-11-22 02:20:38 +01:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->bookshelf = new Bookshelf();
|
|
|
|
$this->book = new Book();
|
|
|
|
$this->chapter = new Chapter();
|
|
|
|
$this->page = new Page();
|
|
|
|
$this->pageRevision = new PageRevision();
|
2018-09-25 17:58:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch all core entity types as an associated array
|
|
|
|
* with their basic names as the keys.
|
2021-01-01 19:31:01 +01:00
|
|
|
* @return array<Entity>
|
2018-09-25 17:58:03 +02:00
|
|
|
*/
|
2019-10-05 13:55:01 +02:00
|
|
|
public function all(): array
|
2018-09-25 17:58:03 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'bookshelf' => $this->bookshelf,
|
|
|
|
'book' => $this->book,
|
|
|
|
'chapter' => $this->chapter,
|
|
|
|
'page' => $this->page,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:00:40 +02:00
|
|
|
/**
|
|
|
|
* Get an entity instance by it's basic name.
|
|
|
|
*/
|
2019-10-05 13:55:01 +02:00
|
|
|
public function get(string $type): Entity
|
2018-09-25 19:00:40 +02:00
|
|
|
{
|
|
|
|
$type = strtolower($type);
|
|
|
|
return $this->all()[$type];
|
|
|
|
}
|
2019-03-30 17:54:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the morph classes, as an array, for a single or multiple types.
|
|
|
|
*/
|
2019-10-05 13:55:01 +02:00
|
|
|
public function getMorphClasses(array $types): array
|
2019-03-30 17:54:15 +01:00
|
|
|
{
|
|
|
|
$morphClasses = [];
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$model = $this->get($type);
|
|
|
|
$morphClasses[] = $model->getMorphClass();
|
|
|
|
}
|
|
|
|
return $morphClasses;
|
|
|
|
}
|
2019-01-27 11:29:23 +01:00
|
|
|
}
|