|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of OPUS. The software OPUS has been originally developed |
| 5 | + * at the University of Stuttgart with funding from the German Research Net, |
| 6 | + * the Federal Department of Higher Education and Research and the Ministry |
| 7 | + * of Science, Research and the Arts of the State of Baden-Wuerttemberg. |
| 8 | + * |
| 9 | + * OPUS 4 is a complete rewrite of the original OPUS software and was developed |
| 10 | + * by the Stuttgart University Library, the Library Service Center |
| 11 | + * Baden-Wuerttemberg, the Cooperative Library Network Berlin-Brandenburg, |
| 12 | + * the Saarland University and State Library, the Saxon State Library - |
| 13 | + * Dresden State and University Library, the Bielefeld University Library and |
| 14 | + * the University Library of Hamburg University of Technology with funding from |
| 15 | + * the German Research Foundation and the European Regional Development Fund. |
| 16 | + * |
| 17 | + * LICENCE |
| 18 | + * OPUS is free software; you can redistribute it and/or modify it under the |
| 19 | + * terms of the GNU General Public License as published by the Free Software |
| 20 | + * Foundation; either version 2 of the Licence, or any later version. |
| 21 | + * OPUS is distributed in the hope that it will be useful, but WITHOUT ANY |
| 22 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 23 | + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 24 | + * details. You should have received a copy of the GNU General Public License |
| 25 | + * along with OPUS; if not, write to the Free Software Foundation, Inc., 51 |
| 26 | + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | + * |
| 28 | + * @copyright Copyright (c) 2023, OPUS 4 development team |
| 29 | + * @license http://www.gnu.org/licenses/gpl.html General Public License |
| 30 | + */ |
| 31 | + |
| 32 | +namespace Opus\Import; |
| 33 | + |
| 34 | +use Opus\Common\ConfigTrait; |
| 35 | +use Opus\Common\DocumentInterface; |
| 36 | +use Zend_Config_Ini; |
| 37 | + |
| 38 | +use function class_exists; |
| 39 | +use function filter_var; |
| 40 | +use function is_array; |
| 41 | +use function is_readable; |
| 42 | + |
| 43 | +use const FILTER_VALIDATE_BOOLEAN; |
| 44 | + |
| 45 | +class ImportRules |
| 46 | +{ |
| 47 | + use ConfigTrait; |
| 48 | + |
| 49 | + public const IMPORT_RULE_CLASS_PREFIX = 'Opus\\Import\\Rules\\'; |
| 50 | + |
| 51 | + /** @var ImportRuleInterface[] */ |
| 52 | + private $rules = []; |
| 53 | + |
| 54 | + /** |
| 55 | + * Loads rules from configuration. |
| 56 | + */ |
| 57 | + public function init() |
| 58 | + { |
| 59 | + $config = $this->getConfig(); |
| 60 | + |
| 61 | + if ( |
| 62 | + ! isset($config->sword->enableImportRules) || |
| 63 | + ! filter_var($config->sword->enableImportRules, FILTER_VALIDATE_BOOLEAN) |
| 64 | + ) { |
| 65 | + // TODO does this belong here? There should not be anything SWORD specific here! |
| 66 | + return; // don't load any rules |
| 67 | + } |
| 68 | + |
| 69 | + $rulesConfig = null; |
| 70 | + |
| 71 | + if (isset($config->import->rulesConfigFile)) { |
| 72 | + $rulesConfigFile = $config->import->rulesConfigFile; |
| 73 | + if (is_readable($rulesConfigFile)) { |
| 74 | + $rulesConfig = new Zend_Config_Ini($rulesConfigFile); |
| 75 | + $rulesConfig = $rulesConfig->toArray(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Get rules from main configuration as fallback |
| 80 | + if ($rulesConfig === null && isset($config->import->rules)) { |
| 81 | + $rulesConfig = $config->import->rules->toArray(); |
| 82 | + } |
| 83 | + |
| 84 | + if (is_array($rulesConfig)) { |
| 85 | + foreach ($rulesConfig as $name => $options) { |
| 86 | + $type = $options['type']; |
| 87 | + |
| 88 | + $rule = $this->createRule($type, $options); |
| 89 | + |
| 90 | + if ($rule !== null) { |
| 91 | + $this->rules[] = $rule; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return ImportRuleInterface[] |
| 99 | + */ |
| 100 | + public function getRules() |
| 101 | + { |
| 102 | + return $this->rules; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @param string $type |
| 107 | + * @param array $options |
| 108 | + * @return ImportRuleInterface|null |
| 109 | + */ |
| 110 | + public function createRule($type, $options) |
| 111 | + { |
| 112 | + if (class_exists($type)) { |
| 113 | + $ruleClass = $type; |
| 114 | + } else { |
| 115 | + $ruleClass = self::IMPORT_RULE_CLASS_PREFIX . $type; |
| 116 | + if (! class_exists($ruleClass)) { |
| 117 | + // TODO throw exception |
| 118 | + return null; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + $rule = new $ruleClass(); |
| 123 | + $rule->setOptions($options); |
| 124 | + |
| 125 | + return $rule; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @param DocumentInterface $document |
| 130 | + */ |
| 131 | + public function apply($document) |
| 132 | + { |
| 133 | + foreach ($this->getRules() as $rule) { |
| 134 | + $rule->apply($document); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments