From 825348ce7a7e66275b28912b772c182ad25037b3 Mon Sep 17 00:00:00 2001 From: krzysiej Date: Thu, 26 May 2022 13:02:52 +0200 Subject: [PATCH] Added three sizes of book cover small medium and large to be returned as cover_url. --- src/Api/AbstractBookInfo.php | 2 +- src/DataParser.php | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Api/AbstractBookInfo.php b/src/Api/AbstractBookInfo.php index 378e190..cc286d7 100644 --- a/src/Api/AbstractBookInfo.php +++ b/src/Api/AbstractBookInfo.php @@ -10,7 +10,7 @@ abstract class AbstractBookInfo public string $description; public string $title; public string $category; - public string $cover_url; + public array $cover_url; public int $pages; public string $cycle; public int $volume; diff --git a/src/DataParser.php b/src/DataParser.php index 1a62dd8..7cb81c3 100644 --- a/src/DataParser.php +++ b/src/DataParser.php @@ -4,6 +4,7 @@ namespace Techtube\Bookinfo; use DiDom\Document; use DiDom\Exceptions\InvalidSelectorException; +use JetBrains\PhpStorm\ArrayShape; use Techtube\Bookinfo\Api\AbstractBookInfo; class DataParser @@ -22,7 +23,9 @@ class DataParser $info->description = $document->first('meta[property="og:description"]')->getAttribute('content'); $info->title = trim($document->first('h1.book__title')->text()); $info->category = trim($document->first('.book__category')->text()); - $info->cover_url = $document->first('meta[property="og:image"]')->getAttribute('content'); + $info->cover_url = $this->generateCoverUrls( + $document->first('meta[property="og:image"]')->getAttribute('content') + ); $info->pages = (int)$document->first('span.book__pages')?->text(); if (preg_match('#(.*) \(tom (\d*)\)#ism', trim($document->first('a[href*="/cykl/"]')?->text()), $series)) { $info->cycle = $series[1]; @@ -49,9 +52,24 @@ class DataParser $bookInfo->title = trim($book->first('.authorAllBooks__singleTextTitle')->text()); $bookInfo->author = trim($book->first('.authorAllBooks__singleTextAuthor')->text()); $bookInfo->url = $book->first('button[data-book-url]')->getAttribute('data-book-url'); - $bookInfo->cover_url = $book->first('.img-fluid')->getAttribute('data-src'); + $bookInfo->cover_url = $this->generateCoverUrls($book->first('.img-fluid')->getAttribute('data-src')); $booksInfo[] = $bookInfo; } return $booksInfo; } + + /** + * @param string $coverUrl + * @return array + */ + #[ArrayShape(['small' => "string", 'medium' => "string", 'large' => "string"])] + private function generateCoverUrls(string $coverUrl): array + { + $coverUrlBase = preg_replace('(\d*?x\d*?\.jpg)', '', $coverUrl); + return [ + 'small' => $coverUrlBase . '70x100.jpg', + 'medium' => $coverUrlBase . '170x243.jpg', + 'large' => $coverUrlBase . '352x500.jpg' + ]; + } } \ No newline at end of file