Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NameValidationUtil.php
Go to the documentation of this file.
1 <?php
8 
10 
12 {
13  const PHP_CLASS_REGEX_PATTERN = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/';
14 
24  public static function validateName($name, $type)
25  {
26  $startingPos = 0;
27  $illegalCharArray = [];
28  $nameToEvaluate = $name;
29 
30  while ($startingPos < strlen($nameToEvaluate)) {
31  $startingPos++;
32  $partialName = substr($nameToEvaluate, 0, $startingPos);
33  $valid = boolval(preg_match(self::PHP_CLASS_REGEX_PATTERN, $partialName));
34 
35  if (!$valid) {
36  $illegalChar = str_split($partialName)[$startingPos -1];
37  $illegalCharArray[] = $illegalChar;
38  $nameToEvaluate = str_replace($illegalChar, "", $nameToEvaluate);
39  $startingPos--;
40  }
41  }
42 
43  if (!empty($illegalCharArray)) {
44  $errorMessage = "{$type} name \"${name}\" contains illegal characters, please fix and re-run.";
45 
46  foreach ($illegalCharArray as $diffChar) {
47  $errorMessage .= "\nTest names cannot contain '{$diffChar}'";
48  }
49 
50  throw new XmlException($errorMessage);
51  }
52  }
53 }
$type
Definition: item.phtml:13
if(!isset($_GET['name'])) $name
Definition: log.php:14