TYPO3 CMS  TYPO3_6-2
Argument.php
Go to the documentation of this file.
1 <?php
3 
18 
24 class Argument {
25 
30  protected $objectManager;
31 
36  protected $queryFactory;
37 
43 
51 
58  protected $propertyMapper;
59 
65 
69  protected $reflectionService;
70 
75 
81  protected $name = '';
82 
88  protected $shortName = NULL;
89 
95  protected $dataType = NULL;
96 
103 
109  protected $isRequired = FALSE;
110 
116  protected $value = NULL;
117 
123  protected $defaultValue = NULL;
124 
130  protected $validator = NULL;
131 
137  protected $validationResults = NULL;
138 
144  protected $uid = NULL;
145 
146  const ORIGIN_CLIENT = 0;
150 
158  protected $origin = 0;
159 
165 
174  public function __construct($name, $dataType) {
175  if (!is_string($name)) {
176  throw new \InvalidArgumentException('$name must be of type string, ' . gettype($name) . ' given.', 1187951688);
177  }
178  if (strlen($name) === 0) {
179  throw new \InvalidArgumentException('$name must be a non-empty string, ' . strlen($name) . ' characters given.', 1232551853);
180  }
181  $this->name = $name;
182  $this->dataType = $dataType;
183  }
184 
189  public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService) {
190  $this->reflectionService = $reflectionService;
191  // Check for classnames (which have at least one underscore or backslash)
192  $this->dataTypeClassSchema = strpbrk($this->dataType, '_\\') !== FALSE ? $this->reflectionService->getClassSchema($this->dataType) : NULL;
193  }
194 
199  public function injectTypeHandlingService(\TYPO3\CMS\Extbase\Service\TypeHandlingService $typeHandlingService) {
200  $this->typeHandlingService = $typeHandlingService;
201  $this->dataType = $this->typeHandlingService->normalizeType($this->dataType);
202  }
203 
210  public function getName() {
211  return $this->name;
212  }
213 
222  public function setShortName($shortName) {
223  if ($shortName !== NULL && (!is_string($shortName) || strlen($shortName) !== 1)) {
224  throw new \InvalidArgumentException('$shortName must be a single character or NULL', 1195824959);
225  }
226  $this->shortName = $shortName;
227  return $this;
228  }
229 
236  public function getShortName() {
237  return $this->shortName;
238  }
239 
247  public function setDataType($dataType) {
248  $this->dataType = $dataType;
249  $this->dataTypeClassSchema = $this->reflectionService->getClassSchema($dataType);
250  return $this;
251  }
252 
259  public function getDataType() {
260  return $this->dataType;
261  }
262 
270  public function setRequired($required) {
271  $this->isRequired = (boolean) $required;
272  return $this;
273  }
274 
281  public function isRequired() {
282  return $this->isRequired;
283  }
284 
292  public function setDefaultValue($defaultValue) {
293  $this->defaultValue = $defaultValue;
294  return $this;
295  }
296 
303  public function getDefaultValue() {
304  return $this->defaultValue;
305  }
306 
314  public function setValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator) {
315  $this->validator = $validator;
316  return $this;
317  }
318 
327  public function setNewValidatorConjunction(array $objectNames) {
328  if ($this->validator === NULL) {
329  $this->validator = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator');
330  }
331  foreach ($objectNames as $objectName) {
332  if (!class_exists($objectName)) {
333  $objectName = 'Tx_Extbase_Validation_Validator_' . $objectName;
334  }
335  $this->validator->addValidator($this->objectManager->get($objectName));
336  }
337  return $this;
338  }
339 
346  public function getValidator() {
347  return $this->validator;
348  }
349 
356  public function getOrigin() {
357  return $this->origin;
358  }
359 
367  public function setValue($rawValue) {
368  if ($this->configurationManager->isFeatureEnabled('rewrittenPropertyMapper')) {
369  if ($rawValue === NULL) {
370  $this->value = NULL;
371  return $this;
372  }
373  if (is_object($rawValue) && $rawValue instanceof $this->dataType) {
374  $this->value = $rawValue;
375  return $this;
376  }
377  try {
378  $this->value = $this->propertyMapper->convert($rawValue, $this->dataType, $this->propertyMappingConfiguration);
379  } catch (TargetNotFoundException $e) {
380  // for optional arguments no exeption is thrown.
381  if ($this->isRequired()) {
382  throw $e;
383  }
384  }
385  $this->validationResults = $this->propertyMapper->getMessages();
386  if ($this->validator !== NULL) {
387  // TODO: Validation API has also changed!!!
388  $validationMessages = $this->validator->validate($this->value);
389  $this->validationResults->merge($validationMessages);
390  }
391  return $this;
392  } else {
393  if ($rawValue === NULL || is_object($rawValue) && $rawValue instanceof $this->dataType) {
394  $this->value = $rawValue;
395  } else {
396  $this->value = $this->transformValue($rawValue);
397  }
398  return $this;
399  }
400  }
401 
414  protected function transformValue($value) {
415  if (!class_exists($this->dataType)) {
416  return $value;
417  }
418  $transformedValue = NULL;
419  if ($this->dataTypeClassSchema !== NULL) {
420  // The target object is an Entity or ValueObject.
421  if (is_numeric($value)) {
422  $this->origin = self::ORIGIN_PERSISTENCE;
423  $transformedValue = $this->findObjectByUid($value);
424  } elseif (is_array($value)) {
425  $this->origin = self::ORIGIN_PERSISTENCE_AND_MODIFIED;
426  $transformedValue = $this->deprecatedPropertyMapper->map(array_keys($value), $value, $this->dataType);
427  }
428  } else {
429  if (!is_array($value)) {
430  throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException('The value was a simple type, so we could not map it to an object. Maybe the @entity or @valueobject annotations are missing?', 1251730701);
431  }
432  $this->origin = self::ORIGIN_NEWLY_CREATED;
433  $transformedValue = $this->deprecatedPropertyMapper->map(array_keys($value), $value, $this->dataType);
434  }
435  if (!$transformedValue instanceof $this->dataType && ($transformedValue !== NULL || $this->isRequired())) {
436  throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException('The value must be of type "' . $this->dataType . '", but was of type "' . (is_object($transformedValue) ? get_class($transformedValue) : gettype($transformedValue)) . '".' . ($this->deprecatedPropertyMapper->getMappingResults()->hasErrors() ? '<p>' . implode('<br />', $this->deprecatedPropertyMapper->getMappingResults()->getErrors()) . '</p>' : ''), 1251730702);
437  }
438  return $transformedValue;
439  }
440 
447  protected function findObjectByUid($uid) {
448  $query = $this->queryFactory->create($this->dataType);
449  $query->getQuerySettings()->setRespectSysLanguage(FALSE);
450  $query->getQuerySettings()->setRespectStoragePage(FALSE);
451  return $query->matching($query->equals('uid', $uid))->execute()->getFirst();
452  }
453 
460  public function getValue() {
461  if ($this->value === NULL) {
462  return $this->defaultValue;
463  } else {
464  return $this->value;
465  }
466  }
467 
474  public function isValue() {
475  return $this->value !== NULL;
476  }
477 
486  }
487 
492  public function isValid() {
493  return !$this->validationResults->hasErrors();
494  }
495 
500  public function getValidationResults() {
502  }
503 
510  public function __toString() {
511  return (string) $this->value;
512  }
513 }
setNewValidatorConjunction(array $objectNames)
Definition: Argument.php:327
injectTypeHandlingService(\TYPO3\CMS\Extbase\Service\TypeHandlingService $typeHandlingService)
Definition: Argument.php:199
setValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator)
Definition: Argument.php:314
injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
Definition: Argument.php:189