diff --git a/src/EntityBuilder/AbstractEntityCommand.php b/src/EntityBuilder/AbstractEntityCommand.php index 406103c..8c14c5f 100644 --- a/src/EntityBuilder/AbstractEntityCommand.php +++ b/src/EntityBuilder/AbstractEntityCommand.php @@ -49,7 +49,7 @@ abstract class AbstractEntityCommand extends Command do { $io->info(sprintf('Creating new "%s"', $this->getEntityClass())); $entity = new ($this->getEntityClass())(); - $builder->setAll($entity); + $builder->setAllByQuestions($entity); $this->em->persist($entity); if ($io->askQuestion(new ConfirmationQuestion(sprintf('Are you sure you want to create "%s" with above properties?', $entity)))) { @@ -61,7 +61,7 @@ abstract class AbstractEntityCommand extends Command $entity = $this->select($io, $id); do { - $builder->setByQuestion($entity); + $builder->setSingleByQuestion($entity); } while ($io->askQuestion(new ConfirmationQuestion('Modify another property?'))); $this->em->persist($entity); } elseif ($input->getOption('delete')) { @@ -87,8 +87,8 @@ abstract class AbstractEntityCommand extends Command $entities = $this->em->getRepository($this->getEntityClass())->findAll(); $table = $io->createTable(); - $table->setHeaders($builder->getPopertyNames()); - $table->setRows(array_map(fn(object $o) => $builder->getAllProperties($o), $entities)); + $table->setHeaders($builder->getPropertyNames()); + $table->setRows(array_map(fn(object $o) => $builder->getAllPropertyValues($o), $entities)); $table->setHeaderTitle($this->getEntityClass()); $table->render(); } else { diff --git a/src/EntityBuilder/BaseElement.php b/src/EntityBuilder/BaseElement.php index 1632e92..1235619 100644 --- a/src/EntityBuilder/BaseElement.php +++ b/src/EntityBuilder/BaseElement.php @@ -31,4 +31,9 @@ class BaseElement { return $this->property; } + + public function transformValue(mixed $value): mixed + { + return $value; + } } \ No newline at end of file diff --git a/src/EntityBuilder/Builder.php b/src/EntityBuilder/Builder.php index 3d5ddb8..8318d47 100644 --- a/src/EntityBuilder/Builder.php +++ b/src/EntityBuilder/Builder.php @@ -29,41 +29,48 @@ class Builder return $this; } - public function getPopertyNames(): array + public function getPropertyNames(): array { return array_map(fn(BaseElement $element) => $element->getProperty(), $this->elements); } - private function setProperty(object $object, BaseElement $element): void + private function setProperty(object $object, string $property, mixed $value): void { - $setter = sprintf('set%s', ucfirst($element->getProperty())); + $setter = sprintf('set%s', ucfirst($property)); if (!method_exists($object, $setter)) { throw new Exception(sprintf('Object does not have setter "%s"', $setter)); } - $object->$setter($this->io->askQuestion($element->getQuestion())); + $object->$setter($value); } - public function setAll(object $object): void + public function setAllByQuestions(object $object): void { foreach ($this->elements as $element) { - $this->setProperty($object, $element); + $value = $element->transformValue($this->io->askQuestion($element->getQuestion())); + $this->setProperty($object, $element->getProperty(), $value); } } - public function setByQuestion(object $object): void + public function setSingleByQuestion(object $object): void { $element = $this->io->askQuestion(new ChoiceQuestion('Select property to modify', $this->elements)); $getter = sprintf('get%s', ucfirst($element->getProperty())); $this->io->writeln(sprintf('Current value: %s', $object->$getter())); - $this->setProperty($object, $element); + + $value = $element->transformValue($this->io->askQuestion($element->getQuestion())); + $this->setProperty($object, $element->getProperty(), $value); } - public function getAllProperties(object $object): array + public function getAllPropertyValues(object $object): array { $properties = []; foreach ($this->elements as $element) { $getter = sprintf('get%s', ucfirst($element->getProperty())); - $properties[$element->getProperty()] = $object->$getter(); + $value = $object->$getter(); + if ($value instanceof \UnitEnum) { + $value = $value->value; + } + $properties[$element->getProperty()] = $value; } return $properties; } @@ -71,7 +78,10 @@ class Builder public function getAllPropertiesTable(object $object): array { $data = []; - foreach ($this->getAllProperties($object) as $property => $value) { + foreach ($this->getAllPropertyValues($object) as $property => $value) { + if ($value instanceof \UnitEnum) { + $value = $value->value; + } $data[] = [$property, $value]; } return $data; diff --git a/src/EntityBuilder/ChoiceElement.php b/src/EntityBuilder/ChoiceElement.php index c106213..0dc4a91 100644 --- a/src/EntityBuilder/ChoiceElement.php +++ b/src/EntityBuilder/ChoiceElement.php @@ -19,5 +19,4 @@ class ChoiceElement extends BaseElement { parent::__construct($property); } - } \ No newline at end of file diff --git a/src/EntityBuilder/EnumChoiceElement.php b/src/EntityBuilder/EnumChoiceElement.php new file mode 100644 index 0000000..09e1027 --- /dev/null +++ b/src/EntityBuilder/EnumChoiceElement.php @@ -0,0 +1,27 @@ +getPrettyProperty(), array_map(fn($enum) => $enum->value, $this->enumClass::cases())); + } + + public function __construct( + string $property, + private readonly string $enumClass, + ) + { + parent::__construct($property); + } + + public function transformValue(mixed $value): mixed + { + return $this->enumClass::from($value); + } +} \ No newline at end of file