Add option to show all entities

This commit is contained in:
Tim 2022-10-25 11:15:07 +02:00
parent 18a6a21085
commit f4a4362592
2 changed files with 23 additions and 5 deletions

View File

@ -33,7 +33,8 @@ abstract class AbstractEntityCommand extends Command
->addOption('create', 'c', InputOption::VALUE_NONE, 'Create entity') ->addOption('create', 'c', InputOption::VALUE_NONE, 'Create entity')
->addOption('modify', 'm', InputOption::VALUE_NONE, 'Edit entity') ->addOption('modify', 'm', InputOption::VALUE_NONE, 'Edit entity')
->addOption('delete', 'd', InputOption::VALUE_NONE, 'Delete entity') ->addOption('delete', 'd', InputOption::VALUE_NONE, 'Delete entity')
->addOption('show', 's', InputOption::VALUE_NONE, 'Show entity'); ->addOption('show', 's', InputOption::VALUE_NONE, 'Show entity')
->addOption('all', 'a', InputOption::VALUE_NONE, 'Show all entities');
} }
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
@ -75,9 +76,17 @@ abstract class AbstractEntityCommand extends Command
$entity = $this->select($io, $id); $entity = $this->select($io, $id);
$table = $io->createTable(); $table = $io->createTable();
$table->setHeaders(['property', 'value']); $table->setHeaders(['property', 'value']);
$table->setRows($builder->getAllProperties($entity)); $table->setRows($builder->getAllPropertiesTable($entity));
$table->setHeaderTitle(sprintf('id: %s', $entity->getId())); $table->setHeaderTitle(sprintf('id: %s', $entity->getId()));
$table->render(); $table->render();
} elseif ($input->getOption('all')) {
$io->info(sprintf('Showing all %s', $this->getEntityClass()));
$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->setHeaderTitle($this->getEntityClass());
$table->render();
} else { } else {
$io->error('Select at least one of the options: Create (-c), Modify (-m), Delete (-d) or Show (-s)'); $io->error('Select at least one of the options: Create (-c), Modify (-m), Delete (-d) or Show (-s)');
return Command::INVALID; return Command::INVALID;

View File

@ -29,7 +29,7 @@ class Builder
return $this; return $this;
} }
private function getPopertyNames(): array public function getPopertyNames(): array
{ {
return array_map(fn(BaseElement $element) => $element->getProperty(), $this->elements); return array_map(fn(BaseElement $element) => $element->getProperty(), $this->elements);
} }
@ -60,10 +60,19 @@ class Builder
public function getAllProperties(object $object): array public function getAllProperties(object $object): array
{ {
$data = []; $properties = [];
foreach ($this->elements as $element) { foreach ($this->elements as $element) {
$getter = sprintf('get%s', ucfirst($element->getProperty())); $getter = sprintf('get%s', ucfirst($element->getProperty()));
$data[] = [$element->getProperty(), $object->$getter()]; $properties[$element->getProperty()] = $object->$getter();
}
return $properties;
}
public function getAllPropertiesTable(object $object): array
{
$data = [];
foreach ($this->getAllProperties($object) as $property => $value) {
$data[] = [$property, $value];
} }
return $data; return $data;
} }