From 87cb1dcb7ab3f20159d85c74ec9c92f450aef43a Mon Sep 17 00:00:00 2001 From: Dalibor Korpar Date: Wed, 6 Mar 2019 21:22:45 +0100 Subject: [PATCH] allow setting getters/setters body (#9) --- src/Generator/Config.php | 10 ++++++++++ src/Generator/Generator.php | 5 ++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Generator/Config.php b/src/Generator/Config.php index e62b24e..7501fa5 100644 --- a/src/Generator/Config.php +++ b/src/Generator/Config.php @@ -94,11 +94,21 @@ public function __construct(?array $config = null) */ public $getterVisibility = 'public'; + /** + * @var string + */ + public $getterBody = 'return $this->__FIELD__;'; + /** * @var string */ public $setterVisibility = 'public'; + /** + * @var string + */ + public $setterBody = '$this[\'__FIELD__\'] = $value;' . "\n" . 'return $this;'; + /** * @var bool */ diff --git a/src/Generator/Generator.php b/src/Generator/Generator.php index ff0580c..3e8a435 100644 --- a/src/Generator/Generator.php +++ b/src/Generator/Generator.php @@ -130,7 +130,7 @@ protected function generateColumn(ClassType $entity, Column $column): void if ($this->config->generateGetters) { $getter = $entity->addMethod('get' . Inflector::classify($column->getField())); $getter->setVisibility($this->config->getterVisibility) - ->addBody('return $this->' . $column->getField() . ';') + ->addBody(str_replace('__FIELD__', $column->getField(), $this->config->getterBody)) ->setReturnType($type) ->setReturnNullable($column->isNullable()); } @@ -139,8 +139,7 @@ protected function generateColumn(ClassType $entity, Column $column): void $setter = $entity->addMethod('set' . Inflector::classify($column->getField())); $setter->setVisibility($this->config->setterVisibility); $setter->addParameter('value')->setTypeHint($type)->setNullable($column->isNullable()); - $setter->addBody('$this[\'' . $column->getField() . '\'] = $value;'); - $setter->addBody('return $this;'); + $setter->addBody(str_replace('__FIELD__', $column->getField(), $this->config->setterBody)); $setter->setReturnType('self'); } }