Wsdl2PhpGenerator\Validator::validateTypeHint PHP Метод

validateTypeHint() публичный статический Метод

Validates a type to be used as a method parameter type hint.
public static validateTypeHint ( string $typeName ) : null | string
$typeName string The name of the type to test.
Результат null | string Returns a valid type hint for the type or null if there is no valid type hint.
    public static function validateTypeHint($typeName)
    {
        $typeHint = null;
        // We currently only support type hints for arrays and DateTimes.
        // Going forward we could support it for generated types. The challenge here are enums as they are actually
        // strings and not class instances and we have no way of determining whether the type is an enum at this point.
        if (substr($typeName, -2) == "[]") {
            $typeHint = 'array';
        } elseif ($typeName == '\\DateTime') {
            $typeHint = $typeName;
        }
        return $typeHint;
    }

Usage Example

Пример #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");
     }
     // Determine parent class
     $classBaseType = null;
     // If we have a base type which is different than the current class then extend that.
     // It is actually possible to have different classes with the same name as PHP SoapClient has a poor
     // understanding of namespaces. Two types with the same name but in different namespaces will have the same
     // identifier.
     if ($this->baseType !== null && $this->baseType !== $this) {
         $classBaseType = $this->baseType->getPhpIdentifier();
     }
     $class = new PhpClass($this->phpIdentifier, false, $classBaseType, null, false, $this->abstract);
     $constructorComment = new PhpDocComment();
     $constructorSource = '';
     $constructorParameters = array();
     $accessors = array();
     // Add base type members to constructor parameter list first and call base class constructor
     $parentMembers = $this->getBaseTypeMembers($this);
     if (!empty($parentMembers)) {
         foreach ($parentMembers as $member) {
             $type = Validator::validateType($member->getType());
             $name = Validator::validateAttribute($member->getName());
             if (!$member->getNullable()) {
                 $constructorComment->addParam(PhpDocElementFactory::getParam($type, $name, ''));
                 $constructorParameters[$name] = Validator::validateTypeHint($type);
             }
         }
         $constructorSource .= '  parent::__construct(' . $this->buildParametersString($constructorParameters, false) . ');' . PHP_EOL;
     }
     // Add member variables
     foreach ($this->members as $member) {
         $type = Validator::validateType($member->getType());
         $name = Validator::validateAttribute($member->getName());
         $typeHint = Validator::validateTypeHint($type);
         $comment = new PhpDocComment();
         $comment->setVar(PhpDocElementFactory::getVar($type, $name, ''));
         $var = new PhpVariable('protected', $name, 'null', $comment);
         $class->addVariable($var);
         if (!$member->getNullable()) {
             if ($type == '\\DateTime') {
                 if ($this->config->get('constructorParamsDefaultToNull')) {
                     $constructorSource .= '  $this->' . $name . ' = $' . $name . ' ? $' . $name . '->format(\\DateTime::ATOM) : null;' . PHP_EOL;
                 } else {
                     $constructorSource .= '  $this->' . $name . ' = $' . $name . '->format(\\DateTime::ATOM);' . PHP_EOL;
                 }
             } else {
                 $constructorSource .= '  $this->' . $name . ' = $' . $name . ';' . PHP_EOL;
             }
             $constructorComment->addParam(PhpDocElementFactory::getParam($type, $name, ''));
             $constructorParameters[$name] = $typeHint;
         }
         $getterComment = new PhpDocComment();
         $getterComment->setReturn(PhpDocElementFactory::getReturn($type, ''));
         $getterCode = '';
         if ($type == '\\DateTime') {
             $getterCode = '  if ($this->' . $name . ' == null) {' . PHP_EOL . '    return null;' . PHP_EOL . '  } else {' . PHP_EOL . '    try {' . PHP_EOL . '      return new \\DateTime($this->' . $name . ');' . PHP_EOL . '    } catch (\\Exception $e) {' . PHP_EOL . '      return false;' . PHP_EOL . '    }' . PHP_EOL . '  }' . PHP_EOL;
         } else {
             $getterCode = '  return $this->' . $name . ';' . PHP_EOL;
         }
         $getter = new PhpFunction('public', 'get' . ucfirst($name), '', $getterCode, $getterComment);
         $accessors[] = $getter;
         $setterComment = new PhpDocComment();
         $setterComment->addParam(PhpDocElementFactory::getParam($type, $name, ''));
         $setterComment->setReturn(PhpDocElementFactory::getReturn($this->phpNamespacedIdentifier, ''));
         $setterCode = '';
         if ($type == '\\DateTime') {
             if ($member->getNullable()) {
                 $setterCode = '  if ($' . $name . ' == null) {' . PHP_EOL . '   $this->' . $name . ' = null;' . PHP_EOL . '  } else {' . PHP_EOL . '    $this->' . $name . ' = $' . $name . '->format(\\DateTime::ATOM);' . PHP_EOL . '  }' . PHP_EOL;
             } else {
                 $setterCode = '  $this->' . $name . ' = $' . $name . '->format(\\DateTime::ATOM);' . PHP_EOL;
             }
         } else {
             $setterCode = '  $this->' . $name . ' = $' . $name . ';' . PHP_EOL;
         }
         $setterCode .= '  return $this;' . PHP_EOL;
         $setter = new PhpFunction('public', 'set' . ucfirst($name), $this->buildParametersString(array($name => $typeHint), true, $member->getNullable() && !empty($typeHint)), $setterCode, $setterComment);
         $accessors[] = $setter;
     }
     $constructor = new PhpFunction('public', '__construct', $this->buildParametersString($constructorParameters, true, $this->config->get('constructorParamsDefaultToNull')), $constructorSource, $constructorComment);
     $class->addFunction($constructor);
     foreach ($accessors as $accessor) {
         $class->addFunction($accessor);
     }
     $this->class = $class;
 }
All Usage Examples Of Wsdl2PhpGenerator\Validator::validateTypeHint