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
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
use BookStack\App\Model;
|
2022-08-16 08:23:53 -04:00
|
|
|
use BookStack\Entities\Models\Bookshelf;
|
2024-02-07 11:37:36 -05:00
|
|
|
use BookStack\Entities\Queries\BookshelfQueries;
|
2022-08-16 08:23:53 -04:00
|
|
|
|
|
|
|
class BookshelfLinkModelResolver implements CrossLinkModelResolver
|
|
|
|
{
|
2024-02-07 11:37:36 -05:00
|
|
|
public function __construct(
|
|
|
|
protected BookshelfQueries $queries
|
|
|
|
) {
|
|
|
|
}
|
2022-08-16 08:23:53 -04:00
|
|
|
public function resolve(string $link): ?Model
|
|
|
|
{
|
2022-08-17 09:39:53 -04:00
|
|
|
$pattern = '/^' . preg_quote(url('/shelves'), '/') . '\/([\w-]+)' . '([#?\/]|$)/';
|
2022-08-16 08:23:53 -04:00
|
|
|
$matches = [];
|
|
|
|
$match = preg_match($pattern, $link, $matches);
|
|
|
|
if (!$match) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$shelfSlug = $matches[1];
|
|
|
|
|
|
|
|
/** @var ?Bookshelf $model */
|
2024-02-07 11:37:36 -05:00
|
|
|
$model = $this->queries->start()->where('slug', '=', $shelfSlug)->first(['id']);
|
2022-08-16 08:23:53 -04:00
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
2022-08-29 12:46:41 -04:00
|
|
|
}
|