|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
|
9
|
|
|
* |
|
10
|
|
|
* @license GNU General Public License version 3 or later. |
|
11
|
|
|
* For the full copyright and license information, please read the |
|
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Kitodo\Dlf\Validation; |
|
16
|
|
|
|
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
19
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Abstract class provides functions for implementing a validation stack. |
|
23
|
|
|
* |
|
24
|
|
|
* @package TYPO3 |
|
25
|
|
|
* @subpackage dlf |
|
26
|
|
|
* |
|
27
|
|
|
* @access public |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract class AbstractDlfValidationStack extends AbstractDlfValidator |
|
30
|
|
|
{ |
|
31
|
|
|
use LoggerAwareTrait; |
|
32
|
|
|
|
|
33
|
|
|
const ITEM_KEY_TITLE = "title"; |
|
34
|
|
|
const ITEM_KEY_BREAK_ON_ERROR = "breakOnError"; |
|
35
|
|
|
const ITEM_KEY_VALIDATOR = "validator"; |
|
36
|
|
|
|
|
37
|
|
|
protected array $validatorStack = []; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(string $valueClassName) |
|
40
|
|
|
{ |
|
41
|
|
|
parent::__construct($valueClassName); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Add validators by validation stack configuration to the internal validator stack. |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $configuration The configuration of validators |
|
48
|
|
|
* |
|
49
|
|
|
* @throws InvalidArgumentException |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
public function addValidators(array $configuration): void |
|
54
|
|
|
{ |
|
55
|
|
|
foreach ($configuration as $configurationItem) { |
|
56
|
|
|
if (!class_exists($configurationItem["className"])) { |
|
57
|
|
|
$this->logger->error('Unable to load class ' . $configurationItem["className"] . '.'); |
|
58
|
|
|
throw new InvalidArgumentException('Unable to load validator class.', 1723200537037); |
|
59
|
|
|
} |
|
60
|
|
|
$breakOnError = !isset($configurationItem["breakOnError"]) || $configurationItem["breakOnError"] !== "false"; |
|
61
|
|
|
$this->addValidator($configurationItem["className"], $configurationItem["title"] ?? "", $breakOnError, $configurationItem["configuration"] ?? []); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Add validator to the internal validator stack. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $className Class name of the validator which was derived from Kitodo\Dlf\Validation\AbstractDlfValidator |
|
69
|
|
|
* @param string $title The title of the validator |
|
70
|
|
|
* @param bool $breakOnError True if the execution of validator stack is interrupted when validator throws an error |
|
71
|
|
|
* @param array|null $configuration The configuration of validator |
|
72
|
|
|
* |
|
73
|
|
|
* @throws InvalidArgumentException |
|
74
|
|
|
* |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function addValidator(string $className, string $title, bool $breakOnError = true, array $configuration = null): void |
|
78
|
|
|
{ |
|
79
|
|
|
if ($configuration === null) { |
|
80
|
|
|
$validator = GeneralUtility::makeInstance($className); |
|
81
|
|
|
} else { |
|
82
|
|
|
$validator = GeneralUtility::makeInstance($className, $configuration); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (!$validator instanceof AbstractDlfValidator) { |
|
86
|
|
|
$this->logger->error($className . ' must be an instance of AbstractDlfValidator.'); |
|
87
|
|
|
throw new InvalidArgumentException('Class must be an instance of AbstractDlfValidator.', 1723121212747); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$title = empty($title) ? $className : $title; |
|
91
|
|
|
|
|
92
|
|
|
$this->validatorStack[] = [self::ITEM_KEY_TITLE => $title, self::ITEM_KEY_VALIDATOR => $validator, self::ITEM_KEY_BREAK_ON_ERROR => $breakOnError]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Check if value is valid across all validation classes of validation stack. |
|
97
|
|
|
* |
|
98
|
|
|
* @param $value The value of defined class name. |
|
|
|
|
|
|
99
|
|
|
* |
|
100
|
|
|
* @throws InvalidArgumentException |
|
101
|
|
|
* |
|
102
|
|
|
* @return void |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function isValid($value): void |
|
105
|
|
|
{ |
|
106
|
|
|
if (!$value instanceof $this->valueClassName) { |
|
107
|
|
|
$this->logger->error('Value must be an instance of ' . $this->valueClassName . '.'); |
|
108
|
|
|
throw new InvalidArgumentException('Type of value is not valid.', 1723127564821); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if (empty($this->validatorStack)) { |
|
112
|
|
|
$this->logger->error('The validation stack has no validator.'); |
|
113
|
|
|
throw new InvalidArgumentException('The validation stack has no validator.', 1724662426); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
foreach ($this->validatorStack as $validationStackItem) { |
|
117
|
|
|
$validator = $validationStackItem[self::ITEM_KEY_VALIDATOR]; |
|
118
|
|
|
$result = $validator->validate($value); |
|
119
|
|
|
|
|
120
|
|
|
foreach ($result->getErrors() as $error) { |
|
121
|
|
|
$this->addError($error->getMessage(), $error->getCode(), [], $validationStackItem[self::ITEM_KEY_TITLE]); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if ($validationStackItem[self::ITEM_KEY_BREAK_ON_ERROR] && $result->hasErrors()) { |
|
125
|
|
|
break; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths