Skip to content

Commit

Permalink
#766 Added a function to add view helpers to a oai server.
Browse files Browse the repository at this point in the history
  • Loading branch information
haogatyp committed Oct 10, 2023
1 parent 59b25c3 commit 6527e7b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
20 changes: 19 additions & 1 deletion modules/oai/models/DefaultServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/modules/oai/models/BaseServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}

0 comments on commit 6527e7b

Please sign in to comment.