Wsdl2PhpGenerator\Validator::validateUnique PHP Method

validateUnique() public static method

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.
    public static function validateUnique($name, $function, $suffix = null)
    {
        $i = 1;
        $newName = $name;
        while (!call_user_func($function, $newName)) {
            if (!$suffix) {
                $newName = $name . ($i + 1);
            } elseif ($i == 1) {
                $newName = $name . $suffix;
            } else {
                $newName = $name . $suffix . $i;
            }
            $i++;
        }
        return $newName;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * 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;
     }
 }