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