From 6527e7b4287a13579605fadad95596c558471e79 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 10 Oct 2023 19:09:21 +0200 Subject: [PATCH] #766 Added a function to add view helpers to a oai server. --- modules/oai/models/DefaultServer.php | 20 ++++++++++++++++- tests/modules/oai/models/BaseServerTest.php | 24 +++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index 50c6eb12e..d534673bc 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -1143,7 +1143,7 @@ public function getViewHelpers() public function setViewHelpers($viewHelpers) { if (is_string($viewHelpers)) { - $viewHelpers = $viewHelpers !== '' ? array_map('trim', explode(',', $viewHelpers)) : []; + $viewHelpers = trim($viewHelpers) !== '' ? array_map('trim', explode(',', $viewHelpers)) : []; } if (is_array($viewHelpers)) { @@ -1154,6 +1154,24 @@ public function setViewHelpers($viewHelpers) $this->viewHelpers = $viewHelpers; } + /** + * Adds viewHelpers + * + * @param array|string $viewHelpers + */ + public function addViewHelpers($viewHelpers) + { + if (is_string($viewHelpers)) { + $viewHelpers = trim($viewHelpers) !== '' ? array_map('trim', explode(',', $viewHelpers)) : []; + } + + if (is_array($viewHelpers)) { + $previousViewHelpers = $this->viewHelpers ?? []; + $viewHelpers = array_unique(array_merge($previousViewHelpers, $viewHelpers)); + $this->viewHelpers = $viewHelpers; + } + } + /** * Gets the checkEmbargo option. * diff --git a/tests/modules/oai/models/BaseServerTest.php b/tests/modules/oai/models/BaseServerTest.php index 7298b8f1c..988ae6e12 100644 --- a/tests/modules/oai/models/BaseServerTest.php +++ b/tests/modules/oai/models/BaseServerTest.php @@ -105,4 +105,28 @@ public function testSetDocumentTypesAllowedWithEmptyString() $baseServer->setDocumentTypesAllowed(''); $this->assertEquals([], $baseServer->getDocumentTypesAllowed()); } + + public function testAddViewHelpers() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setViewHelpers('viewhelper1, viewhelper2'); + $this->assertEquals(['viewhelper1', 'viewhelper2', 'listMetadataFormats'], $baseServer->getViewHelpers()); + + $baseServer->addViewHelpers('viewhelper3, viewhelper4'); + $this->assertEquals( + ['viewhelper1', 'viewhelper2', 'viewhelper3', 'viewhelper4', 'listMetadataFormats'], + $baseServer->getViewHelpers() + ); + } + + public function testAddViewHelpersWithEmptyString() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setViewHelpers('viewhelper1, viewhelper2'); + $baseServer->addViewHelpers(''); + $this->assertEquals( + ['viewhelper1', 'viewhelper2', 'listMetadataFormats'], + $baseServer->getViewHelpers() + ); + } }