Skip to content

Instantly share code, notes, and snippets.

@dbu
Created May 9, 2019 11:08
Show Gist options
  • Select an option

  • Save dbu/9f1d67c1d553fdd8001ca1a825003a9b to your computer and use it in GitHub Desktop.

Select an option

Save dbu/9f1d67c1d553fdd8001ca1a825003a9b to your computer and use it in GitHub Desktop.

Revisions

  1. dbu created this gist May 9, 2019.
    100 changes: 100 additions & 0 deletions CommandHelper.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    abstract class CommandHelper
    {
    public static function getStringOption(InputInterface $input, string $name): ?string
    {
    $optionData = $input->getOption($name);

    if (null !== $optionData && !\is_string($optionData)) {
    throw new \InvalidArgumentException(sprintf('Invalid data provided for --%s', $name));
    }

    return $optionData;
    }

    public static function getIntOption(InputInterface $input, string $name): ?int
    {
    $optionData = $input->getOption($name);

    if (null !== $optionData && !\is_numeric($optionData)) {
    throw new \InvalidArgumentException(sprintf('Invalid data provided for --%s', $name));
    }

    return null !== $optionData ? (int) $optionData : null;
    }

    public static function getBoolOption(InputInterface $input, string $name): bool
    {
    $optionData = $input->getOption($name);
    $optionData = filter_var($optionData, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);

    if (!\is_bool($optionData)) {
    throw new \InvalidArgumentException(sprintf('Invalid data provided for --%s', $name));
    }

    return $optionData;
    }

    public static function getArrayOption(InputInterface $input, string $name): array
    {
    $optionData = $input->getOption($name);

    if (null === $optionData) {
    return [];
    }
    if (!\is_array($optionData)) {
    throw new \InvalidArgumentException(sprintf('Invalid data provided for --%s', $name));
    }

    return $optionData;
    }

    /**
    * @return string[] if the option is missing, this returns an empty array
    */
    public static function getCSVOption(InputInterface $input, string $name): array
    {
    $optionData = self::getStringOption($input, $name);
    if (null === $optionData) {
    return [];
    }

    return array_filter(explode(',', $optionData));
    }

    public static function getStringArgument(InputInterface $input, string $name): ?string
    {
    $argData = $input->getArgument($name);
    if (null !== $argData && !\is_string($argData)) {
    throw new \InvalidArgumentException(sprintf('Invalid data provided for argument "%s"', $name));
    }

    return $argData;
    }

    /**
    * @return string[]|null
    */
    public static function getArrayArgument(InputInterface $input, string $name): ?array
    {
    $argData = $input->getArgument($name);
    if (null !== $argData && !\is_array($argData)) {
    throw new \InvalidArgumentException(sprintf('Invalid data provided for argument "%s"', $name));
    }

    return $argData;
    }

    public static function getDateOption(InputInterface $input, string $name): ?\DateTimeImmutable
    {
    $dateString = self::getStringOption($input, $name);
    if (null === $dateString) {
    return null;
    }

    try {
    return new \DateTimeImmutable($dateString);
    } catch (\Exception $exception) {
    throw new \InvalidArgumentException(sprintf('Invalid data provided for argument "%s": %s', $name, $exception->getMessage()));
    }
    }
    }