From a4fc1c7bc20b73422c92b32e805c1a5647b1ecdf Mon Sep 17 00:00:00 2001 From: Dalibor Korpar Date: Fri, 1 Mar 2019 10:46:38 +0100 Subject: [PATCH] add option to generate phpdoc properties (#3) --- examples/Pdo/generate.php | 2 +- src/Generator/Config.php | 10 ++++++++++ src/Generator/Generator.php | 20 +++++++++++++++----- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/examples/Pdo/generate.php b/examples/Pdo/generate.php index 82d509d..43dd617 100644 --- a/examples/Pdo/generate.php +++ b/examples/Pdo/generate.php @@ -5,7 +5,7 @@ $config = new \DodoIt\EntityGenerator\Generator\Config([ 'path' => __DIR__ . '/Entities', 'extends' => \Examples\Pdo\Entities\Entity::class, - 'namespace' => 'Examples\Pdo\Entities' + 'namespace' => 'Examples\Pdo\Entities', ]); $pdo = new \PDO('mysql:dbname=example;host=127.0.0.1', 'root', ''); diff --git a/src/Generator/Config.php b/src/Generator/Config.php index 0fc7cd4..d246b89 100644 --- a/src/Generator/Config.php +++ b/src/Generator/Config.php @@ -95,6 +95,16 @@ public function __construct(?array $config = null) */ public $propertyVisibility = 'protected'; + /** + * @var bool + */ + public $generatePhpDocProperties = false; + + /** + * @var string + */ + public $phpDocProperty = '@property'; + /** * @param string $name * @return void diff --git a/src/Generator/Generator.php b/src/Generator/Generator.php index a0602a1..36a9352 100644 --- a/src/Generator/Generator.php +++ b/src/Generator/Generator.php @@ -102,11 +102,21 @@ protected function validateColumnName(string $table, Column $column): void protected function generateColumn(ClassType $entity, Column $column): void { $type = $this->getColumnType($column); - $entity->addProperty($column->getField()) - ->setVisibility($this->config->propertyVisibility) - ->addComment('') - ->addComment('@var ' . $type) - ->addComment(''); + + if($this->config->generateProperties) { + $entity->addProperty($column->getField()) + ->setVisibility($this->config->propertyVisibility) + ->addComment('@var ' . $type); + } + + if ($this->config->generateColumnConstant) { + $columnConstant = $this->config->prefix . Strings::upper(Inflector::tableize($column->getField())); + $entity->addConstant($columnConstant, $column->getField()); + } + + if($this->config->generatePhpDocProperties) { + $entity->addComment($this->config->phpDocProperty . ' ' . $type . ' $' .$column->getField()); + } if ($this->config->generateColumnConstant) { $columnConstant = $this->config->prefix . Strings::upper(Inflector::tableize($column->getField()));