Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 10 additions & 1 deletion src/Console/Traits/ArrayOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
20 changes: 20 additions & 0 deletions src/Console/Traits/DryRun.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace AlwaysOpen\Sidekick\Console\Traits;

trait DryRun
{
public const string DRYRUN_SIGNATURE = ' {--dry-run : Whether to discard changes}';

public function initializeDryRun(): void
{
if (! str_contains($this->signature, '--dry-run')) {
Comment thread
qschmick marked this conversation as resolved.
$this->signature .= self::DRYRUN_SIGNATURE;
}
}

protected function isDryRun(): bool
{
return $this->option('dry-run');
}
}
30 changes: 30 additions & 0 deletions src/Console/Traits/EnumOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace AlwaysOpen\Sidekick\Console\Traits;

use InvalidArgumentException;

trait EnumOption
{
/**
* @param string $key
* @param array $options
* @param mixed|null $default
*
* @return array|string|bool|null
*/
protected function enumOption(string $key, array $options, mixed $default = null): array|string|bool|null
{
$value = $this->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;
}
}
37 changes: 37 additions & 0 deletions src/Console/Traits/NumericOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace AlwaysOpen\Sidekick\Console\Traits;

use InvalidArgumentException;

trait NumericOption
{
Comment thread
qschmick marked this conversation as resolved.
/**
* Retrieve an option value and cast it to a numeric type.
*
* @param string $key The option key to retrieve.
* @param string $type The numeric type to cast to ('int' or 'float'). Defaults to 'int'.
* @return int|float|null The numeric value of the option, or null if not set.
* @throws InvalidArgumentException If the value is not numeric or if the type is invalid.
*/
protected function numericOption(string $key, string $type = 'int'): int|float|null
{
$value = $this->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;
}
}
31 changes: 31 additions & 0 deletions src/Console/Traits/RequiredEnumOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace AlwaysOpen\Sidekick\Console\Traits;

use InvalidArgumentException;

trait RequiredEnumOption
{
/**
* @param string $key
* @param array $options
*
* @throws InvalidArgumentException
*
* @return array|string|bool
*/
protected function requiredEnumOption(string $key, array $options): array|string|bool
{
$value = $this->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;
}
}
35 changes: 35 additions & 0 deletions src/Console/Traits/RequiredNumericOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace AlwaysOpen\Sidekick\Console\Traits;

use InvalidArgumentException;

trait RequiredNumericOption
{
/**
* @param string $key
* @param string $type
*
* @return int|float
*/
protected function requiredNumericOption(string $key, string $type = 'int'): int|float
{
$value = $this->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;
}
}
8 changes: 5 additions & 3 deletions src/Console/Traits/RequiredOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down