diff --git a/composer.json b/composer.json index 35814fd..440b5a1 100644 --- a/composer.json +++ b/composer.json @@ -24,11 +24,11 @@ "php": "^7.1", "nette/utils": "^2.4|^3.0", "nette/php-generator": "^2.6|^3.0", - "ninjify/qa": "^0.8", "symfony/console": "^4.2", "doctrine/inflector": "^1.3" }, "require-dev": { + "ninjify/qa": ">=0.8", "phpunit/phpunit": ">=7.0" }, "scripts": { @@ -57,4 +57,4 @@ "temp/phpstan/vendor/bin/phpstan analyse -l 2 -c phpstan.neon src --memory-limit 1024M" ] } -} \ No newline at end of file +} diff --git a/src/Entity/Entity.php b/src/Entity/Entity.php index 8a84ff5..b5339cb 100644 --- a/src/Entity/Entity.php +++ b/src/Entity/Entity.php @@ -22,6 +22,7 @@ class Entity implements ArrayAccess, IteratorAggregate, Countable public function __construct(array $arr = []) { $this->data = $arr; + foreach ($arr as $k => $v) { $this->$k = $v; } @@ -36,7 +37,7 @@ public function _getModifications(): array public function tableName(): ?string { - return static::TABLE; + return self::TABLE; } /** diff --git a/src/Factory/GeneratorPdoFactory.php b/src/Factory/GeneratorPdoFactory.php index a8d9160..35a108d 100644 --- a/src/Factory/GeneratorPdoFactory.php +++ b/src/Factory/GeneratorPdoFactory.php @@ -22,6 +22,7 @@ public function create(Config $config): Generator { $repository = new PdoRepository($this->pdo); $generator = new Generator($repository, $config); + return $generator; } diff --git a/src/Generator/Config.php b/src/Generator/Config.php index f9be4c1..469400b 100644 --- a/src/Generator/Config.php +++ b/src/Generator/Config.php @@ -12,6 +12,7 @@ public function __construct(?array $config = null) if ($config === null) { return; } + foreach ($config as $key => $value) { $this->$key = $value; } diff --git a/src/Generator/Generator.php b/src/Generator/Generator.php index 9580aab..e8cf1d0 100644 --- a/src/Generator/Generator.php +++ b/src/Generator/Generator.php @@ -36,16 +36,22 @@ public function generate(?string $table = null, ?string $query = null): void if ($table === null) { throw new Exception('When using query table argument has to be provided!'); } + $this->repository->createViewFromQuery($table, $query); $this->generateEntity($table); $this->repository->dropView($table); + return; } + if ($table !== null) { $this->generateEntity($table); + return; } + $tables = $this->repository->getTables(); + foreach ($tables as $oneTable) { $this->generateEntity($oneTable); } @@ -62,31 +68,39 @@ public function generateEntity(string $table): void $entity = $namespace->addClass($shortClassName); $phpDocProperties = []; + if (!$this->config->rewrite && class_exists($fqnClassName)) { $this->cloneEntityFromExistingEntity($entity, ClassType::from($fqnClassName)); $phpDocProperties = Helper::getPhpDocComments($entity->getComment() ?? ''); } + $entity->addConstant($this->config->tableConstant, $table)->setVisibility('public'); $entity->setExtends($this->config->extends); $columns = $this->repository->getTableColumns($table); $mapping = []; + foreach ($columns as $column) { $this->validateColumnName($table, $column); $this->generateColumnConstant($entity, $column); + if (isset($entity->properties[$column->getField()]) || in_array($column->getField(), $phpDocProperties, true)) { continue; } + $mapping[$column->getField()] = Inflector::classify($column->getField()); $this->generateColumn($entity, $column); } + if ($this->config->generateMapping) { if (isset($entity->properties['mapping'])) { $mapping += $entity->getProperty('mapping')->getValue(); } + $entity->addProperty('mapping', $mapping)->setVisibility('protected') ->addComment('')->addComment('@var string[]')->addComment(''); } + file_put_contents($this->config->path . '/' . $shortClassName . '.php', $file->__toString()); } @@ -143,13 +157,17 @@ protected function generateColumn(ClassType $entity, Column $column): void protected function getColumnType(Column $column): string { $dbColumnType = $column->getType(); + if (Strings::contains($dbColumnType, '(')) { $dbColumnType = Strings::lower(Strings::before($dbColumnType, '(')); } + $typeMapping = Helper::multiArrayFlip($this->config->typeMapping); + if (isset($typeMapping[$dbColumnType])) { return $typeMapping[$dbColumnType]; } + return 'string'; } @@ -159,13 +177,16 @@ protected function generateColumnConstant(ClassType $entity, Column $column): vo $entity->addConstant($this->config->primaryKeyConstant, $column->getField()) ->setVisibility('public'); } + if ($this->config->generateColumnConstant) { $columnConstant = $this->config->prefix . Strings::upper(Inflector::tableize($column->getField())); + if ($columnConstant === 'CLASS') { $columnConstant = '_CLASS'; } $constants = $entity->getConstants(); + if (!isset($constants[$column->getField()])) { $entity->addConstant($columnConstant, $column->getField())->setVisibility('public'); } @@ -195,9 +216,11 @@ private function getMethodBody(string $class, string $name): string $source = file($func->getFileName()); $bodyLines = array_slice($source, $startLine, $length); $body = ''; + foreach ($bodyLines as $bodyLine) { $body .= Strings::after($bodyLine, "\t\t"); } + return $body; } diff --git a/src/Generator/Helper.php b/src/Generator/Helper.php index ff0844a..d2f0c81 100644 --- a/src/Generator/Helper.php +++ b/src/Generator/Helper.php @@ -14,11 +14,13 @@ class Helper public static function multiArrayFlip(array $array): array { $result = []; + foreach ($array as $key => $insideArray) { foreach ($insideArray as $value) { $result[$value] = $key; } } + return $result; } @@ -26,9 +28,11 @@ public static function camelize(string $input, array $replacements = [], string { $words = explode($separator, $input); $result = ''; + foreach ($words as $word) { $result .= $replacements[$word] ?? Inflector::singularize(ucfirst($word)); } + return $result; } @@ -44,9 +48,11 @@ public static function getPhpDocComments(string $phpDoc): array PREG_SET_ORDER ); $result = []; + foreach ($matches as $match) { $result[] = $match[2]; } + return $result; } diff --git a/src/Repository/PdoRepository.php b/src/Repository/PdoRepository.php index b496af1..47de2e9 100644 --- a/src/Repository/PdoRepository.php +++ b/src/Repository/PdoRepository.php @@ -32,6 +32,7 @@ public function getTableColumns(string $table): array { $query = $this->db->query('SHOW COLUMNS FROM `' . $table . '`'); $query->setFetchMode(PDO::FETCH_CLASS, Column::class); + return $query->fetchAll(); } diff --git a/tests/Generator/GeneratorTest.php b/tests/Generator/GeneratorTest.php index b09e033..9ab406d 100644 --- a/tests/Generator/GeneratorTest.php +++ b/tests/Generator/GeneratorTest.php @@ -149,9 +149,9 @@ public function testGenerateEntity_WithGenerateConstant_ShouldGenerateConstants( include $entityFile; $entityContents = file_get_contents($entityFile); - $this->assertContains('const TABLE_NAME = \'constants\'', $entityContents); - $this->assertContains('const PK_CONSTANT = \'id\'', $entityContents); - $this->assertContains('const ID = \'id\'', $entityContents); + $this->assertStringContainsString('const TABLE_NAME = \'constants\'', $entityContents); + $this->assertStringContainsString('const PK_CONSTANT = \'id\'', $entityContents); + $this->assertStringContainsString('const ID = \'id\'', $entityContents); unlink($entityFile); } diff --git a/tests/TestEntities/PhpDocPropertyEntity.php b/tests/TestEntities/PhpDocPropertyEntity.php index 735ae38..3144b4d 100644 --- a/tests/TestEntities/PhpDocPropertyEntity.php +++ b/tests/TestEntities/PhpDocPropertyEntity.php @@ -7,7 +7,7 @@ /** * @property int $id * @property string $title - * @property int $published + * @property bool $published * @property \DateTimeInterface $created_at */ class PhpDocPropertyEntity extends Entity @@ -24,6 +24,7 @@ public function getId(): int public function setId(int $value): self { $this['id'] = $value; + return $this; } @@ -37,6 +38,7 @@ public function getTitle(): ?string public function setTitle(?string $value): self { $this['title'] = $value; + return $this; } @@ -50,6 +52,7 @@ public function getPublished(): bool public function setPublished(bool $value): self { $this['published'] = $value; + return $this; } @@ -63,6 +66,7 @@ public function getCreatedAt(): ?\DateTimeInterface public function setCreatedAt(?\DateTimeInterface $value): self { $this['created_at'] = $value; + return $this; } diff --git a/tests/TestEntities/UserEntity.php b/tests/TestEntities/UserEntity.php index a7d1df0..1ca2bcb 100644 --- a/tests/TestEntities/UserEntity.php +++ b/tests/TestEntities/UserEntity.php @@ -30,6 +30,7 @@ public function getId(): int public function setId(int $value): self { $this['id'] = $value; + return $this; } @@ -43,6 +44,7 @@ public function getUsername(): string public function setUsername(string $value): self { $this['username'] = $value; + return $this; } @@ -56,6 +58,7 @@ public function getLastLogin(): ?DateTime public function setLastLogin(DateTime $value): self { $this['last_login'] = $value; + return $this; } @@ -69,6 +72,7 @@ public function isActive(): bool public function setActive(bool $value): self { $this['active'] = $value; + return $this; }