From 96c1f9b347607b54c496297305093b47c1298c3b Mon Sep 17 00:00:00 2001 From: Dalibor Korpar Date: Wed, 6 Mar 2019 10:24:31 +0100 Subject: [PATCH] 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')); + } + }