From f4a4362592e849787da682695c416366a09d7993 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 25 Oct 2022 11:15:07 +0200 Subject: [PATCH] Add option to show all entities --- src/EntityBuilder/AbstractEntityCommand.php | 13 +++++++++++-- src/EntityBuilder/Builder.php | 15 ++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/EntityBuilder/AbstractEntityCommand.php b/src/EntityBuilder/AbstractEntityCommand.php index 0457f68..cb33fb6 100644 --- a/src/EntityBuilder/AbstractEntityCommand.php +++ b/src/EntityBuilder/AbstractEntityCommand.php @@ -33,7 +33,8 @@ abstract class AbstractEntityCommand extends Command ->addOption('create', 'c', InputOption::VALUE_NONE, 'Create entity') ->addOption('modify', 'm', InputOption::VALUE_NONE, 'Edit 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 @@ -75,9 +76,17 @@ abstract class AbstractEntityCommand extends Command $entity = $this->select($io, $id); $table = $io->createTable(); $table->setHeaders(['property', 'value']); - $table->setRows($builder->getAllProperties($entity)); + $table->setRows($builder->getAllPropertiesTable($entity)); $table->setHeaderTitle(sprintf('id: %s', $entity->getId())); $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 { $io->error('Select at least one of the options: Create (-c), Modify (-m), Delete (-d) or Show (-s)'); return Command::INVALID; diff --git a/src/EntityBuilder/Builder.php b/src/EntityBuilder/Builder.php index 202f4e7..3d5ddb8 100644 --- a/src/EntityBuilder/Builder.php +++ b/src/EntityBuilder/Builder.php @@ -29,7 +29,7 @@ class Builder return $this; } - private function getPopertyNames(): array + public function getPopertyNames(): array { return array_map(fn(BaseElement $element) => $element->getProperty(), $this->elements); } @@ -60,10 +60,19 @@ class Builder public function getAllProperties(object $object): array { - $data = []; + $properties = []; foreach ($this->elements as $element) { $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; }