diff --git a/README.md b/README.md index 250b749..a79d0e7 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ composer require always-open/sidekick |------------------|-----------------| | 1.x | PHP 7.4 and 8.x | | 4.x | PHP 8.x+ | +| 5.x | PHP 8.2+ | +| 6.x | PHP 8.3+ | ## Usage diff --git a/composer.json b/composer.json index 915bfdf..c44bb50 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "php": "^8.2.0|^8.3.0|^8.4.0", + "php": "^8.3.0|^8.4.0", "illuminate/contracts": "^11.0|^12.0", "jdorn/sql-formatter": "^1.2", "league/flysystem-aws-s3-v3": "^3.29", diff --git a/src/Console/Traits/ArrayOption.php b/src/Console/Traits/ArrayOption.php index 22bdd1b..bfb8f25 100644 --- a/src/Console/Traits/ArrayOption.php +++ b/src/Console/Traits/ArrayOption.php @@ -12,6 +12,15 @@ trait ArrayOption */ protected function arrayOption(string $key, string $delimiter = ','): array { - return array_filter(explode($delimiter, $this->option($key) ?? '')); + $val = $this->option($key); + if (null === $val) { + return []; + } + + if (is_array($val)) { + return $val; + } + + return array_filter(explode($delimiter, $val)); } } diff --git a/src/Console/Traits/DryRun.php b/src/Console/Traits/DryRun.php new file mode 100644 index 0000000..1d271f7 --- /dev/null +++ b/src/Console/Traits/DryRun.php @@ -0,0 +1,20 @@ +signature, '--dry-run')) { + $this->signature .= self::DRYRUN_SIGNATURE; + } + } + + protected function isDryRun(): bool + { + return $this->option('dry-run'); + } +} diff --git a/src/Console/Traits/EnumOption.php b/src/Console/Traits/EnumOption.php new file mode 100644 index 0000000..088cc98 --- /dev/null +++ b/src/Console/Traits/EnumOption.php @@ -0,0 +1,30 @@ +option($key); + + if (null === $value) { + return $default; + } + + if (! in_array($value, $options, true)) { + throw new InvalidArgumentException($key . ' must be one of ' . implode(', ', $options) . '.'); + } + + return $value; + } +} diff --git a/src/Console/Traits/NumericOption.php b/src/Console/Traits/NumericOption.php new file mode 100644 index 0000000..944f1eb --- /dev/null +++ b/src/Console/Traits/NumericOption.php @@ -0,0 +1,37 @@ +option($key); + + if (null === $value) { + return $value; + } + + if (! is_numeric($value)) { + throw new InvalidArgumentException($key . ' must be a number.'); + } + + if (! in_array($type, ['int', 'float'])) { + throw new InvalidArgumentException($type . ' is not valid, must be one of "int", "float".'); + } + + settype($value, $type); + + return $value; + } +} diff --git a/src/Console/Traits/RequiredEnumOption.php b/src/Console/Traits/RequiredEnumOption.php new file mode 100644 index 0000000..ebf7aac --- /dev/null +++ b/src/Console/Traits/RequiredEnumOption.php @@ -0,0 +1,31 @@ +option($key); + + if (null === $value) { + throw new InvalidArgumentException($key . ' is required'); + } + + if (! in_array($value, $options, true)) { + throw new InvalidArgumentException($key . ' must be one of ' . implode(', ', $options) . '.'); + } + + return $value; + } +} diff --git a/src/Console/Traits/RequiredNumericOption.php b/src/Console/Traits/RequiredNumericOption.php new file mode 100644 index 0000000..32b71e9 --- /dev/null +++ b/src/Console/Traits/RequiredNumericOption.php @@ -0,0 +1,35 @@ +option($key); + + if (null === $value) { + throw new InvalidArgumentException($key . ' is required'); + } + + if (! is_numeric($value)) { + throw new InvalidArgumentException($key . ' must be a number.'); + } + + if (! in_array($type, ['int', 'float'])) { + throw new InvalidArgumentException($type . ' is not valid, must be one of "int", "float".'); + } + + settype($value, $type); + + return $value; + } +} diff --git a/src/Console/Traits/RequiredOption.php b/src/Console/Traits/RequiredOption.php index 00bda18..1a2af8a 100644 --- a/src/Console/Traits/RequiredOption.php +++ b/src/Console/Traits/RequiredOption.php @@ -2,21 +2,23 @@ namespace AlwaysOpen\Sidekick\Console\Traits; +use InvalidArgumentException; + trait RequiredOption { /** * @param string $key * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException * * @return array|string|bool */ - protected function requiredOption(string $key) + protected function requiredOption(string $key): array|string|bool { $value = $this->option($key); if (null === $value) { - throw new \InvalidArgumentException($key . ' is required'); + throw new InvalidArgumentException($key . ' is required'); } return $value;