/** * @param string $name * @param string $type * @return PropertyGenerator */ protected function getProperty($name, $type) { $property = new PropertyGenerator($name, null, PropertyGenerator::FLAG_PROTECTED); $property->setDocBlock(new DocBlockGenerator()); $property->getDocBlock()->setTag(new Tag\GenericTag('var', $type)); return $property; }
/** * @return PropertyGenerator */ protected function getIdProperty() { $id = new PropertyGenerator('id', null, PropertyGenerator::FLAG_PROTECTED); $id->setDocBlock(new DocBlockGenerator()); $id->getDocBlock()->setTag(new Tag\GenericTag('var', 'int')); $id->getDocBlock()->setTag(new Tag\GenericTag('ORM\\Id')); $id->getDocBlock()->setTag(new Tag\GenericTag('ORM\\GeneratedValue')); $id->getDocBlock()->setTag(new Tag\GenericTag('ORM\\Column(type="integer")')); return $id; }
/** * @param string $className * @return TraitGenerator */ public function handle(string $className) { if (!preg_match(static::MATCH_PATTERN, $className, $matches)) { return null; } $baseName = $matches[1]; $namespace = $this->deriveNamespaceFromClassName($className); $camelCaseBaseName = $this->stripInterfaceLabelFromName($this->makeClassNameCamelCase($baseName)); $trait = new TraitGenerator("Needs{$baseName}Trait"); $trait->setNamespaceName($namespace); $docBlock = new DocBlockGenerator(null, null, array(array("name" => "var", "content" => "\\{$namespace}\\{$baseName}"))); $property = new PropertyGenerator($camelCaseBaseName, null, PropertyGenerator::FLAG_PROTECTED); $property->setDocBlock($docBlock); $trait->addProperties(array($property)); $parameter = new ParameterGenerator($camelCaseBaseName, "{$namespace}\\{$baseName}"); $method = new MethodGenerator("set" . $this->stripInterfaceLabelFromName($baseName), array($parameter), MethodGenerator::FLAG_PUBLIC, sprintf('$this->%1$s = $%1$s;', $camelCaseBaseName)); $trait->addMethods(array($method)); return $trait; }
/** * @param \Model\Generator\Part\Model|\Model\Generator\Part\PartInterface $part */ public function preRun(PartInterface $part) { /** * @var Table $table */ $table = $part->getTable(); /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); /** @var array|AbstractLink[] $linkList */ $linkList = $table->getLink(); foreach ($linkList as $link) { $_name = strtoupper($link->getForeignEntity()); $name = 'JOIN_' . $_name; $property = new PropertyGenerator($name, strtolower($_name), PropertyGenerator::FLAG_CONSTANT); $tags = array(array('name' => 'const'), array('name' => 'var', 'description' => 'string')); $docblock = new DocBlockGenerator('JOIN сущность ' . $_name); $docblock->setTags($tags); $property->setDocBlock($docblock); $this->_data[$table->getName()][$name] = $property; } }
/** * @param \Model\Generator\Part\Model $part */ public function generateEnumConstantList($part) { $file = $part->getFile(); $table = $part->getTable(); foreach ($table->getColumn() as $column) { if ($column->getColumnType() == 'enum') { $enumList = $column->getEnumValuesAsArray(); // пропускаем флаги и enum поля с числом параметров >10 if (substr($column->getName(), 0, 3) == 'is_' || $enumList == array('y', 'n') || count($enumList) > 10) { continue; } foreach ($enumList as $enumValue) { $columnName = $column->getName(); $name = strtoupper($columnName . '_' . $enumValue); $property = new PropertyGenerator($name, $enumValue, PropertyGenerator::FLAG_CONSTANT); $tags = array(array('name' => 'const'), array('name' => 'var', 'description' => 'string')); $docblock = new DocBlockGenerator("Значение {$enumValue} поля {$columnName}"); $docblock->setTags($tags); $property->setDocBlock($docblock); $file->getClass()->addPropertyFromGenerator($property); } } } }
/** * @param \Model\Generator\Part\Model|\Model\Generator\Part\PartInterface $part */ public function preRun(PartInterface $part) { /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); /** @var Table $table */ $table = $part->getTable(); /** @var string $tableName */ //$tableName = $table->getName(); /** @var $linkList|Link[] \Model\Cluster\Schema\Table\Column */ //$linkList = $table->getLink(); if ($table->isTree()) { $names = array('WITH_CHILD', 'WITH_CHILD_COLLECTION', 'WITH_ALL_CHILD', 'WITH_ALL_CHILD_COLLECTION'); foreach ($names as $name) { $property = new PropertyGenerator($name, strtolower($name), PropertyGenerator::FLAG_CONSTANT); $tags = array(array('name' => 'const'), array('name' => 'var', 'description' => 'string')); $docblock = new DocBlockGenerator('WITH entity for child list'); $docblock->setTags($tags); $property->setDocBlock($docblock); $file->getClass()->addPropertyFromGenerator($property); } } }
/** * @param string $columnName * @param ConstraintObject $foreignKey */ protected function addOptionsProperty($columnName, ConstraintObject $foreignKey) { $columnName = lcfirst(StaticFilter::execute($columnName, 'Word\\UnderscoreToCamelCase')); $property = new PropertyGenerator($columnName . 'Options'); $property->addFlag(PropertyGenerator::FLAG_PRIVATE); $property->setDocBlock(new DocBlockGenerator($columnName . ' options', null, [['name' => 'var', 'description' => 'array']])); $this->addPropertyFromGenerator($property); }
/** * @group ZF-7205 */ public function testPropertyCanHaveDocBlock() { $codeGenProperty = new PropertyGenerator('someVal', 'some string value', PropertyGenerator::FLAG_STATIC | PropertyGenerator::FLAG_PROTECTED); $codeGenProperty->setDocBlock('@var string $someVal This is some val'); $expected = <<<EOS /** * @var string \$someVal This is some val */ protected static \$someVal = 'some string value'; EOS; $this->assertEquals($expected, $codeGenProperty->generate()); }
/** * @return ClassGenerator[] */ private function getTypesCode() { $generator = $this; return array_map(function (Structure\TypeStructure $type) use($generator) { $code = new ClassGenerator($type->getClassName()); $code->setNamespaceName($generator->getFullNamespace($type->getClassName(), true)); foreach ($type->getMembers() as $member) { $tag = new Tag(); $tag->setName('var'); if ($member->isPrimitive()) { $tag->setDescription($member->getType()); } else { $tag->setDescription("\\" . $generator->getFullNamespace($member->getType())); } $docBlock = new DocBlockGenerator(null, null, [$tag]); $property = new PropertyGenerator(); $property->setName($member->getName()); $property->setDocBlock($docBlock); $code->addPropertyFromGenerator($property); } return $code; }, $this->service->getTypes()->getArrayCopy()); }
/** * @return PropertyGenerator */ protected function getStackProperty() { $property = new PropertyGenerator('stack', null, PropertyGenerator::FLAG_PROTECTED); $property->setDocBlock(new DocBlockGenerator('Tokens stack', '', array(array('name' => 'var', 'description' => '\\SplStack')))); return $property; }
/** * @param $className */ protected function addMessageTemplates($className) { // generate property $property = new PropertyGenerator(); $property->setName('messageTemplates'); $property->setDefaultValue(['invalid' . $className => 'Value "%value%" is not valid']); $property->setVisibility(PropertyGenerator::FLAG_PROTECTED); // check for api docs if ($this->config['flagAddDocBlocks']) { $property->setDocBlock(new DocBlockGenerator('Validation failure message template definitions', '', [new GenericTag('var', 'array')])); } // add property $this->addPropertyFromGenerator($property); }
/** * {@inheritdoc} */ public function createDTO(Type $type) { $class = $this->createClassFromType($type); $parentType = $type->getParent(); // set the parent class if ($parentType) { $parentType = $parentType->getBase(); if ($parentType->getSchema()->getTargetNamespace() !== SchemaReader::XSD_NS) { $class->setExtendedClass($parentType->getName()); } } // check if the type is abstract $class->setAbstract($type->isAbstract()); // create the constructor $constructor = new MethodGenerator('__construct'); $constructorDoc = new DocBlockGenerator(); $constructorBody = ''; $constructor->setDocBlock($constructorDoc); $class->addMethodFromGenerator($constructor); /* @var \Goetas\XML\XSDReader\Schema\Type\ComplexType $type */ foreach ($type->getElements() as $element) { /* @var \Goetas\XML\XSDReader\Schema\Element\Element $element */ $docElementType = $this->namespaceInflector->inflectDocBlockQualifiedName($element->getType()); $elementName = $element->getName(); // create a param and a param tag used in constructor and setter docs $param = new ParameterGenerator($elementName, $this->namespaceInflector->inflectName($element->getType())); $paramTag = new ParamTag($elementName, $docElementType); // if the param type is not a PHP primitive type and its namespace is different that the class namespace if ($type->getSchema()->getTargetNamespace() === SchemaReader::XSD_NS and $class->getNamespaceName() !== $this->namespaceInflector->inflectNamespace($element->getType())) { $class->addUse($this->namespaceInflector->inflectQualifiedName($element->getType())); } // set the parameter nullability if ($element->isNil() or $this->config->isNullConstructorArguments()) { $param->setDefaultValue(new ValueGenerator(null, ValueGenerator::TYPE_NULL)); } /* * PROPERTY CREATION */ $docDescription = new Html2Text($type->getDoc()); $doc = new DocBlockGenerator(); $doc->setShortDescription($docDescription->getText()); $doc->setTag(new GenericTag('var', $docElementType)); $property = new PropertyGenerator(); $property->setDocBlock($doc); $property->setName(filter_var($elementName, FILTER_CALLBACK, array('options' => array($this, 'sanitizeVariableName')))); $property->setVisibility($this->config->isAccessors() ? AbstractMemberGenerator::VISIBILITY_PROTECTED : AbstractMemberGenerator::VISIBILITY_PUBLIC); $class->addPropertyFromGenerator($property); /* * IMPORTS */ if ($element->getType()->getSchema()->getTargetNamespace() !== SchemaReader::XSD_NS and $this->namespaceInflector->inflectNamespace($element->getType()) !== $class->getNamespaceName()) { $class->addUse($this->namespaceInflector->inflectQualifiedName($element->getType())); } /* * CONSTRUCTOR PARAM CREATION */ $constructorDoc->setTag($paramTag); $constructorBody .= "\$this->{$elementName} = \${$elementName};" . AbstractGenerator::LINE_FEED; $constructor->setParameter($param); /* * ACCESSORS CREATION */ if ($this->config->isAccessors()) { // create the setter $setterDoc = new DocBlockGenerator(); $setterDoc->setTag($paramTag); $setter = new MethodGenerator('set' . ucfirst($elementName)); $setter->setParameter($param); $setter->setDocBlock($setterDoc); $setter->setBody("\$this->{$elementName} = \${$elementName};"); $class->addMethodFromGenerator($setter); // create the getter $getterDoc = new DocBlockGenerator(); $getterDoc->setTag(new ReturnTag($docElementType)); $getter = new MethodGenerator('get' . ucfirst($elementName)); $getter->setDocBlock($getterDoc); $getter->setBody("return \$this->{$elementName};"); $class->addMethodFromGenerator($getter); } } // finalize the constructor body $constructor->setBody($constructorBody); $this->serializeClass($class); // register the class in the classmap $this->classmap[$class->getName()] = $class->getNamespaceName() . '\\' . $class->getName(); return $class->getName(); }
/** * Add form property * * @param $formClass */ protected function addFormProperty($formClass) { $property = new PropertyGenerator(lcfirst($formClass)); $property->addFlag(PropertyGenerator::FLAG_PRIVATE); $property->setDocBlock(new DocBlockGenerator(null, null, [['name' => 'var', 'description' => $formClass]])); $this->addPropertyFromGenerator($property); }
/** * @param string $columnName */ protected function addOptionsProperty($columnName) { $columnName = lcfirst($this->filterUnderscoreToCamelCase($columnName)); $property = new PropertyGenerator($columnName . 'Options'); $property->addFlag(PropertyGenerator::FLAG_PRIVATE); $property->setDocBlock(new DocBlockGenerator($columnName . ' options', null, [['name' => 'var', 'description' => 'array']])); $this->addPropertyFromGenerator($property); }
/** * 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'))))); } } }
/** * @param ClassGenerator $class * @param Tag $tagItem */ protected function addPropertyFlags(ClassGenerator $class, Tag $tagItem) { $flags = []; foreach ($tagItem->getAttributes() as $tagAttribute) { if ($tagAttribute->isFlag()) { $flags[] = $tagAttribute->getName(); } } sort($flags); $property = new PropertyGenerator('flags', [], PropertyGenerator::FLAG_PROTECTED); $property->setDefaultValue($flags); $docBlock = new DocBlockGenerator(); $docBlock->setTag(new GenericTag('var', 'array')); $property->setDocBlock($docBlock); $class->addPropertyFromGenerator($property); }
/** * @param string $columnName * @param ConstraintObject $foreignKey */ protected function addOptionsProperty($columnName, ConstraintObject $foreignKey) { $property = new PropertyGenerator($columnName . 'Options'); $property->addFlag(PropertyGenerator::FLAG_PRIVATE); $property->setDocBlock(new DocBlockGenerator($columnName . ' options', null, [['name' => 'var', 'description' => 'array']])); $this->addPropertyFromGenerator($property); }
/** * @param $columnName * @param $columnType * * @return PropertyGenerator */ protected function generateProperty($columnName, $columnType) { $property = new PropertyGenerator($columnName); $property->addFlag(PropertyGenerator::FLAG_PROTECTED); $property->setDocBlock(new DocBlockGenerator($columnName . ' property', null, [['name' => 'var', 'description' => $columnType]])); return $property; }