| 
									
										
										
										
											2022-08-16 20:23:53 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  | namespace BookStack\References\ModelResolvers; | 
					
						
							| 
									
										
										
										
											2022-08-16 20:23:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\App\Model; | 
					
						
							| 
									
										
										
										
											2022-08-16 20:23:53 +08:00
										 |  |  | use BookStack\Entities\Models\Chapter; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ChapterLinkModelResolver implements CrossLinkModelResolver | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function resolve(string $link): ?Model | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  |         $pattern = '/^' . preg_quote(url('/books'), '/') . '\/([\w-]+)' . '\/chapter\/' . '([\w-]+)' . '([#?\/]|$)/'; | 
					
						
							| 
									
										
										
										
											2022-08-16 20:23:53 +08:00
										 |  |  |         $matches = []; | 
					
						
							|  |  |  |         $match = preg_match($pattern, $link, $matches); | 
					
						
							|  |  |  |         if (!$match) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $bookSlug = $matches[1]; | 
					
						
							|  |  |  |         $chapterSlug = $matches[2]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var ?Chapter $model */ | 
					
						
							| 
									
										
										
										
											2022-08-17 23:59:23 +08:00
										 |  |  |         $model = Chapter::query()->whereSlugs($bookSlug, $chapterSlug)->first(['id']); | 
					
						
							| 
									
										
										
										
											2022-08-16 20:23:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $model; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-08-30 00:46:41 +08:00
										 |  |  | } |