Created
          May 9, 2019 11:08 
        
      - 
      
- 
        Save dbu/9f1d67c1d553fdd8001ca1a825003a9b to your computer and use it in GitHub Desktop. 
    typed options and arguments for symfony console
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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())); | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
A nicer option would be to decorate the $input with a class providing these methods. that class could then even be used in
bin/console