Wsdl2PhpGenerator\Validator::validateClass PHP Method

validateClass() public static method

Validates a class name against PHP naming conventions and already defined classes.
public static validateClass ( string $name, string $namespace = null ) : string
$name string the name of the class to test
$namespace string the name of the namespace
return string The validated version of the submitted class name
    public static function validateClass($name, $namespace = null)
    {
        $name = self::validateNamingConvention($name);
        $prefix = !empty($namespace) ? $namespace . '\\' : '';
        $name = self::validateUnique($name, function ($name) use($prefix) {
            // Use reflection to get access to private isKeyword method.
            // @todo Remove this when we stop supporting PHP 5.3.
            $isKeywordMethod = new \ReflectionMethod(__CLASS__, 'isKeyword');
            $isKeywordMethod->setAccessible(true);
            $isKeyword = $isKeywordMethod->invoke(null, $name);
            return !$isKeyword && !interface_exists($prefix . $name) && !class_exists($prefix . $name);
        }, self::NAME_SUFFIX);
        return $name;
    }

Usage Example

   /**
    * Generates the class if not already generated
    */
   public function generateClass()
   {
       // Add prefix and suffix
       $name = $this->config->getPrefix() . $this->identifier . $this->config->getSuffix();
       // Generate a valid classname
       $name = Validator::validateClass($name);
       // uppercase the name
       $name = ucfirst($name);
       // Create the class object
       $comment = new PhpDocComment($this->description);
       $this->class = new PhpClass($name, $this->config->getClassExists(), '\\SoapClient', $comment);
       // Create the constructor
       $comment = new PhpDocComment();
       $comment->addParam(PhpDocElementFactory::getParam('array', 'options', 'A array of config values'));
       $comment->addParam(PhpDocElementFactory::getParam('string', 'wsdl', 'The wsdl file to use'));
       $comment->setAccess(PhpDocElementFactory::getPublicAccess());
       $source = '  foreach (self::$classmap as $key => $value) {
   if (!isset($options[\'classmap\'][$key])) {
     $options[\'classmap\'][$key] = $value;
   }
 }
 ' . $this->generateServiceOptions() . '
 parent::__construct($wsdl, $options);' . PHP_EOL;
       $function = new PhpFunction('public', '__construct', 'array $options = array(), $wsdl = \'' . $this->config->getInputFile() . '\'', $source, $comment);
       // Add the constructor
       $this->class->addFunction($function);
       // Generate the classmap
       $name = 'classmap';
       $comment = new PhpDocComment();
       $comment->setAccess(PhpDocElementFactory::getPrivateAccess());
       $comment->setVar(PhpDocElementFactory::getVar('array', $name, 'The defined classes'));
       $init = 'array(' . PHP_EOL;
       foreach ($this->types as $type) {
           if ($type instanceof ComplexType) {
               $init .= "  '" . $type->getIdentifier() . "' => '" . $this->config->getNamespaceName() . "\\" . $type->getPhpIdentifier() . "'," . PHP_EOL;
           }
       }
       $init = substr($init, 0, strrpos($init, ','));
       $init .= ')';
       $var = new PhpVariable('private static', $name, $init, $comment);
       // Add the classmap variable
       $this->class->addVariable($var);
       // Add all methods
       foreach ($this->operations as $operation) {
           $name = Validator::validateOperation($operation->getName());
           $comment = new PhpDocComment($operation->getDescription());
           $comment->setAccess(PhpDocElementFactory::getPublicAccess());
           $comment->setReturn(PhpDocElementFactory::getReturn($operation->getReturns(), ''));
           foreach ($operation->getParams() as $param => $hint) {
               $arr = $operation->getPhpDocParams($param, $this->types);
               $comment->addParam(PhpDocElementFactory::getParam($arr['type'], $arr['name'], $arr['desc']));
           }
           $source = '  return $this->__soapCall(\'' . $operation->getName() . '\', array(' . $operation->getParamStringNoTypeHints() . '));' . PHP_EOL;
           $paramStr = $operation->getParamString($this->types);
           $function = new PhpFunction('public', $name, $paramStr, $source, $comment);
           if ($this->class->functionExists($function->getIdentifier()) == false) {
               $this->class->addFunction($function);
           }
       }
   }
All Usage Examples Of Wsdl2PhpGenerator\Validator::validateClass