Zend\Code\Generator\ClassGenerator::addProperty PHP Method

addProperty() public method

Add Property from scalars
public addProperty ( string $name, string | array $defaultValue = null, integer $flags = PropertyGenerator::FLAG_PUBLIC ) : self
$name string
$defaultValue string | array
$flags integer
return self
    public function addProperty($name, $defaultValue = null, $flags = PropertyGenerator::FLAG_PUBLIC)
    {
        if (!is_string($name)) {
            throw new Exception\InvalidArgumentException(sprintf('%s::%s expects string for name', get_class($this), __FUNCTION__));
        }
        // backwards compatibility
        // @todo remove this on next major version
        if ($flags === PropertyGenerator::FLAG_CONSTANT) {
            return $this->addConstant($name, $defaultValue);
        }
        return $this->addPropertyFromGenerator(new PropertyGenerator($name, $defaultValue, $flags));
    }

Usage Example

Example #1
0
 private function writeApiFile()
 {
     $file = $this->path . DIRECTORY_SEPARATOR . 'Api.php';
     if (file_exists($file) && class_exists($this->namespace . '\\Api')) {
         $class = new ClassReflection($this->namespace . '\\Api');
         $apiClass = ClassGenerator::fromReflection($class);
     } else {
         $apiClass = new ClassGenerator('Api', $this->namespace, null, 'OpenStack\\Common\\Api\\AbstractApi');
     }
     if (!$apiClass->hasMethod('__construct')) {
         $apiClass->addProperty('params', null, PropertyGenerator::FLAG_PRIVATE);
         $apiClass->addMethod('__construct', [], MethodGenerator::FLAG_PUBLIC, '$this->params = new Params;');
     }
     foreach ($this->operations as $operation) {
         $path = str_replace(['{', '}'], '', $operation['path']);
         $name = strtolower($operation['method']) . ucfirst(substr($path, strrpos($path, DIRECTORY_SEPARATOR) + 1));
         if ($apiClass->hasMethod($name)) {
             continue;
         }
         foreach ($operation['params'] as $kName => $arr) {
             $operation['params'][$kName] = '$this->params->' . $kName . ucfirst($arr['location']) . '()';
         }
         $body = sprintf("return %s;", $this->arrayEncoder->encode($operation, ['array.align' => true]));
         $body = str_replace("'\$", '$', $body);
         $body = str_replace("()'", '()', $body);
         $docblock = new DocBlockGenerator(sprintf("Returns information about %s %s HTTP operation", $operation['method'], $operation['path']), null, [new ReturnTag(['array'])]);
         $apiClass->addMethod($name, [], MethodGenerator::FLAG_PUBLIC, $body, $docblock);
     }
     $output = sprintf("<?php\n\n%s", $apiClass->generate());
     file_put_contents($file, $output);
 }
All Usage Examples Of Zend\Code\Generator\ClassGenerator::addProperty