本文整理匯總了PHP中Zend\Code\Generator\PropertyGenerator::setFlags方法的典型用法代碼示例。如果您正苦於以下問題:PHP PropertyGenerator::setFlags方法的具體用法?PHP PropertyGenerator::setFlags怎麽用?PHP PropertyGenerator::setFlags使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Zend\Code\Generator\PropertyGenerator
的用法示例。
在下文中一共展示了PropertyGenerator::setFlags方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: addProperties
/**
* Append Properties, Getter and Setter to Model class
*
* @param Generator\ClassGenerator $class
* @param array $properties
* @param array $hydratorStrategy
*/
protected function addProperties(Generator\ClassGenerator &$class, array $properties)
{
foreach ($properties as $metadata) {
$propertyInfos = $this->getNormalizedFilterOrProperty($metadata);
$name = $propertyInfos['name'];
$required = $propertyInfos['required'];
$lazyLoading = $propertyInfos['lazyLoading'];
$dataType = $propertyInfos['dataType'];
$defaultValue = $propertyInfos['defaultValue'];
$description = $propertyInfos['description'];
$flags = Generator\PropertyGenerator::FLAG_PROTECTED;
foreach ($propertyInfos['uses'] as $use) {
$class->addUse($use);
}
$property = new Generator\PropertyGenerator($name);
$property->setFlags($flags);
if (!empty($defaultValue) or 'bool' === $dataType) {
$property->setDefaultValue($defaultValue, $dataType);
}
$property->setDocBlock(new Generator\DocBlockGenerator($description));
$class->addPropertyFromGenerator($property);
// Add Setter method
$setterParam = new Generator\ParameterGenerator($name, true === $lazyLoading ? null : $dataType);
if (!$required) {
$setterParam->setDefaultValue(null);
}
$setterBody = '';
if (true === $lazyLoading) {
if ($dataType == "ResultSet") {
$class->addUse('Mailjet\\Api\\ResultSet\\Exception');
$setterBody .= sprintf('if (! ($%s instanceof \\Closure || $%s instanceof %s)) {' . PHP_EOL . ' throw new Exception\\InvalidArgumentException("%s must be an instance of \'%s\' or \\Closure");' . PHP_EOL . '}' . PHP_EOL . PHP_EOL, $name, $name, $dataType, $name, $dataType);
} else {
$setterBody .= sprintf('if (! ($%s instanceof \\Closure || $%s instanceof %s)) {' . PHP_EOL . ' throw new Exception\\InvalidArgumentException("$%s must be an instance of \'%s\' or \\Closure");' . PHP_EOL . '}' . PHP_EOL . PHP_EOL, $name, $name, $dataType, $name, $dataType);
}
}
$setterBody .= sprintf('$this->%s = $%s;' . PHP_EOL . 'return $this;' . PHP_EOL, $name, $name);
$class->addMethod('set' . ucfirst($name), array($setterParam), Generator\MethodGenerator::FLAG_PUBLIC, $setterBody, new Generator\DocBlockGenerator("Sets the " . $property->getDocBlock()->getShortDescription(), '', array(new ParamTag(array('name' => $name, 'datatype' => $dataType)), new ReturnTag(array('datatype' => $class->getName())))));
// Add Getter method
$getterBody = '';
if (true === $lazyLoading) {
$getterBody .= sprintf('if ($this->%s instanceof \\Closure) {' . PHP_EOL . ' $this->%s = call_user_func($this->%s);' . PHP_EOL . '}' . PHP_EOL . 'if (! $this->%s instanceof %s) {' . PHP_EOL . ' $this->%s = new %s();' . PHP_EOL . '}' . PHP_EOL . PHP_EOL, $name, $name, $name, $name, $dataType, $name, $dataType);
}
$getterBody .= sprintf('return $this->%s;', $name);
$class->addMethod('get' . ucfirst($name), array(), Generator\MethodGenerator::FLAG_PUBLIC, $getterBody, new Generator\DocBlockGenerator('Gets the ' . $property->getDocBlock()->getShortDescription(), '', array(new ReturnTag(array('datatype' => $dataType)))));
// If DataType Resulset => add/remove methods
if ('ResultSet' === $dataType) {
$class->addMethod('add' . ucfirst($name) . 'Item', array(new Generator\ParameterGenerator('item', $propertyInfos['model'])), Generator\MethodGenerator::FLAG_PUBLIC, sprintf('return $this->get%s()->add($item);' . PHP_EOL, ucfirst($name)), new Generator\DocBlockGenerator('Add new item to ' . ucfirst($name), '', array(new ReturnTag(array('datatype' => 'bool')))));
$class->addMethod('remove' . ucfirst($name) . 'Item', array(new Generator\ParameterGenerator('item', $propertyInfos['model'])), Generator\MethodGenerator::FLAG_PUBLIC, sprintf('return $this->get%s()->remove($item);' . PHP_EOL, ucfirst($name)), new Generator\DocBlockGenerator('Remove $item from ' . ucfirst($name), '', array(new ReturnTag(array('datatype' => 'bool')))));
}
}
}