If a name is not unique then append a suffix and numbering.
public static validateUnique ( $name, callable $function, string $suffix = null ) : string | ||
$name | The name to test. | |
$function | callable | A callback which should return true if the element is unique. Otherwise false. |
$suffix | string | A suffix to append between the name and numbering. |
return | string | A unique name. |
/** * Implements the loading of the class object * * @throws Exception if the class is already generated(not null) */ protected function generateClass() { if ($this->class != null) { throw new Exception("The class has already been generated"); } $this->class = new PhpClass($this->phpIdentifier, false); $first = true; $names = array(); foreach ($this->values as $value) { $name = Validator::validateConstant($value); $name = Validator::validateUnique($name, function ($name) use($names) { return !in_array($name, $names); }); if ($first) { $this->class->addConstant($name, '__default'); $first = false; } $this->class->addConstant($value, $name); $names[] = $name; } }