/** * @param type $name * @param type $type * @param type $docString * @return PhpProperty */ public function addProperty($name, $type, $docString) { $property = new PropertyGenerator(); $docblock = new DocBlockGenerator(); $property->setName($name); $property->setVisibility(PropertyGenerator::VISIBILITY_PROTECTED); $docblock->setShortDescription($docString); $docblock->setTag(new Tag(array('name' => 'var', 'description' => $type))); $property->setDocblock($docblock); $this->addPropertyFromGenerator($property); return $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()); }
public function buildModel($model) { $class = new ClassGenerator($model->config->name); $class->setExtendedClass('\\app\\extensions\\data\\Model'); $class->setNamespaceName('app\\models'); $repository = "{$model->config->repository}" == "true" ? "(repositoryClass=\"app\\resources\\repository\\{$model->config->name}Repository\")\n" : null; $callback = "{$model->config->callback}" == "true" ? "@HasLifecycleCallbacks\n" : null; $table = "@Table(name=\"{$model->config->table}\")"; $docblock = new DocblockGenerator("@Entity" . $repository . $callback . $table); $class->setDocblock($docblock); $validations = array(); foreach ($model->fields->field as $field) { $type = "{$field->type}"; $nullable = isset($field->nullable) ? ", nullable=\"{$field->nullable}\"" : null; $length = isset($field->length) ? ", length=\"{$field->length}\"" : null; $default = isset($field->default) ? "{$field->default}" : null; if ($type == "boolean" && $default) { $default = $default == "true" ? true : false; } $property = new PropertyGenerator("{$field->name}", $default, PropertyGenerator::FLAG_PROTECTED); switch ($type) { case "index": $docblock = '@Id @Column(type="integer") @GeneratedValue'; break; case "relation": $mappedBy = isset($field->mappedBy) ? ", mappedBy=\"{$field->mappedBy}\"" : ""; $inversedBy = isset($field->inversedBy) ? ", inversedBy=\"{$field->inversedBy}\"" : ""; $cascades = ''; if (isset($field->cascades->cascade[0])) { $cascades = ", cascade={"; foreach ($field->cascades->cascade as $cascade) { $cascades .= "\"{$cascade}\","; } $cascades = substr($cascades, 0, -1) . "}"; } $docblock = "@{$field->relation}(targetEntity=\"{$field->targetEntity}\"{$mappedBy}{$inversedBy}{$cascades})"; break; default: $docblock = "@Column(type=\"{$type}\"{$nullable}{$length})"; } $property->setDocblock($docblock); $class->setProperty($property); } foreach ($model->validations->validation as $validation) { $validations["{$validation->field}"] = array(); foreach ($validation->rules->rule as $rule) { $validations["{$validation->field}"][] = array("{$rule->type}", "message" => "{$rule->message}"); } } if (!empty($validations)) { $class->setProperty(new PropertyGenerator("validations", $validations, PropertyGenerator::FLAG_PROTECTED)); } if ($callback) { $beforePersist = new MethodGenerator('beforePersist'); $beforeUpdate = new MethodGenerator('beforeUpdate'); $beforePersist->setDocblock("@PrePersist"); $beforeUpdate->setDocblock("@PreUpdate"); $beforePersist->setBody("\$this->created = new \\DateTime();"); $beforeUpdate->setBody("\$this->modified = new \\DateTime();"); $class->setMethods(array($beforePersist, $beforeUpdate)); } return $class; }