‪TYPO3CMS  10.4
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 
23 
28 {
46  public static function ‪resolve(string $validatorIdentifier): string
47  {
48  // Trim leading slash if $validatorName is FQCN like \TYPO3\CMS\Extbase\Validation\Validator\FloatValidator
49  $validatorIdentifier = ltrim($validatorIdentifier, '\\');
50 
51  $validatorClassName = $validatorIdentifier;
52  if (strpbrk($validatorIdentifier, ':') !== false) {
53  // Found shorthand validator, either extbase or foreign extension
54  // NotEmpty or Acme.MyPck.Ext:MyValidator
55  [$vendorNamespace, $validatorBaseName] = explode(':', $validatorIdentifier);
56 
57  // todo: at this point ($validatorIdentifier !== $vendorNamespace) is always true as $validatorIdentifier
58  // todo: contains a colon ":" and $vendorNamespace doesn't.
59  if ($validatorIdentifier !== $vendorNamespace && $validatorBaseName !== '') {
60  // Shorthand custom
61  if (strpos($vendorNamespace, '.') !== false) {
62  $extensionNameParts = explode('.', $vendorNamespace);
63  $vendorNamespace = array_pop($extensionNameParts);
64  $vendorName = implode('\\', $extensionNameParts);
65  $validatorClassName = $vendorName . '\\' . $vendorNamespace . '\\Validation\\Validator\\' . $validatorBaseName;
66  }
67  } else {
68  // todo: the only way to reach this path is to use a validator identifier like "Integer:"
69  // todo: as we are using $validatorIdentifier here, this path always fails.
70  // Shorthand built in
71  $validatorClassName = 'TYPO3\\CMS\\Extbase\\Validation\\Validator\\' . ‪self::getValidatorType($validatorIdentifier);
72  }
73  } elseif (strpbrk($validatorIdentifier, '\\') === false) {
74  // Shorthand built in
75  $validatorClassName = 'TYPO3\\CMS\\Extbase\\Validation\\Validator\\' . ‪self::getValidatorType($validatorIdentifier);
76  }
77 
78  if (!‪StringUtility::endsWith($validatorClassName, 'Validator')) {
79  $validatorClassName .= 'Validator';
80  }
81 
82  if (!class_exists($validatorClassName)) {
83  throw new ‪NoSuchValidatorException('Validator class ' . $validatorClassName . ' does not exist', 1365799920);
84  }
85 
86  if (!is_subclass_of($validatorClassName, ValidatorInterface::class)) {
88  'Validator class ' . $validatorClassName . ' must implement ' . ValidatorInterface::class,
89  1365776838
90  );
91  }
92 
93  return $validatorClassName;
94  }
95 
102  private static function ‪getValidatorType(string $type): string
103  {
104  switch ($type) {
105  case 'int':
106  $type = 'Integer';
107  break;
108  case 'bool':
109  $type = 'Boolean';
110  break;
111  case 'double':
112  $type = 'Float';
113  break;
114  case 'numeric':
115  $type = 'Number';
116  break;
117  default:
118  $type = ucfirst($type);
119  }
120  return $type;
121  }
122 }
‪TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException
Definition: NoSuchValidatorException.php:26
‪TYPO3\CMS\Core\Utility\StringUtility\endsWith
‪static bool endsWith($haystack, $needle)
Definition: StringUtility.php:61
‪TYPO3\CMS\Extbase\Validation\ValidatorClassNameResolver\resolve
‪static string resolve(string $validatorIdentifier)
Definition: ValidatorClassNameResolver.php:46
‪TYPO3\CMS\Extbase\Validation
Definition: Error.php:16
‪TYPO3\CMS\Extbase\Validation\ValidatorClassNameResolver\getValidatorType
‪static string getValidatorType(string $type)
Definition: ValidatorClassNameResolver.php:102
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:22
‪TYPO3\CMS\Extbase\Validation\ValidatorClassNameResolver
Definition: ValidatorClassNameResolver.php:28