forked from vufind-org/vufind
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move record version key handling to the new Versions helper.
This makes the functionality reusable and overridable.
- Loading branch information
1 parent
1bc1f33
commit 2427e7e
Showing
4 changed files
with
202 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
/** | ||
* Helper that provides support methods for record versions search | ||
* | ||
* PHP version 7 | ||
* | ||
* Copyright (C) The National Library of Finland 2020-2023. | ||
* | ||
* 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 Record | ||
* @author Ere Maijala <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org Main Page | ||
*/ | ||
|
||
namespace VuFind\Record; | ||
|
||
/** | ||
* Helper that provides support methods for record versions search | ||
* | ||
* @category VuFind | ||
* @package Record | ||
* @author Ere Maijala <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org Main Page | ||
*/ | ||
class VersionsHelper | ||
{ | ||
/** | ||
* Record loader | ||
* | ||
* @var Loader | ||
*/ | ||
protected $recordLoader; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param Loader $recordLoader Record loader | ||
*/ | ||
public function __construct( | ||
Loader $recordLoader | ||
) { | ||
$this->recordLoader = $recordLoader; | ||
} | ||
|
||
/** | ||
* Get record driver and work keys from query params | ||
* | ||
* @param array $params Query params containing id and/or keys | ||
* @param string $backend Search backend ID | ||
* | ||
* @return array with driver and keys | ||
*/ | ||
public function getDriverAndWorkKeysFromParams( | ||
array $params, | ||
string $backend | ||
): array { | ||
$id = $params['id'] ?? null; | ||
$keys = $params['keys'] ?? null; | ||
$driver = null; | ||
if ($id) { | ||
$driver = $this->recordLoader->load($id, $backend, true); | ||
if (!($driver instanceof \VuFind\RecordDriver\Missing)) { | ||
$keys = $driver->tryMethod('getWorkKeys') ?? $keys; | ||
} | ||
} | ||
return compact('driver', 'keys'); | ||
} | ||
|
||
/** | ||
* Convert work keys to a search string | ||
* | ||
* @param array $keys Work keys | ||
* | ||
* @return string | ||
*/ | ||
public function getSearchStringFromWorkKeys(array $keys): string | ||
{ | ||
$mapFunc = function ($val) { | ||
return '"' . addcslashes($val, '"') . '"'; | ||
}; | ||
|
||
return implode(' OR ', array_map($mapFunc, (array)$keys)); | ||
} | ||
|
||
/** | ||
* Get search type for work keys search | ||
* | ||
* @return string | ||
*/ | ||
public function getWorkKeysSearchType(): string | ||
{ | ||
return 'WorkKeys'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/** | ||
* Versions helper factory. | ||
* | ||
* PHP version 7 | ||
* | ||
* Copyright (C) The National Library of Finland 2023. | ||
* | ||
* 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 Record | ||
* @author Ere Maijala <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org/wiki/development Wiki | ||
*/ | ||
|
||
namespace VuFind\Record; | ||
|
||
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; | ||
use Laminas\ServiceManager\Exception\ServiceNotFoundException; | ||
use Laminas\ServiceManager\Factory\FactoryInterface; | ||
use Psr\Container\ContainerExceptionInterface as ContainerException; | ||
use Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* Versions helper factory. | ||
* | ||
* @category VuFind | ||
* @package Record | ||
* @author Ere Maijala <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org/wiki/development Wiki | ||
*/ | ||
class VersionsHelperFactory implements FactoryInterface | ||
{ | ||
/** | ||
* Create an object | ||
* | ||
* @param ContainerInterface $container Service manager | ||
* @param string $requestedName Service being created | ||
* @param null|array $options Extra options (optional) | ||
* | ||
* @return object | ||
* | ||
* @throws ServiceNotFoundException if unable to resolve the service. | ||
* @throws ServiceNotCreatedException if an exception is raised when | ||
* creating a service. | ||
* @throws ContainerException&\Throwable if any other error occurs | ||
*/ | ||
public function __invoke( | ||
ContainerInterface $container, | ||
$requestedName, | ||
array $options = null | ||
) { | ||
if (!empty($options)) { | ||
throw new \Exception('Unexpected options passed to factory.'); | ||
} | ||
return new $requestedName( | ||
$container->get(\VuFind\Record\Loader::class) | ||
); | ||
} | ||
} |