1<?php 2 3namespace ComboStrap; 4 5use ComboStrap\Web\Url; 6 7class FetcherPdf extends FetcherRawLocalPath 8{ 9 10 11 private ?int $pageNumber = null; 12 13 14 public function buildFromUrl(Url $url): FetcherPdf 15 { 16 try { 17 $fragment = $url->getFragment(); 18 $fragments = explode($fragment, "="); 19 if ($fragments[0] === "page" && sizeof($fragments) >= 2) { 20 try { 21 $this->pageNumber = DataType::toInteger($fragments[1]); 22 } catch (ExceptionBadArgument $e) { 23 throw new ExceptionBadArgument("The pdf page number anchor seems to not be a number. Error: {$e->getMessage()}"); 24 } 25 } 26 } catch (ExceptionNotFound $e) { 27 // ok no page 28 } 29 parent::buildFromUrl($url); 30 return $this; 31 } 32 33 public function getMime(): Mime 34 { 35 return Mime::create(Mime::PDF); 36 } 37 38 function getFetchUrl(Url $url = null): Url 39 { 40 $url = parent::getFetchUrl($url); 41 if ($this->pageNumber !== null) { 42 $url->setFragment("page={$this->pageNumber}"); 43 } 44 return $url; 45 } 46 47 /** 48 * @throws ExceptionNotFound 49 */ 50 function getPageNumber(): int 51 { 52 if ($this->pageNumber === null) { 53 throw new ExceptionNotFound("No page number"); 54 } 55 return $this->pageNumber; 56 } 57 58} 59