‪TYPO3CMS  10.4
Argument.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 
28 {
32  protected ‪$propertyMapper;
33 
38 
44  protected ‪$name = '';
45 
51  protected ‪$shortName;
52 
58  protected ‪$dataType;
59 
65  protected ‪$isRequired = false;
66 
72  protected ‪$value;
73 
79  protected ‪$defaultValue;
80 
86  protected ‪$validator;
87 
93  protected ‪$validationResults;
94 
98  private ‪$hasBeenValidated = false;
99 
104  {
105  $this->propertyMapper = ‪$propertyMapper;
106  }
107 
112  {
113  $this->propertyMappingConfiguration = ‪$propertyMappingConfiguration;
114  }
115 
123  public function ‪__construct(‪$name, ‪$dataType)
124  {
125  if (!is_string(‪$name)) {
126  throw new \InvalidArgumentException('$name must be of type string, ' . gettype(‪$name) . ' given.', 1187951688);
127  }
128  if (‪$name === '') {
129  throw new \InvalidArgumentException('$name must be a non-empty string.', 1232551853);
130  }
131  $this->name = ‪$name;
133 
134  $this->validationResults = new ‪Result();
135  }
136 
142  public function ‪getName()
143  {
144  return ‪$this->name;
145  }
146 
154  public function ‪setShortName(‪$shortName)
155  {
156  if (‪$shortName !== null && (!is_string(‪$shortName) || strlen(‪$shortName) !== 1)) {
157  throw new \InvalidArgumentException('$shortName must be a single character or NULL', 1195824959);
158  }
159  $this->shortName = ‪$shortName;
160  return $this;
161  }
162 
168  public function ‪getShortName()
169  {
170  return ‪$this->shortName;
171  }
172 
178  public function ‪getDataType()
179  {
180  return ‪$this->dataType;
181  }
182 
189  public function ‪setRequired($required)
190  {
191  $this->‪isRequired = (bool)$required;
192  return $this;
193  }
194 
200  public function ‪isRequired()
201  {
202  return ‪$this->isRequired;
203  }
204 
212  {
213  $this->defaultValue = ‪$defaultValue;
214  return $this;
215  }
216 
222  public function ‪getDefaultValue()
223  {
224  return ‪$this->defaultValue;
225  }
226 
234  {
235  $this->validator = ‪$validator;
236  return $this;
237  }
238 
244  public function ‪getValidator()
245  {
247  }
248 
257  public function ‪setValue($rawValue)
258  {
259  if ($rawValue === null) {
260  $this->value = null;
261  return $this;
262  }
263  if (is_object($rawValue) && $rawValue instanceof $this->dataType) {
264  $this->value = $rawValue;
265  return $this;
266  }
267  try {
268  $this->value = $this->propertyMapper->convert($rawValue, $this->dataType, $this->propertyMappingConfiguration);
269  } catch (TargetNotFoundException $e) {
270  // for optional arguments no exception is thrown.
271  if ($this->‪isRequired()) {
272  throw $e;
273  }
274  }
275  $this->validationResults->merge($this->propertyMapper->getMessages());
276  return $this;
277  }
278 
284  public function ‪getValue()
285  {
286  if ($this->value === null) {
287  return ‪$this->defaultValue;
288  }
289  return ‪$this->value;
290  }
291 
297  public function ‪getPropertyMappingConfiguration()
298  {
300  }
301 
305  public function ‪isValid(): bool
306  {
307  return !$this->‪validate()->‪hasErrors();
308  }
309 
315  public function ‪__toString()
316  {
317  return (string)‪$this->value;
318  }
319 
323  public function ‪validate(): Result
324  {
325  if ($this->hasBeenValidated) {
327  }
328 
329  if ($this->validator !== null) {
330  $validationMessages = $this->validator->validate($this->value);
331  $this->validationResults->‪merge($validationMessages);
332  }
333 
334  $this->hasBeenValidated = true;
336  }
337 }
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$dataType
‪string $dataType
Definition: Argument.php:53
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$isRequired
‪bool $isRequired
Definition: Argument.php:59
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setValue
‪TYPO3 CMS Extbase Mvc Controller Argument setValue($rawValue)
Definition: Argument.php:246
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setShortName
‪TYPO3 CMS Extbase Mvc Controller Argument setShortName($shortName)
Definition: Argument.php:143
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setRequired
‪TYPO3 CMS Extbase Mvc Controller Argument setRequired($required)
Definition: Argument.php:178
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$name
‪string $name
Definition: Argument.php:41
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\__construct
‪__construct($name, $dataType)
Definition: Argument.php:112
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getValidator
‪TYPO3 CMS Extbase Validation Validator ValidatorInterface getValidator()
Definition: Argument.php:233
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\normalizeType
‪static string normalizeType(string $type)
Definition: TypeHandlingUtility.php:80
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\validate
‪TYPO3 CMS Extbase Error Result validate()
Definition: Argument.php:312
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$hasBeenValidated
‪bool $hasBeenValidated
Definition: Argument.php:87
‪TYPO3\CMS\Extbase\Error\Result\hasErrors
‪bool hasErrors()
Definition: Result.php:295
‪TYPO3\CMS\Extbase\Mvc\Controller
Definition: AbstractController.php:16
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Extbase\Error\Result\merge
‪merge(Result $otherResult)
Definition: Result.php:445
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\injectPropertyMapper
‪injectPropertyMapper(PropertyMapper $propertyMapper)
Definition: Argument.php:92
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setDefaultValue
‪TYPO3 CMS Extbase Mvc Controller Argument setDefaultValue($defaultValue)
Definition: Argument.php:200
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getPropertyMappingConfiguration
‪TYPO3 CMS Extbase Mvc Controller MvcPropertyMappingConfiguration getPropertyMappingConfiguration()
Definition: Argument.php:286
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\isRequired
‪bool isRequired()
Definition: Argument.php:189
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility
Definition: TypeHandlingUtility.php:29
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$propertyMappingConfiguration
‪MvcPropertyMappingConfiguration $propertyMappingConfiguration
Definition: Argument.php:35
‪TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration
Definition: MvcPropertyMappingConfiguration.php:26
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setValidator
‪TYPO3 CMS Extbase Mvc Controller Argument setValidator(ValidatorInterface $validator)
Definition: Argument.php:222
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getName
‪string getName()
Definition: Argument.php:131
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getDefaultValue
‪mixed getDefaultValue()
Definition: Argument.php:211
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\injectPropertyMappingConfiguration
‪injectPropertyMappingConfiguration(MvcPropertyMappingConfiguration $propertyMappingConfiguration)
Definition: Argument.php:100
‪TYPO3\CMS\Extbase\Property\PropertyMapper
Definition: PropertyMapper.php:37
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getShortName
‪string getShortName()
Definition: Argument.php:157
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$validator
‪TYPO3 CMS Extbase Validation Validator ValidatorInterface $validator
Definition: Argument.php:77
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$value
‪mixed null $value
Definition: Argument.php:65
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$shortName
‪string $shortName
Definition: Argument.php:47
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getDataType
‪string getDataType()
Definition: Argument.php:167
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$propertyMapper
‪TYPO3 CMS Extbase Property PropertyMapper $propertyMapper
Definition: Argument.php:31
‪TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException
Definition: TargetNotFoundException.php:26
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$validationResults
‪TYPO3 CMS Extbase Error Result $validationResults
Definition: Argument.php:83
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\__toString
‪string __toString()
Definition: Argument.php:304
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:22
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument
Definition: Argument.php:28
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\$defaultValue
‪mixed $defaultValue
Definition: Argument.php:71
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\isValid
‪bool isValid()
Definition: Argument.php:294
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\getValue
‪mixed getValue()
Definition: Argument.php:273