[TASK] Streamline phpdoc annotations in EXT:extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Validation / Validator / AbstractValidator.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Validation\Validator;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException;
18
19 /**
20 * Abstract validator
21 */
22 abstract class AbstractValidator implements ValidatorInterface
23 {
24 /**
25 * Specifies whether this validator accepts empty values.
26 *
27 * If this is TRUE, the validators isValid() method is not called in case of an empty value
28 * Note: A value is considered empty if it is NULL or an empty string!
29 * By default all validators except for NotEmpty and the Composite Validators accept empty values
30 *
31 * @var bool
32 */
33 protected $acceptsEmptyValues = true;
34
35 /**
36 * This contains the supported options, their default values, types and descriptions.
37 *
38 * @var array
39 */
40 protected $supportedOptions = [];
41
42 /**
43 * @var array
44 */
45 protected $options = [];
46
47 /**
48 * @var \TYPO3\CMS\Extbase\Error\Result
49 */
50 protected $result;
51
52 /**
53 * Constructs the validator and sets validation options
54 *
55 * @param array $options Options for the validator
56 * @throws InvalidValidationOptionsException
57 */
58 public function __construct(array $options = [])
59 {
60 // check for options given but not supported
61 if (($unsupportedOptions = array_diff_key($options, $this->supportedOptions)) !== []) {
62 throw new InvalidValidationOptionsException('Unsupported validation option(s) found: ' . implode(', ', array_keys($unsupportedOptions)), 1379981890);
63 }
64
65 // check for required options being set
66 array_walk(
67 $this->supportedOptions,
68 function ($supportedOptionData, $supportedOptionName, $options) {
69 if (isset($supportedOptionData[3]) && $supportedOptionData[3] === true && !array_key_exists($supportedOptionName, $options)) {
70 throw new InvalidValidationOptionsException('Required validation option not set: ' . $supportedOptionName, 1379981891);
71 }
72 },
73 $options
74 );
75
76 // merge with default values
77 $this->options = array_merge(
78 array_map(
79 function ($value) {
80 return $value[0];
81 },
82 $this->supportedOptions
83 ),
84 $options
85 );
86 }
87
88 /**
89 * Checks if the given value is valid according to the validator, and returns
90 * the error messages object which occurred.
91 *
92 * @param mixed $value The value that should be validated
93 * @return \TYPO3\CMS\Extbase\Error\Result
94 */
95 public function validate($value)
96 {
97 $this->result = new \TYPO3\CMS\Extbase\Error\Result();
98 if ($this->acceptsEmptyValues === false || $this->isEmpty($value) === false) {
99 $this->isValid($value);
100 }
101 return $this->result;
102 }
103
104 /**
105 * Check if $value is valid. If it is not valid, needs to add an error
106 * to result.
107 *
108 * @param mixed $value
109 */
110 abstract protected function isValid($value);
111
112 /**
113 * Creates a new validation error object and adds it to $this->results
114 *
115 * @param string $message The error message
116 * @param int $code The error code (a unix timestamp)
117 * @param array $arguments Arguments to be replaced in message
118 * @param string $title title of the error
119 */
120 protected function addError($message, $code, array $arguments = [], $title = '')
121 {
122 $this->result->addError(new \TYPO3\CMS\Extbase\Validation\Error($message, $code, $arguments, $title));
123 }
124
125 /**
126 * Returns the options of this validator
127 *
128 * @return array
129 */
130 public function getOptions()
131 {
132 return $this->options;
133 }
134
135 /**
136 * @param mixed $value
137 * @return bool TRUE if the given $value is NULL or an empty string ('')
138 */
139 final protected function isEmpty($value)
140 {
141 return $value === null || $value === '';
142 }
143
144 /**
145 * Wrap static call to LocalizationUtility to simplify unit testing
146 *
147 * @param string $translateKey
148 * @param string $extensionName
149 * @param array $arguments
150 *
151 * @return string|null
152 */
153 protected function translateErrorMessage($translateKey, $extensionName, $arguments = [])
154 {
155 return \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
156 $translateKey,
157 $extensionName,
158 $arguments
159 );
160 }
161 }