namespace BookStack\Api;
+use BookStack\Entities\Models\BookChild;
use BookStack\Entities\Models\Entity;
+use BookStack\Entities\Models\Page;
class ApiEntityListFormatter
{
* @var array<string|int, string|callable>
*/
protected array $fields = [
- 'id', 'name', 'slug', 'book_id', 'chapter_id', 'draft',
- 'template', 'priority', 'created_at', 'updated_at',
+ 'id',
+ 'name',
+ 'slug',
+ 'book_id',
+ 'chapter_id',
+ 'draft',
+ 'template',
+ 'priority',
+ 'created_at',
+ 'updated_at',
];
public function __construct(array $list)
return $this;
}
+ /**
+ * Include parent book/chapter info in the formatted data.
+ */
+ public function withParents(): self
+ {
+ $this->withField('book', function (Entity $entity) {
+ if ($entity instanceof BookChild && $entity->book) {
+ return $entity->book->only(['id', 'name', 'slug']);
+ }
+ return null;
+ });
+
+ $this->withField('chapter', function (Entity $entity) {
+ if ($entity instanceof Page && $entity->chapter) {
+ return $entity->chapter->only(['id', 'name', 'slug']);
+ }
+ return null;
+ });
+
+ return $this;
+ }
+
/**
* Format the data and return an array of formatted content.
* @return array[]