‪TYPO3CMS  9.5
Argument.php
Go to the documentation of this file.
1 <?php
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 
20 
25 {
29  protected ‪$propertyMapper;
30 
35 
41  protected ‪$name = '';
42 
48  protected ‪$shortName;
49 
55  protected ‪$dataType;
56 
62  protected ‪$isRequired = false;
63 
69  protected ‪$value;
70 
76  protected ‪$defaultValue;
77 
83  protected ‪$validator;
84 
90  protected ‪$validationResults;
91 
95  private ‪$hasBeenValidated = false;
96 
100  public function ‪injectPropertyMapper(\‪TYPO3\CMS\‪Extbase\Property\PropertyMapper ‪$propertyMapper)
101  {
102  $this->propertyMapper = ‪$propertyMapper;
103  }
104 
109  {
110  $this->propertyMappingConfiguration = ‪$propertyMappingConfiguration;
111  }
112 
120  public function ‪__construct(‪$name, ‪$dataType)
121  {
122  if (!is_string(‪$name)) {
123  throw new \InvalidArgumentException('$name must be of type string, ' . gettype(‪$name) . ' given.', 1187951688);
124  }
125  if (‪$name === '') {
126  throw new \InvalidArgumentException('$name must be a non-empty string.', 1232551853);
127  }
128  $this->name = ‪$name;
130 
131  $this->validationResults = new ‪Result();
132  }
133 
139  public function ‪getName()
140  {
141  return ‪$this->name;
142  }
143 
151  public function ‪setShortName(‪$shortName)
152  {
153  if (‪$shortName !== null && (!is_string(‪$shortName) || strlen(‪$shortName) !== 1)) {
154  throw new \InvalidArgumentException('$shortName must be a single character or NULL', 1195824959);
155  }
156  $this->shortName = ‪$shortName;
157  return $this;
158  }
159 
165  public function ‪getShortName()
166  {
167  return ‪$this->shortName;
168  }
169 
175  public function ‪getDataType()
176  {
177  return ‪$this->dataType;
178  }
179 
186  public function ‪setRequired($required)
187  {
188  $this->‪isRequired = (bool)$required;
189  return $this;
190  }
191 
197  public function ‪isRequired()
198  {
199  return ‪$this->isRequired;
200  }
201 
209  {
210  $this->defaultValue = ‪$defaultValue;
211  return $this;
212  }
213 
219  public function ‪getDefaultValue()
220  {
221  return ‪$this->defaultValue;
222  }
223 
230  public function ‪setValidator(\‪TYPO3\CMS\‪Extbase\Validation\Validator\‪ValidatorInterface ‪$validator)
231  {
232  $this->validator = ‪$validator;
233  return $this;
234  }
235 
241  public function ‪getValidator()
242  {
244  }
245 
254  public function ‪setValue($rawValue)
255  {
256  if ($rawValue === null) {
257  $this->value = null;
258  return $this;
259  }
260  if (is_object($rawValue) && $rawValue instanceof $this->dataType) {
261  $this->value = $rawValue;
262  return $this;
263  }
264  try {
265  $this->value = $this->propertyMapper->convert($rawValue, $this->dataType, $this->propertyMappingConfiguration);
266  } catch (TargetNotFoundException $e) {
267  // for optional arguments no exeption is thrown.
268  if ($this->‪isRequired()) {
269  throw $e;
270  }
271  }
272  $this->validationResults->merge($this->propertyMapper->getMessages());
273  return $this;
274  }
275 
281  public function ‪getValue()
282  {
283  if ($this->value === null) {
284  return ‪$this->defaultValue;
285  }
286  return ‪$this->value;
287  }
288 
294  public function ‪getPropertyMappingConfiguration()
295  {
297  }
298 
302  public function ‪isValid(): bool
303  {
304  return !$this->‪validate()->‪hasErrors();
305  }
306 
311  public function ‪getValidationResults()
312  {
313  trigger_error(
314  'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
315  E_USER_DEPRECATED
316  );
317 
318  return $this->‪validate();
319  }
320 
326  public function ‪__toString()
327  {
328  return (string)‪$this->value;
329  }
330 
334  public function ‪validate(): \‪TYPO3\CMS\‪Extbase\Error\Result
335  {
336  if ($this->hasBeenValidated) {
338  }
339 
340  if ($this->validator !== null) {
341  $validationMessages = $this->validator->validate($this->value);
342  $this->validationResults->‪merge($validationMessages);
343  }
344 
345  $this->hasBeenValidated = true;
347  }
348 }
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$dataType
‪string $dataType
Definition: Argument.php:50
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$isRequired
‪bool $isRequired
Definition: Argument.php:56
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setValue
‪TYPO3 CMS Extbase Mvc Controller Argument setValue($rawValue)
Definition: Argument.php:243
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setShortName
‪TYPO3 CMS Extbase Mvc Controller Argument setShortName($shortName)
Definition: Argument.php:140
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setRequired
‪TYPO3 CMS Extbase Mvc Controller Argument setRequired($required)
Definition: Argument.php:175
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$name
‪string $name
Definition: Argument.php:38
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\__construct
‪__construct($name, $dataType)
Definition: Argument.php:109
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getValidator
‪TYPO3 CMS Extbase Validation Validator ValidatorInterface getValidator()
Definition: Argument.php:230
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getValidationResults
‪TYPO3 CMS Extbase Error Result getValidationResults()
Definition: Argument.php:300
‪TYPO3
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\validate
‪TYPO3 CMS Extbase Error Result validate()
Definition: Argument.php:323
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$hasBeenValidated
‪bool $hasBeenValidated
Definition: Argument.php:84
‪TYPO3\CMS\Extbase\Error\Result\hasErrors
‪bool hasErrors()
Definition: Result.php:309
‪TYPO3\CMS\Extbase\Mvc\Controller
Definition: AbstractController.php:2
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:20
‪TYPO3\CMS\Extbase\Error\Result\merge
‪merge(Result $otherResult)
Definition: Result.php:402
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setDefaultValue
‪TYPO3 CMS Extbase Mvc Controller Argument setDefaultValue($defaultValue)
Definition: Argument.php:197
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getPropertyMappingConfiguration
‪TYPO3 CMS Extbase Mvc Controller MvcPropertyMappingConfiguration getPropertyMappingConfiguration()
Definition: Argument.php:283
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\isRequired
‪bool isRequired()
Definition: Argument.php:186
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility
Definition: TypeHandlingUtility.php:19
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$propertyMappingConfiguration
‪MvcPropertyMappingConfiguration $propertyMappingConfiguration
Definition: Argument.php:32
‪TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration
Definition: MvcPropertyMappingConfiguration.php:22
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getName
‪string getName()
Definition: Argument.php:128
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\normalizeType
‪static string normalizeType($type)
Definition: TypeHandlingUtility.php:70
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getDefaultValue
‪mixed getDefaultValue()
Definition: Argument.php:208
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\injectPropertyMappingConfiguration
‪injectPropertyMappingConfiguration(MvcPropertyMappingConfiguration $propertyMappingConfiguration)
Definition: Argument.php:97
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getShortName
‪string getShortName()
Definition: Argument.php:154
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$validator
‪TYPO3 CMS Extbase Validation Validator ValidatorInterface $validator
Definition: Argument.php:74
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$value
‪mixed $value
Definition: Argument.php:62
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$shortName
‪string $shortName
Definition: Argument.php:44
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getDataType
‪string getDataType()
Definition: Argument.php:164
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setValidator
‪TYPO3 CMS Extbase Mvc Controller Argument setValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator)
Definition: Argument.php:219
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$propertyMapper
‪TYPO3 CMS Extbase Property PropertyMapper $propertyMapper
Definition: Argument.php:28
‪TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException
Definition: TargetNotFoundException.php:21
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$validationResults
‪TYPO3 CMS Extbase Error Result $validationResults
Definition: Argument.php:80
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\__toString
‪string __toString()
Definition: Argument.php:315
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:21
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument
Definition: Argument.php:25
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$defaultValue
‪mixed $defaultValue
Definition: Argument.php:68
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\injectPropertyMapper
‪injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
Definition: Argument.php:89
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\isValid
‪bool isValid()
Definition: Argument.php:291
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getValue
‪mixed getValue()
Definition: Argument.php:270