2022-08-16 12:23:53 +00:00
|
|
|
<?php
|
|
|
|
|
2022-08-17 13:39:53 +00:00
|
|
|
namespace BookStack\References\ModelResolvers;
|
2022-08-16 12:23:53 +00:00
|
|
|
|
2023-05-17 16:56:55 +00:00
|
|
|
use BookStack\App\Model;
|
2022-08-16 12:23:53 +00:00
|
|
|
use BookStack\Entities\Models\Bookshelf;
|
|
|
|
|
|
|
|
class BookshelfLinkModelResolver implements CrossLinkModelResolver
|
|
|
|
{
|
|
|
|
public function resolve(string $link): ?Model
|
|
|
|
{
|
2022-08-17 13:39:53 +00:00
|
|
|
$pattern = '/^' . preg_quote(url('/shelves'), '/') . '\/([\w-]+)' . '([#?\/]|$)/';
|
2022-08-16 12:23:53 +00:00
|
|
|
$matches = [];
|
|
|
|
$match = preg_match($pattern, $link, $matches);
|
|
|
|
if (!$match) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$shelfSlug = $matches[1];
|
|
|
|
|
|
|
|
/** @var ?Bookshelf $model */
|
2022-08-29 16:46:41 +00:00
|
|
|
$model = Bookshelf::query()->where('slug', '=', $shelfSlug)->first(['id']);
|
2022-08-16 12:23:53 +00:00
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
2022-08-29 16:46:41 +00:00
|
|
|
}
|