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