Make tag and exclude-tag trully optional in ShortUrlsParamsInput

This commit is contained in:
Alejandro Celaya
2025-12-15 08:57:54 +01:00
parent 7cdefcb4b6
commit b7ae228a95
3 changed files with 34 additions and 20 deletions

View File

@@ -45,16 +45,16 @@ final class ShortUrlsParamsInput
)]
public string|null $domain = null;
/** @var string[] */
/** @var string[]|null */
#[Option('A list of tags that short URLs need to include', name: 'tag', shortcut: 't')]
public array $tags = [];
public array|null $tags = null;
#[Option('If --tag is provided, returns only short URLs including ALL of them')]
public bool $tagsAll = false;
/** @var string[] */
/** @var string[]|null */
#[Option('A list of tags that short URLs should NOT include', name: 'exclude-tag', shortcut: 'et')]
public array $excludeTags = [];
public array|null $excludeTags = null;
#[Option('If --exclude-tag is provided, returns only short URLs not including ANY of them')]
public bool $excludeTagsAll = false;
@@ -88,17 +88,10 @@ final class ShortUrlsParamsInput
public function toArray(OutputInterface $output): array
{
$tagsMode = $this->tagsAll ? TagsMode::ALL->value : TagsMode::ANY->value;
$excludeTagsMode = $this->excludeTagsAll ? TagsMode::ALL->value : TagsMode::ANY->value;
$data = [
ShortUrlsParamsInputFilter::PAGE => $this->page,
ShortUrlsParamsInputFilter::SEARCH_TERM => $this->searchTerm,
ShortUrlsParamsInputFilter::DOMAIN => $this->domain,
ShortUrlsParamsInputFilter::TAGS => array_unique($this->tags),
ShortUrlsParamsInputFilter::TAGS_MODE => $tagsMode,
ShortUrlsParamsInputFilter::EXCLUDE_TAGS => array_unique($this->excludeTags),
ShortUrlsParamsInputFilter::EXCLUDE_TAGS_MODE => $excludeTagsMode,
ShortUrlsParamsInputFilter::ORDER_BY => $this->orderBy,
ShortUrlsParamsInputFilter::START_DATE => InputUtils::processDate('start-date', $this->startDate, $output),
ShortUrlsParamsInputFilter::END_DATE => InputUtils::processDate('end-date', $this->endDate, $output),
@@ -107,6 +100,18 @@ final class ShortUrlsParamsInput
ShortUrlsParamsInputFilter::API_KEY_NAME => $this->apiKeyName,
];
if ($this->tags !== null) {
$tagsMode = $this->tagsAll ? TagsMode::ALL : TagsMode::ANY;
$data[ShortUrlsParamsInputFilter::TAGS_MODE] = $tagsMode->value;
$data[ShortUrlsParamsInputFilter::TAGS] = array_unique($this->tags);
}
if ($this->excludeTags !== null) {
$excludeTagsMode = $this->excludeTagsAll ? TagsMode::ALL : TagsMode::ANY;
$data[ShortUrlsParamsInputFilter::EXCLUDE_TAGS_MODE] = $excludeTagsMode->value;
$data[ShortUrlsParamsInputFilter::EXCLUDE_TAGS] = array_unique($this->excludeTags);
}
if ($this->all) {
$data[ShortUrlsParamsInputFilter::ITEMS_PER_PAGE] = Paginator::ALL_ITEMS;
}

View File

@@ -15,7 +15,7 @@ use Symfony\Component\Console\Input\InputOption;
final readonly class ShortUrlDataInput
{
private readonly TagsOption $tagsOption;
private TagsOption $tagsOption;
public function __construct(Command $command, private bool $longUrlAsOption = false)
{

View File

@@ -203,25 +203,34 @@ class ListShortUrlsCommandTest extends TestCase
array $commandArgs,
int|null $page,
string|null $searchTerm,
array $tags,
array|null $tags,
string $tagsMode,
string|null $startDate = null,
string|null $endDate = null,
array $excludeTags = [],
array|null $excludeTags = null,
string $excludeTagsMode = TagsMode::ANY->value,
string|null $apiKeyName = null,
): void {
$this->shortUrlService->expects($this->once())->method('listShortUrls')->with(ShortUrlsParams::fromRawData([
$expectedData = [
'page' => $page,
'searchTerm' => $searchTerm,
'tags' => $tags,
'tagsMode' => $tagsMode,
'startDate' => $startDate !== null ? Chronos::parse($startDate)->toAtomString() : null,
'endDate' => $endDate !== null ? Chronos::parse($endDate)->toAtomString() : null,
'excludeTags' => $excludeTags,
'excludeTagsMode' => $excludeTagsMode,
'apiKeyName' => $apiKeyName,
]))->willReturn(new Paginator(new ArrayAdapter([])));
];
if ($tags !== null) {
$expectedData['tags'] = $tags;
}
if ($excludeTags !== null) {
$expectedData['excludeTags'] = $excludeTags;
}
$this->shortUrlService->expects($this->once())->method('listShortUrls')->with(ShortUrlsParams::fromRawData(
$expectedData,
))->willReturn(new Paginator(new ArrayAdapter([])));
$this->commandTester->setInputs(['n']);
$this->commandTester->execute($commandArgs);
@@ -231,7 +240,7 @@ class ListShortUrlsCommandTest extends TestCase
{
yield [[], 1, null, [], TagsMode::ANY->value];
yield [['--page' => $page = 3], $page, null, [], TagsMode::ANY->value];
yield [['--tags-all' => true], 1, null, [], TagsMode::ALL->value];
yield [['--tags-all' => true, '--tag' => ['foo']], 1, null, ['foo'], TagsMode::ALL->value];
yield [['--search-term' => $searchTerm = 'search this'], 1, $searchTerm, [], TagsMode::ANY->value];
yield [
['--page' => $page = 3, '--search-term' => $searchTerm = 'search this', '--tag' => $tags = ['foo', 'bar']],
@@ -270,7 +279,7 @@ class ListShortUrlsCommandTest extends TestCase
['--exclude-tag' => ['foo', 'bar'], '--exclude-tags-all' => true],
1,
null,
[],
null,
TagsMode::ANY->value,
null,
null,