TYPO3  7.6
Argument.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Controller;
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\Property\Exception\TargetNotFoundException;
19 
25 class Argument
26 {
30  protected $propertyMapper;
31 
36 
42  protected $name = '';
43 
49  protected $shortName = null;
50 
56  protected $dataType = null;
57 
63  protected $isRequired = false;
64 
70  protected $value = null;
71 
77  protected $defaultValue = null;
78 
84  protected $validator = null;
85 
91  protected $validationResults = null;
92 
96  public function injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
97  {
98  $this->propertyMapper = $propertyMapper;
99  }
100 
105  {
106  $this->propertyMappingConfiguration = $propertyMappingConfiguration;
107  }
108 
117  public function __construct($name, $dataType)
118  {
119  if (!is_string($name)) {
120  throw new \InvalidArgumentException('$name must be of type string, ' . gettype($name) . ' given.', 1187951688);
121  }
122  if ($name === '') {
123  throw new \InvalidArgumentException('$name must be a non-empty string.', 1232551853);
124  }
125  $this->name = $name;
127  }
128 
135  public function getName()
136  {
137  return $this->name;
138  }
139 
148  public function setShortName($shortName)
149  {
150  if ($shortName !== null && (!is_string($shortName) || strlen($shortName) !== 1)) {
151  throw new \InvalidArgumentException('$shortName must be a single character or NULL', 1195824959);
152  }
153  $this->shortName = $shortName;
154  return $this;
155  }
156 
163  public function getShortName()
164  {
165  return $this->shortName;
166  }
167 
174  public function getDataType()
175  {
176  return $this->dataType;
177  }
178 
186  public function setRequired($required)
187  {
188  $this->isRequired = (bool)$required;
189  return $this;
190  }
191 
198  public function isRequired()
199  {
200  return $this->isRequired;
201  }
202 
211  {
212  $this->defaultValue = $defaultValue;
213  return $this;
214  }
215 
222  public function getDefaultValue()
223  {
224  return $this->defaultValue;
225  }
226 
234  public function setValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator)
235  {
236  $this->validator = $validator;
237  return $this;
238  }
239 
246  public function getValidator()
247  {
248  return $this->validator;
249  }
250 
259  public function setValue($rawValue)
260  {
261  if ($rawValue === null) {
262  $this->value = null;
263  return $this;
264  }
265  if (is_object($rawValue) && $rawValue instanceof $this->dataType) {
266  $this->value = $rawValue;
267  return $this;
268  }
269  try {
270  $this->value = $this->propertyMapper->convert($rawValue, $this->dataType, $this->propertyMappingConfiguration);
271  } catch (TargetNotFoundException $e) {
272  // for optional arguments no exeption is thrown.
273  if ($this->isRequired()) {
274  throw $e;
275  }
276  }
277  $this->validationResults = $this->propertyMapper->getMessages();
278  if ($this->validator !== null) {
279  // @todo Validation API has also changed!!!
280  $validationMessages = $this->validator->validate($this->value);
281  $this->validationResults->merge($validationMessages);
282  }
283  return $this;
284  }
285 
292  public function getValue()
293  {
294  if ($this->value === null) {
295  return $this->defaultValue;
296  } else {
297  return $this->value;
298  }
299  }
300 
308  {
310  }
311 
316  public function isValid()
317  {
318  return !$this->validationResults->hasErrors();
319  }
320 
325  public function getValidationResults()
326  {
328  }
329 
336  public function __toString()
337  {
338  return (string)$this->value;
339  }
340 }