Create EnumChoiceElement and allow enums as values

This commit is contained in:
Tim 2023-09-07 10:53:51 +02:00
parent 991c552a46
commit da62dc4a6e
5 changed files with 57 additions and 16 deletions

View File

@ -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 {

View File

@ -31,4 +31,9 @@ class BaseElement
{
return $this->property;
}
public function transformValue(mixed $value): mixed
{
return $value;
}
}

View File

@ -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;

View File

@ -19,5 +19,4 @@ class ChoiceElement extends BaseElement
{
parent::__construct($property);
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Ardent\UtilBundle\EntityBuilder;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
class EnumChoiceElement extends BaseElement
{
public function getQuestion(): Question
{
return new ChoiceQuestion($this->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);
}
}