2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities;
|
2018-09-25 07:30:50 -04:00
|
|
|
|
2020-11-21 19:17:45 -05: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 11:58:03 -04:00
|
|
|
/**
|
2021-06-26 11:23:15 -04:00
|
|
|
* Class EntityProvider.
|
2018-09-25 11:58:03 -04:00
|
|
|
*
|
|
|
|
* 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 07:30:50 -04:00
|
|
|
class EntityProvider
|
|
|
|
{
|
2023-03-13 09:18:33 -04:00
|
|
|
public Bookshelf $bookshelf;
|
|
|
|
public Book $book;
|
|
|
|
public Chapter $chapter;
|
|
|
|
public Page $page;
|
|
|
|
public PageRevision $pageRevision;
|
2018-09-25 11:58:03 -04:00
|
|
|
|
2020-11-21 20:20:38 -05: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 11:58:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch all core entity types as an associated array
|
|
|
|
* with their basic names as the keys.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2021-01-01 13:31:01 -05:00
|
|
|
* @return array<Entity>
|
2018-09-25 11:58:03 -04:00
|
|
|
*/
|
2019-10-05 07:55:01 -04:00
|
|
|
public function all(): array
|
2018-09-25 11:58:03 -04:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'bookshelf' => $this->bookshelf,
|
2021-06-26 11:23:15 -04:00
|
|
|
'book' => $this->book,
|
|
|
|
'chapter' => $this->chapter,
|
|
|
|
'page' => $this->page,
|
2018-09-25 11:58:03 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-09-25 13:00:40 -04:00
|
|
|
/**
|
2023-03-13 09:18:33 -04:00
|
|
|
* Get an entity instance by its basic name.
|
2018-09-25 13:00:40 -04:00
|
|
|
*/
|
2019-10-05 07:55:01 -04:00
|
|
|
public function get(string $type): Entity
|
2018-09-25 13:00:40 -04:00
|
|
|
{
|
|
|
|
$type = strtolower($type);
|
2023-03-13 09:18:33 -04:00
|
|
|
$instance = $this->all()[$type] ?? null;
|
|
|
|
|
|
|
|
if (is_null($instance)) {
|
|
|
|
throw new \InvalidArgumentException("Provided type \"{$type}\" is not a valid entity type");
|
|
|
|
}
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2023-03-13 09:18:33 -04:00
|
|
|
return $instance;
|
2018-09-25 13:00:40 -04:00
|
|
|
}
|
2019-03-30 12:54:15 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the morph classes, as an array, for a single or multiple types.
|
|
|
|
*/
|
2019-10-05 07:55:01 -04:00
|
|
|
public function getMorphClasses(array $types): array
|
2019-03-30 12:54:15 -04:00
|
|
|
{
|
|
|
|
$morphClasses = [];
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$model = $this->get($type);
|
|
|
|
$morphClasses[] = $model->getMorphClass();
|
|
|
|
}
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-03-30 12:54:15 -04:00
|
|
|
return $morphClasses;
|
|
|
|
}
|
2019-01-27 05:29:23 -05:00
|
|
|
}
|