Removed more functional-php usages

This commit is contained in:
Alejandro Celaya
2023-11-30 14:34:21 +01:00
parent 549c6605f0
commit bff4bd12ae
20 changed files with 156 additions and 91 deletions

View File

@@ -14,8 +14,8 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function Functional\filter;
use function Functional\invoke;
use function array_filter;
use function array_map;
use function sprintf;
use function str_contains;
@@ -23,7 +23,7 @@ class DomainRedirectsCommand extends Command
{
public const NAME = 'domain:redirects';
public function __construct(private DomainServiceInterface $domainService)
public function __construct(private readonly DomainServiceInterface $domainService)
{
parent::__construct();
}
@@ -52,9 +52,9 @@ class DomainRedirectsCommand extends Command
$askNewDomain = static fn () => $io->ask('Domain authority for which you want to set specific redirects');
/** @var string[] $availableDomains */
$availableDomains = invoke(
filter($this->domainService->listDomains(), static fn (DomainItem $item) => ! $item->isDefault),
'toString',
$availableDomains = array_map(
static fn (DomainItem $item) => $item->toString(),
array_filter($this->domainService->listDomains(), static fn (DomainItem $item) => ! $item->isDefault),
);
if (empty($availableDomains)) {
$input->setArgument('domain', $askNewDomain());

View File

@@ -8,30 +8,30 @@ use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Output\OutputInterface;
use function Functional\intersperse;
use function array_pop;
final class ShlinkTable
{
private const DEFAULT_STYLE_NAME = 'default';
private const TABLE_TITLE_STYLE = '<options=bold> %s </>';
private function __construct(private readonly Table $baseTable, private readonly bool $withRowSeparators)
private function __construct(private readonly Table $baseTable, private readonly bool $withRowSeparators = false)
{
}
public static function default(OutputInterface $output): self
{
return new self(new Table($output), false);
return new self(new Table($output));
}
public static function withRowSeparators(OutputInterface $output): self
{
return new self(new Table($output), true);
return new self(new Table($output), withRowSeparators: true);
}
public static function fromBaseTable(Table $baseTable): self
{
return new self($baseTable, false);
return new self($baseTable);
}
public function render(array $headers, array $rows, ?string $footerTitle = null, ?string $headerTitle = null): void
@@ -39,7 +39,7 @@ final class ShlinkTable
$style = Table::getStyleDefinition(self::DEFAULT_STYLE_NAME);
$style->setFooterTitleFormat(self::TABLE_TITLE_STYLE)
->setHeaderTitleFormat(self::TABLE_TITLE_STYLE);
$tableRows = $this->withRowSeparators ? intersperse($rows, new TableSeparator()) : $rows;
$tableRows = $this->withRowSeparators ? $this->addRowSeparators($rows) : $rows;
$table = clone $this->baseTable;
$table->setStyle($style)
@@ -49,4 +49,20 @@ final class ShlinkTable
->setHeaderTitle($headerTitle)
->render();
}
private function addRowSeparators(array $rows): array
{
$aggregation = [];
$separator = new TableSeparator();
foreach ($rows as $row) {
$aggregation[] = $row;
$aggregation[] = $separator;
}
// Remove last separator
array_pop($aggregation);
return $aggregation;
}
}