Skip to content

Commit

Permalink
Merge pull request #37 from kiwilan/develop
Browse files Browse the repository at this point in the history
v1.0.30
  • Loading branch information
ewilan-riviere authored Sep 23, 2023
2 parents bdcd459 + 88961ad commit fc01714
Show file tree
Hide file tree
Showing 32 changed files with 1,409 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
[*.{yml,yaml,json}]
indent_size = 2
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Some resources about OPDS and eBooks:

- OPDS 1.2: support advanced acquisition feeds
- OPDS 2.0: support `Facets`, `Groups`, advanced `belongsTo`
- Add [OPDS Page Streaming Extension](- https://github.com/anansi-project/opds-pse) from `anansi-project`
- Add [OPDS Page Streaming Extension](https://github.com/anansi-project/opds-pse) from `anansi-project`

## Installation

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kiwilan/php-opds",
"description": "PHP package to create OPDS feed for eBooks.",
"version": "1.0.23",
"version": "1.0.30",
"keywords": [
"php",
"ebook",
Expand All @@ -26,9 +26,11 @@
"require-dev": {
"kiwilan/php-xml-reader": "^1.0",
"laravel/pint": "^1.2",
"opis/json-schema": "^2.3",
"pestphp/pest": "^1.20",
"phpstan/phpstan": "^1.10",
"spatie/ray": "^1.28"
"spatie/ray": "^1.28",
"symfony/console": "^6.3"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 8 additions & 8 deletions src/Engine/OpdsJsonEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function addBookEntry(OpdsEntryBook $entry): array
}

$serie = $entry->getSerie();
$belongsTo = null;
$belongsTo = (object) [];

if ($serie) {
$belongsTo = [
Expand Down Expand Up @@ -150,11 +150,11 @@ public function addBookEntry(OpdsEntryBook $entry): array
'@type' => 'http://schema.org/EBook',
'identifier' => $identifier,
'title' => $entry->getTitle(),
'author' => $mainAuthor,
'translator' => $entry->getTranslator(),
'language' => $entry->getLanguage(),
'publisher' => $entry->getPublisher(),
'modified' => $entry->getUpdated(),
'author' => $mainAuthor ?? '',
'translator' => $entry->getTranslator() ?? '',
'language' => $entry->getLanguage() ?? 'English',
'publisher' => $entry->getPublisher() ?? '',
'modified' => $entry->getUpdated()->format(DATE_ATOM),
'description' => $summary,
'belongsTo' => $belongsTo,
],
Expand All @@ -163,8 +163,8 @@ public function addBookEntry(OpdsEntryBook $entry): array
$this->addJsonLink(rel: 'http://opds-spec.org/acquisition', href: $entry->getDownload(), type: 'application/epub+zip'),
],
'images' => [
['href' => $entry->getMedia(), 'type' => 'image/jpeg', 'height' => 1400, 'width' => 800],
['href' => $entry->getMediaThumbnail(), 'type' => 'image/jpeg', 'height' => 700, 'width' => 400],
['href' => $entry->getMedia() ?? '', 'type' => 'image/jpeg', 'height' => 1400, 'width' => 800],
['href' => $entry->getMediaThumbnail() ?? '', 'type' => 'image/jpeg', 'height' => 700, 'width' => 400],
// ['href' => 'http://example.org/cover.svg', 'type' => 'image/svg+xml'],
],
];
Expand Down
6 changes: 4 additions & 2 deletions src/Engine/OpdsXmlEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ public function addBookEntry(OpdsEntryBook $entry): array
$categories[] = $this->addXmlNode(attributes: ['term' => $item, 'label' => $item]);
}

foreach ($entry->getAuthors() as $item) {
$authors[] = ['name' => $item->getName(), 'uri' => $item->getUri()];
if ($entry->getAuthors()) {
foreach ($entry->getAuthors() as $item) {
$authors[] = ['name' => $item->getName(), 'uri' => $item->getUri()];
}
}

$media = $entry->getMedia();
Expand Down
16 changes: 8 additions & 8 deletions src/Entries/OpdsEntryBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class OpdsEntryBook extends OpdsEntryNavigation
{
/**
* @param string[] $categories
* @param OpdsEntryBookAuthor[] $authors
* @param OpdsEntryBookAuthor[]|null $authors
* @param ?string $isbn @deprecated Use `identifier` instead
*/
public function __construct(
Expand All @@ -22,9 +22,9 @@ public function __construct(
protected ?string $download = null,
protected ?string $mediaThumbnail = null,
protected array $categories = [],
protected array $authors = [],
protected ?array $authors = [],
protected DateTime|string|null $published = null,
protected ?int $volume = null,
protected int|float|null $volume = null,
protected ?string $serie = null,
protected ?string $language = null,
protected ?string $isbn = null,
Expand Down Expand Up @@ -84,7 +84,7 @@ public function published(DateTime|string|null $published): self
return $this;
}

public function volume(int $volume): self
public function volume(int|float|null $volume): self
{
$this->volume = $volume;

Expand Down Expand Up @@ -152,9 +152,9 @@ public function getCategories(): array
}

/**
* @return OpdsEntryBookAuthor[]
* @return OpdsEntryBookAuthor[]|null
*/
public function getAuthors(): array
public function getAuthors(): ?array
{
return $this->authors;
}
Expand All @@ -164,7 +164,7 @@ public function getPublished(): DateTime|string|null
return $this->published;
}

public function getVolume(): ?int
public function getVolume(): int|float|null
{
return $this->volume;
}
Expand Down Expand Up @@ -205,7 +205,7 @@ public function toArray(): array
'download' => $this->getDownload(),
'mediaThumbnail' => $this->getMediaThumbnail(),
'categories' => $this->getCategories(),
'authors' => array_map(fn (OpdsEntryBookAuthor $author) => $author->toArray(), $this->getAuthors()),
'authors' => $this->getAuthors() ? array_map(fn (OpdsEntryBookAuthor $author) => $author->toArray(), $this->getAuthors()) : null,
'published' => $this->getPublished(),
'volume' => $this->getVolume(),
'serie' => $this->getSerie(),
Expand Down
4 changes: 2 additions & 2 deletions src/Opds.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ public function get(): self
/**
* Send response to browser.
*
* @param bool $mock To send valid response to browser it should be to `true`.
* @param bool $mock To send valid response to browser it should be to `false`.
* @return void|never
*/
public function send(bool $mock = true)
public function send(bool $mock = false)
{
if (! $this->response) {
$this->get();
Expand Down
20 changes: 17 additions & 3 deletions src/OpdsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ public function getContents(): string
return $this->contents;
}

/**
* Get JSON contents if is valid.
*
* @throws \Exception
*/
public function getJson(): object
{
if (! $this->isJson) {
throw new \Exception('OPDS Response: content is not JSON');
}

return json_decode($this->contents);
}

/**
* Set headers.
*
Expand All @@ -118,10 +132,10 @@ public function setContents(string $contents): self
/**
* Send content to browser with correct header.
*
* @param bool $mock To send valid response to browser it should be to `true`.
* @param bool $mock To send valid response to browser it should be to `false`.
* @return never|void
*/
public function send(bool $mock = true)
public function send(bool $mock = false)
{
foreach ($this->headers as $type => $value) {
header($type.': '.$value);
Expand All @@ -131,7 +145,7 @@ public function send(bool $mock = true)

echo $this->contents;

if ($mock) {
if (! $mock) {
exit;
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/OpdsEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
expect($entry->getTranslator())->toBe('Translator');
expect($entry->getPublisher())->toBe('Publisher');
expect($entry->toArray())->toBeArray();

$entry = $entry->volume(1.2);
expect($entry->getVolume())->toBe(1.2);
})->with('feeds-books');

it('can use setter', function () {
Expand Down
27 changes: 27 additions & 0 deletions tests/OpdsJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,30 @@ function getConfigV2(): OpdsConfig
expect($opds)->toBeInstanceOf(Opds::class);
expect($opds->getEngine()->getContents())->toBeArray();
});

// https://github.com/opds-community/drafts/tree/master/schema
it('can validate metadata schema', function () {
$opds = Opds::make(getConfigV2())->get();

$res = validator()->validate(
$opds->getResponse()->getJson()->metadata,
getSchema(SCHEMA_FEED_METADATA)
);

printValidatorErrors($res);
expect($res->isValid())->toBeTrue();
});

it('can validate feed schema', function () {
$opds = Opds::make(getConfigV2())
->feeds(manyFeeds())
->get();

$res = validator()->validate(
$opds->getResponse()->getJson(),
getSchema(FEED_SCHEMA),
);

printValidatorErrors($res);
expect($res->isValid())->toBeTrue();
});
4 changes: 2 additions & 2 deletions tests/OpdsResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
$response = OpdsResponse::make($engine, $opds->getOutput(), 200);
$response->setContents(exampleXml());

$response->send(mock: false);
$response->send(mock: true);

expect($opds)->toBeInstanceOf(Opds::class);
})->expectOutputString(exampleXml());

it('can use response method', function () {
$opds = Opds::make() // @phpstan-ignore-line
->send(mock: false);
->send(mock: true);

expect($opds)->toBeNull();
});
28 changes: 28 additions & 0 deletions tests/OpdsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,31 @@
expect($search['template'])->toBe('?q={searchTerms}');
expect($search['type'])->toBe('application/atom+xml');
});

it('can have engine string', function () {
$opds = Opds::make()
->feeds([]);
$engine = $opds->getEngine();

expect($engine->__toString())->toBeString();

$opds = Opds::make()
->feeds([])
->isSearch();
$engine = $opds->getEngine();
$xml = XmlReader::make($engine->__toString());

expect($engine->__toString())->toBeString();
expect($xml->getRoot())->toBe('OpenSearchDescription');

$opds = Opds::make()
->feeds(manyFeeds());
$engine = $opds->getEngine();

expect($engine->__toString())->toBeString();

$opds = Opds::make(getConfigV2())
->feeds(manyFeeds());

expect($opds->getEngine()->__toString())->toBeString();
});
Loading

0 comments on commit fc01714

Please sign in to comment.