]> BookStack Code Mirror - bookstack/blob - app/References/ModelResolvers/ChapterLinkModelResolver.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / References / ModelResolvers / ChapterLinkModelResolver.php
1 <?php
2
3 namespace BookStack\References\ModelResolvers;
4
5 use BookStack\App\Model;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Queries\ChapterQueries;
8
9 class ChapterLinkModelResolver implements CrossLinkModelResolver
10 {
11     public function __construct(
12         protected ChapterQueries $queries
13     ) {
14     }
15
16     public function resolve(string $link): ?Model
17     {
18         $pattern = '/^' . preg_quote(url('/books'), '/') . '\/([\w-]+)' . '\/chapter\/' . '([\w-]+)' . '([#?\/]|$)/';
19         $matches = [];
20         $match = preg_match($pattern, $link, $matches);
21         if (!$match) {
22             return null;
23         }
24
25         $bookSlug = $matches[1];
26         $chapterSlug = $matches[2];
27
28         /** @var ?Chapter $model */
29         $model = $this->queries->usingSlugs($bookSlug, $chapterSlug)->first(['id']);
30
31         return $model;
32     }
33 }