Prevent resetting of non-providen params in EditShortUrlCommand

This commit is contained in:
Alejandro Celaya 2024-07-26 19:26:48 +02:00
parent 5bccdded8a
commit 65ea1e00a6

View File

@ -102,18 +102,34 @@ readonly final class ShortUrlDataInput
private function getCommonData(InputInterface $input): array private function getCommonData(InputInterface $input): array
{ {
$longUrl = $this->longUrlAsOption ? $input->getOption('long-url') : $input->getArgument('longUrl'); $longUrl = $this->longUrlAsOption ? $input->getOption('long-url') : $input->getArgument('longUrl');
$tags = array_unique(flatten(array_map(splitByComma(...), $input->getOption('tags')))); $data = [ShortUrlInputFilter::LONG_URL => $longUrl];
$maxVisits = $input->getOption('max-visits');
return [ // Avoid setting arguments that were not explicitly provided.
ShortUrlInputFilter::LONG_URL => $longUrl, // This is important when editing short URLs and should not make a difference when creating.
ShortUrlInputFilter::VALID_SINCE => $input->getOption('valid-since'), if ($input->hasParameterOption(['--valid-since', '-s'])) {
ShortUrlInputFilter::VALID_UNTIL => $input->getOption('valid-until'), $data[ShortUrlInputFilter::VALID_SINCE] = $input->getOption('valid-since');
ShortUrlInputFilter::MAX_VISITS => $maxVisits !== null ? (int) $maxVisits : null, }
ShortUrlInputFilter::TAGS => $tags, if ($input->hasParameterOption(['--valid-until', '-v'])) {
ShortUrlInputFilter::TITLE => $input->getOption('title'), $data[ShortUrlInputFilter::VALID_UNTIL] = $input->getOption('valid-until');
ShortUrlInputFilter::CRAWLABLE => $input->getOption('crawlable'), }
ShortUrlInputFilter::FORWARD_QUERY => !$input->getOption('no-forward-query'), if ($input->hasParameterOption(['--max-visits', '-m'])) {
]; $maxVisits = $input->getOption('max-visits');
$data[ShortUrlInputFilter::MAX_VISITS] = $maxVisits !== null ? (int) $maxVisits : null;
}
if ($input->hasParameterOption(['--tags', '-t'])) {
$tags = array_unique(flatten(array_map(splitByComma(...), $input->getOption('tags'))));
$data[ShortUrlInputFilter::TAGS] = $tags;
}
if ($input->hasParameterOption('--title')) {
$data[ShortUrlInputFilter::TITLE] = $input->getOption('title');
}
if ($input->hasParameterOption(['--crawlable', '-r'])) {
$data[ShortUrlInputFilter::CRAWLABLE] = $input->getOption('crawlable');
}
if ($input->hasParameterOption(['--no-forward-query', '-w'])) {
$data[ShortUrlInputFilter::FORWARD_QUERY] = !$input->getOption('no-forward-query');
}
return $data;
} }
} }