Skip to content

Commit

Permalink
v1.0.22
Browse files Browse the repository at this point in the history
- Fix XML search page root
  • Loading branch information
ewilan-riviere committed Sep 14, 2023
1 parent 52f4747 commit f735885
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion 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.21",
"version": "1.0.22",
"keywords": [
"php",
"ebook",
Expand Down
21 changes: 15 additions & 6 deletions src/Engine/OpdsEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ public function getContents(): array
/**
* Convert `content` to XML.
*/
protected function toXML(): string
protected function toXML(array $rootElement = [
'rootElementName' => 'feed',
'_attributes' => OpdsNamespaces::VERSION_1_2,
]): string
{
return ArrayToXml::convert(
array: $this->contents,
rootElement: [
'rootElementName' => 'feed',
'_attributes' => OpdsNamespaces::VERSION_1_2,
],
rootElement: $rootElement,
replaceSpacesByUnderScoresInKeyNames: true,
xmlEncoding: 'UTF-8'
);
Expand All @@ -117,7 +117,16 @@ protected function toJSON(): string
public function __toString(): string
{
if ($this->opds->getOutput() === OpdsOutputEnum::xml) {
return $this->toXML();
if (! $this->getOpds()->checkIfSearch()) {
return $this->toXML();
}

return $this->toXML([
'rootElementName' => 'OpenSearchDescription',
'_attributes' => [
'xmlns' => 'http://a9.com/-/spec/opensearch/1.1/',
],
]);
}

return $this->toJSON();
Expand Down
25 changes: 25 additions & 0 deletions tests/OpdsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,28 @@
expect(fn () => $opds->url('http://localhost:8000/opds?version=0.8'))->toThrow(Exception::class);
expect(fn () => $opds->url('http://localhost:8000/opds?version=0.8'))->toThrow('OPDS version 0.8 is not supported.');
});

it('can use search', function () {
$opds = Opds::make()
->title('feed')
->isSearch()
->get();

$xml = XmlReader::make($opds->getResponse()->getContents());

expect($opds->getOutput())->toBe(OpdsOutputEnum::xml);
expect($xml->getRoot())->toBe('OpenSearchDescription');
expect($xml->getRootNS()['xmlns'])->toBe('http://a9.com/-/spec/opensearch/1.1/');
expect(count($xml->getContent()))->toBe(12);

$url = $xml->getContent()['Url'];
$self = $url[0]['@attributes'];
$search = $url[1]['@attributes'];

expect($self['template'])->toBe('');
expect($self['type'])->toBe('application/opensearchdescription+xml');
expect($self['rel'])->toBe('self');

expect($search['template'])->toBe('?q={searchTerms}');
expect($search['type'])->toBe('application/atom+xml');
});

0 comments on commit f735885

Please sign in to comment.