‪TYPO3CMS  ‪main
ValidatorClassNameResolver.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 
27 {
44  public static function ‪resolve(string $validatorIdentifier): string
45  {
46  // Trim leading slash if $validatorName is FQCN like \TYPO3\CMS\Extbase\Validation\Validator\FloatValidator
47  $validatorIdentifier = ltrim($validatorIdentifier, '\\');
48 
49  $validatorClassName = $validatorIdentifier;
50  if (strpbrk($validatorIdentifier, ':') !== false) {
51  // Found shorthand validator, either extbase or foreign extension
52  // NotEmpty or Acme.MyPck.Ext:MyValidator
53  [$vendorNamespace, $validatorBaseName] = explode(':', $validatorIdentifier);
54 
55  // todo: at this point ($validatorIdentifier !== $vendorNamespace) is always true as $validatorIdentifier
56  // todo: contains a colon ":" and $vendorNamespace doesn't.
57  if ($validatorIdentifier !== $vendorNamespace && $validatorBaseName !== '') {
58  // Shorthand custom
59  if (str_contains($vendorNamespace, '.')) {
60  $extensionNameParts = explode('.', $vendorNamespace);
61  $vendorNamespace = array_pop($extensionNameParts);
62  $vendorName = implode('\\', $extensionNameParts);
63  $validatorClassName = $vendorName . '\\' . $vendorNamespace . '\\Validation\\Validator\\' . $validatorBaseName;
64  }
65  } else {
66  // todo: the only way to reach this path is to use a validator identifier like "Integer:"
67  // todo: as we are using $validatorIdentifier here, this path always fails.
68  // Shorthand built in
69  $validatorClassName = 'TYPO3\\CMS\\Extbase\\Validation\\Validator\\' . ‪self::getValidatorType($validatorIdentifier);
70  }
71  } elseif (strpbrk($validatorIdentifier, '\\') === false) {
72  // Shorthand built in
73  $validatorClassName = 'TYPO3\\CMS\\Extbase\\Validation\\Validator\\' . ‪self::getValidatorType($validatorIdentifier);
74  }
75 
76  if (!str_ends_with($validatorClassName, 'Validator')) {
77  $validatorClassName .= 'Validator';
78  }
79 
80  if (!class_exists($validatorClassName)) {
81  throw new ‪NoSuchValidatorException('Validator class ' . $validatorClassName . ' does not exist', 1365799920);
82  }
83 
84  if (!is_subclass_of($validatorClassName, ValidatorInterface::class)) {
86  'Validator class ' . $validatorClassName . ' must implement ' . ValidatorInterface::class,
87  1365776838
88  );
89  }
90 
91  return $validatorClassName;
92  }
93 
100  private static function ‪getValidatorType(string $type): string
101  {
102  switch ($type) {
103  case 'int':
104  $type = 'Integer';
105  break;
106  case 'bool':
107  $type = 'Boolean';
108  break;
109  case 'double':
110  $type = 'Float';
111  break;
112  case 'numeric':
113  $type = 'Number';
114  break;
115  default:
116  $type = ucfirst($type);
117  }
118  return $type;
119  }
120 }
‪TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException
Definition: NoSuchValidatorException.php:25
‪TYPO3\CMS\Extbase\Validation
Definition: Error.php:16
‪TYPO3\CMS\Extbase\Validation\ValidatorClassNameResolver\resolve
‪static resolve(string $validatorIdentifier)
Definition: ValidatorClassNameResolver.php:44
‪TYPO3\CMS\Extbase\Validation\ValidatorClassNameResolver\getValidatorType
‪static string getValidatorType(string $type)
Definition: ValidatorClassNameResolver.php:100
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:26
‪TYPO3\CMS\Extbase\Validation\ValidatorClassNameResolver
Definition: ValidatorClassNameResolver.php:27