Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
dakorpar committed Mar 6, 2019
2 parents 55abec7 + 96c1f9b commit 8e9d90d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/Generator/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -125,15 +125,15 @@ 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());
}

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;');
Expand Down
12 changes: 12 additions & 0 deletions src/Generator/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace DodoIt\EntityGenerator\Generator;

use Doctrine\Common\Inflector\Inflector;

class Helper
{

Expand All @@ -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;
}

}
6 changes: 6 additions & 0 deletions tests/Generator/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ public function testMultiArrayFlip(): void
]);
}

public function testCamelize()
{
$this->assertEquals('User', Helper::camelize('users'));
$this->assertEquals('UserLogin', Helper::camelize('users_logins'));
}

}

0 comments on commit 8e9d90d

Please sign in to comment.