Files
slotdb-cli/src/Command/Group/GroupFindCommand.php
2025-03-15 14:41:43 +01:00

61 lines
1.6 KiB
PHP

<?php
namespace SlotDb\Cli\Command\Group;
use SlotDb\Client\SlotDbClient;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name:"group:find", aliases:[ "groups" ], description:"Find all groups")]
class GroupFindCommand extends Command
{
public function __construct(
private readonly SlotDbClient $client
)
{
parent::__construct();
}
protected function configure()
{
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$groups = $this->client->findGroups();
$keys = [];
foreach ($groups as $group) {
$keys = array_merge($keys, array_keys($group));
}
$keys = array_unique($keys);
$table = new Table($output);
$table->setHeaders($keys);
$table->setStyle('box');
foreach ($groups as $group) {
$row = [];
foreach ($keys as $key) {
$row[] = isset($group[$key]) ? $group[$key] : null;
}
$row = array_map(fn($v) => match(true) {
is_bool($v) => "<fg=magenta>".($v?"true":"false")."</>",
is_int($v) => "<fg=cyan>{$v}</>",
is_string($v) => "<fg=yellow>{$v}</>",
default => $v,
}, $row);
$table->addRow($row);
}
$table->render();
return self::SUCCESS;
}
}