Moved logic to a service and left bookinfo as a separate container.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
21
src/BookFinder.php
Normal file
21
src/BookFinder.php
Normal 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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user