Initial bookinfo

This commit is contained in:
kplaczek
2022-05-23 20:36:28 +02:00
parent 02024755eb
commit 42e90c250e
5 changed files with 105 additions and 2 deletions

View File

@@ -0,0 +1,19 @@
<?php
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;
}

29
src/BookInfo.php Normal file
View File

@@ -0,0 +1,29 @@
<?php
namespace Techtube\Bookinfo;
use DiDom\Document;
use Techtube\Bookinfo\Api\AbstractBookInfo;
class BookInfo extends AbstractBookInfo
{
public function __construct($url)
{
$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->series = $series[1];
$this->volume = $series[2];
}
$this->language = $jsonInfo->inLanguage ?? null;
$this->datePublished = $jsonInfo->datePublished ?? null;
}
}