From f735885011e918463e7fcd601ca3ef6bea0bac8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ewilan=20Rivi=C3=A8re?= Date: Thu, 14 Sep 2023 17:43:42 +0200 Subject: [PATCH] v1.0.22 - Fix XML search page root --- composer.json | 2 +- src/Engine/OpdsEngine.php | 21 +++++++++++++++------ tests/OpdsTest.php | 25 +++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 68c5d52..245bb01 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Engine/OpdsEngine.php b/src/Engine/OpdsEngine.php index 89ce122..3335012 100644 --- a/src/Engine/OpdsEngine.php +++ b/src/Engine/OpdsEngine.php @@ -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' ); @@ -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(); diff --git a/tests/OpdsTest.php b/tests/OpdsTest.php index 09e8b08..392e5ff 100644 --- a/tests/OpdsTest.php +++ b/tests/OpdsTest.php @@ -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'); +});