Moved logic to a service and left bookinfo as a separate container.

This commit is contained in:
kplaczek
2022-05-23 23:18:12 +02:00
parent 292b24c025
commit 0e3c8852ce
4 changed files with 51 additions and 23 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,2 @@
/vendor/ /vendor/
.idea .idea
/vendor/

View File

@@ -4,16 +4,17 @@ namespace Techtube\Bookinfo\Api;
abstract class AbstractBookInfo abstract class AbstractBookInfo
{ {
protected string $author; public string $author;
protected string $isbn; public string $isbn;
protected string $description; public string $description;
protected string $title; public string $title;
protected string $category; public string $category;
protected string $cover_url; public string $cover_url;
protected int $pages; public int $pages;
protected string $series; public string $series;
protected int $volume; public int $volume;
protected string $language; public string $language;
protected string $datePublished; public string $datePublished;
abstract public function parse();
} }

21
src/BookFinder.php Normal file
View File

@@ -0,0 +1,21 @@
<?php
namespace Techtube\Bookinfo;
use DiDom\Document;
use Techtube\Bookinfo\Api\AbstractBookInfo;
class BookFinder
{
public function search(string $searchText): \Exception
{
return new \Exception('need to implement this');
}
public function byUrl($url): AbstractBookInfo
{
return (new BookInfo(new Document($url, true)))->parse();
}
}

View File

@@ -7,23 +7,30 @@ use Techtube\Bookinfo\Api\AbstractBookInfo;
class BookInfo extends AbstractBookInfo class BookInfo extends AbstractBookInfo
{ {
public function __construct($url) private Document $document;
public function __construct($document)
{ {
$document = new Document($url, true); $this->document = $document;
$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'); public function parse(): self
$this->description = $document->first('meta[property="og:description"]')->getAttribute('content'); {
$this->title = trim($document->first('h1.book__title')->text()); $jsonInfo = json_decode($this->document->first('script[type="application/ld+json"]')->text());
$this->category = trim($document->first('.book__category')->text()); $this->author = $this->document->first('meta[property="books:author"]')->getAttribute('content');
$this->cover_url = $document->first('meta[property="og:image"]')->getAttribute('content'); $this->isbn = $this->document->first('meta[property="books:isbn"]')->getAttribute('content');
$this->pages = (int)$document->first('span.book__pages')?->text(); $this->description = $this->document->first('meta[property="og:description"]')->getAttribute('content');
if (preg_match('#(.*) \(tom (\d*)\)#ism', trim($document->first('a[href*="/cykl/"]')->text()), $series)) { $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->series = $series[1];
$this->volume = $series[2]; $this->volume = $series[2];
} }
$this->language = $jsonInfo->inLanguage ?? null; $this->language = $jsonInfo->inLanguage ?? null;
$this->datePublished = $jsonInfo->datePublished ?? null; $this->datePublished = $jsonInfo->datePublished ?? null;
return $this;
} }
} }