From f4dc65dd7f24d80d1dfec3f0c5d9f713208d5947 Mon Sep 17 00:00:00 2001 From: Dalibor Korpar Date: Wed, 6 Mar 2019 10:15:07 +0100 Subject: [PATCH 1/2] Add visibility to getters and setters (#5) --- src/Generator/Config.php | 10 ++++++++++ src/Generator/Generator.php | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Generator/Config.php b/src/Generator/Config.php index d246b89..678bf34 100644 --- a/src/Generator/Config.php +++ b/src/Generator/Config.php @@ -75,6 +75,16 @@ public function __construct(?array $config = null) */ public $generateSetters = true; + /** + * @var string + */ + public $getterVisibility = 'public'; + + /** + * @var string + */ + public $setterVisibility = 'public'; + /** * @var bool */ diff --git a/src/Generator/Generator.php b/src/Generator/Generator.php index 47c9696..629dfac 100644 --- a/src/Generator/Generator.php +++ b/src/Generator/Generator.php @@ -125,7 +125,7 @@ protected function generateColumn(ClassType $entity, Column $column): void if ($this->config->generateGetters) { $getter = $entity->addMethod('get' . Inflector::classify($column->getField())); - $getter->setVisibility('public') + $getter->setVisibility($this->config->getterVisibility) ->addBody('return $this->' . $column->getField() . ';') ->setReturnType($type) ->setReturnNullable($column->isNullable()); @@ -133,7 +133,7 @@ protected function generateColumn(ClassType $entity, Column $column): void if ($this->config->generateSetters) { $setter = $entity->addMethod('set' . Inflector::classify($column->getField())); - $setter->setVisibility('public'); + $setter->setVisibility($this->config->setterVisibility); $setter->addParameter('value')->setTypeHint($type)->setNullable($column->isNullable()); $setter->addBody('$this[\'' . $column->getField() . '\'] = $value;'); $setter->addBody('return $this;'); From 96c1f9b347607b54c496297305093b47c1298c3b Mon Sep 17 00:00:00 2001 From: Dalibor Korpar Date: Wed, 6 Mar 2019 10:24:31 +0100 Subject: [PATCH 2/2] singularize all words not just last one (#6) * singularize all words not just last one --- src/Generator/Generator.php | 2 +- src/Generator/Helper.php | 12 ++++++++++++ tests/Generator/HelperTest.php | 6 ++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Generator/Generator.php b/src/Generator/Generator.php index 629dfac..ab00cbe 100644 --- a/src/Generator/Generator.php +++ b/src/Generator/Generator.php @@ -84,7 +84,7 @@ protected function getClassName(string $table): string if (isset($this->config->replacements[$table])) { return $this->config->replacements[$table]; } - return $this->config->prefix . Inflector::singularize(Inflector::classify($table)) . $this->config->suffix; + return $this->config->prefix . Helper::camelize($table) . $this->config->suffix; } /** diff --git a/src/Generator/Helper.php b/src/Generator/Helper.php index 23300c8..7941eee 100644 --- a/src/Generator/Helper.php +++ b/src/Generator/Helper.php @@ -2,6 +2,8 @@ namespace DodoIt\EntityGenerator\Generator; +use Doctrine\Common\Inflector\Inflector; + class Helper { @@ -20,4 +22,14 @@ public static function multiArrayFlip(array $array): array return $result; } + public static function camelize(string $input, string $separator = '_'): string + { + $words = explode($separator, $input); + $result = ''; + foreach ($words as $word) { + $result .= Inflector::singularize(ucfirst($word)); + } + return $result; + } + } diff --git a/tests/Generator/HelperTest.php b/tests/Generator/HelperTest.php index 064c5aa..97cc88c 100644 --- a/tests/Generator/HelperTest.php +++ b/tests/Generator/HelperTest.php @@ -22,4 +22,10 @@ public function testMultiArrayFlip(): void ]); } + public function testCamelize() + { + $this->assertEquals('User', Helper::camelize('users')); + $this->assertEquals('UserLogin', Helper::camelize('users_logins')); + } + }