From 0e3c8852ce1019890c2c6ad54373e6dc9f9e1f37 Mon Sep 17 00:00:00 2001 From: kplaczek Date: Mon, 23 May 2022 23:18:12 +0200 Subject: [PATCH] Moved logic to a service and left bookinfo as a separate container. --- .gitignore | 1 - src/Api/AbstractBookInfo.php | 23 ++++++++++++----------- src/BookFinder.php | 21 +++++++++++++++++++++ src/BookInfo.php | 29 ++++++++++++++++++----------- 4 files changed, 51 insertions(+), 23 deletions(-) create mode 100644 src/BookFinder.php diff --git a/.gitignore b/.gitignore index 73c2c96..0dca145 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ /vendor/ .idea -/vendor/ diff --git a/src/Api/AbstractBookInfo.php b/src/Api/AbstractBookInfo.php index fae3f5f..1c89093 100644 --- a/src/Api/AbstractBookInfo.php +++ b/src/Api/AbstractBookInfo.php @@ -4,16 +4,17 @@ namespace Techtube\Bookinfo\Api; abstract class AbstractBookInfo { - protected string $author; - protected string $isbn; - protected string $description; - protected string $title; - protected string $category; - protected string $cover_url; - protected int $pages; - protected string $series; - protected int $volume; - protected string $language; - protected string $datePublished; + public string $author; + public string $isbn; + public string $description; + public string $title; + public string $category; + public string $cover_url; + public int $pages; + public string $series; + public int $volume; + public string $language; + public string $datePublished; + abstract public function parse(); } \ No newline at end of file diff --git a/src/BookFinder.php b/src/BookFinder.php new file mode 100644 index 0000000..a72bb61 --- /dev/null +++ b/src/BookFinder.php @@ -0,0 +1,21 @@ +parse(); + } +} + diff --git a/src/BookInfo.php b/src/BookInfo.php index 24913a9..c56b35b 100644 --- a/src/BookInfo.php +++ b/src/BookInfo.php @@ -7,23 +7,30 @@ use Techtube\Bookinfo\Api\AbstractBookInfo; class BookInfo extends AbstractBookInfo { - public function __construct($url) + private Document $document; + + public function __construct($document) { - $document = new Document($url, true); - $jsonInfo = json_decode($document->first('script[type="application/ld+json"]')->text()); - $this->author = $document->first('meta[property="books:author"]')->getAttribute('content'); - $this->isbn = $document->first('meta[property="books:isbn"]')->getAttribute('content'); - $this->description = $document->first('meta[property="og:description"]')->getAttribute('content'); - $this->title = trim($document->first('h1.book__title')->text()); - $this->category = trim($document->first('.book__category')->text()); - $this->cover_url = $document->first('meta[property="og:image"]')->getAttribute('content'); - $this->pages = (int)$document->first('span.book__pages')?->text(); - if (preg_match('#(.*) \(tom (\d*)\)#ism', trim($document->first('a[href*="/cykl/"]')->text()), $series)) { + $this->document = $document; + } + + public function parse(): self + { + $jsonInfo = json_decode($this->document->first('script[type="application/ld+json"]')->text()); + $this->author = $this->document->first('meta[property="books:author"]')->getAttribute('content'); + $this->isbn = $this->document->first('meta[property="books:isbn"]')->getAttribute('content'); + $this->description = $this->document->first('meta[property="og:description"]')->getAttribute('content'); + $this->title = trim($this->document->first('h1.book__title')->text()); + $this->category = trim($this->document->first('.book__category')->text()); + $this->cover_url = $this->document->first('meta[property="og:image"]')->getAttribute('content'); + $this->pages = (int)$this->document->first('span.book__pages')?->text(); + if (preg_match('#(.*) \(tom (\d*)\)#ism', trim($this->document->first('a[href*="/cykl/"]')?->text()), $series)) { $this->series = $series[1]; $this->volume = $series[2]; } $this->language = $jsonInfo->inLanguage ?? null; $this->datePublished = $jsonInfo->datePublished ?? null; + return $this; } }