Skip to content

Commit

Permalink
intrduced spec interface and created support method
Browse files Browse the repository at this point in the history
  • Loading branch information
ThoWagen committed Jan 24, 2025
1 parent 7735a70 commit 0be9adf
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* @link https://vufind.org/wiki/development:architecture:record_data_formatter
* Wiki
*/
abstract class AbstractBase implements \VuFind\I18n\Translator\TranslatorAwareInterface
abstract class AbstractBase implements SpecInterface, \VuFind\I18n\Translator\TranslatorAwareInterface
{
use \VuFind\I18n\Translator\TranslatorAwareTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
*/
protected function getExpectedInterface()
{
return AbstractBase::class;
return SpecInterface::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* RecordDataFormatter specs interface.
*
* PHP version 8
*
* Copyright (C) Hebis Verbundzentrale 2025.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package RecordDataFormatter
* @author Thomas Wagener <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:architecture:record_data_formatter
* Wiki
*/

namespace VuFind\RecordDataFormatter\Specs;

/**
* RecordDataFormatter specs interface.
*
* @category VuFind
* @package RecordDataFormatter
* @author Thomas Wagener <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:architecture:record_data_formatter
* Wiki
*/
interface SpecInterface
{
/**
* Get default configuration.
*
* @param string $key Key for configuration to look up.
*
* @return array
*/
public function getDefaults(string $key): array;
}
29 changes: 19 additions & 10 deletions module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

use Laminas\View\Helper\AbstractHelper;
use VuFind\RecordDataFormatter\Specs\PluginManager as SpecsManager;
use VuFind\RecordDataFormatter\Specs\SpecInterface;
use VuFind\RecordDriver\AbstractBase as RecordDriver;

use function call_user_func;
Expand Down Expand Up @@ -215,14 +216,10 @@ public function getData(...$args): array
*/
public function getDefaults(string $key): array
{
$specClass = \VuFind\RecordDataFormatter\Specs\DefaultRecord::class;
if ($this->driver !== null) {
$specClass = $this->driver->getRecordDataFormatterSpecClass();
$specs = $this->getSpecPluginForDriver();
if ($specs === null) {
throw new \Exception('Using the RecordDataFormatter view helper with a driver that is not supported.');
}
if ($specClass === null) {
return [];
}
$specs = $this->specsManager->get($specClass);
return $specs->getDefaults($key);
}

Expand All @@ -238,16 +235,28 @@ public function getDefaults(string $key): array
* @deprecated Set defaults on spec class directly
*/
public function setDefaults(string $key, array|callable $values): void
{
$specs = $this->getSpecPluginForDriver();
if ($specs !== null && method_exists($specs, 'setDefaults')) {
$specs->setDefaults($key, $values);
}
}

/**
* Get matching spec plugin for the driver.
*
* @return ?SpecInterface
*/
protected function getSpecPluginForDriver(): ?SpecInterface
{
$specClass = \VuFind\RecordDataFormatter\Specs\DefaultRecord::class;
if ($this->driver !== null) {
$specClass = $this->driver->getRecordDataFormatterSpecClass();
}
if ($specClass === null) {
return;
return null;
}
$specs = $this->specsManager->get($specClass);
$specs->setDefaults($key, $values);
return $this->specsManager->get($specClass);
}

/**
Expand Down

0 comments on commit 0be9adf

Please sign in to comment.