Skip to content

Commit

Permalink
update some for choose logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 9, 2021
1 parent 04b0126 commit 1e14a53
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 18 deletions.
62 changes: 51 additions & 11 deletions src/Component/Interact/Choose.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,54 @@
*/
class Choose extends InteractiveHandle
{
/**
* @var array
*/
protected $data = [];

/**
* @var bool
*/
protected $allowExit = true;

/**
* The default selected key
*
* @var string
*/
protected $default;

/**
* The selected key
*
* @var string
*/
protected $selected;

/**
* @var string
*/
protected $selectedVal;

/**
* Choose one of several options
*
* @param string $description
* @param string|array $options Option data
* e.g
* [
* // option => value
* '1' => 'chengdu',
* '2' => 'beijing'
* ]
* @param string|int $default Default option
* @param bool $allowExit
* @param string $description
* @param string|array $options Option data
* e.g
* [
* // option => value
* '1' => 'chengdu',
* '2' => 'beijing'
* ]
* @param null|int|string $default Default option
* @param bool $allowExit
* @param array $opts
* @psalm-param array{returnVal: bool, retFilter: callable} $opts
*
* @return string
*/
public static function one(string $description, $options, $default = null, bool $allowExit = true): string
public static function one(string $description, $options, $default = null, bool $allowExit = true, array $opts = []): string
{
if (!$description = trim($description)) {
Show::error('Please provide a description text!', 1);
Expand Down Expand Up @@ -71,6 +102,15 @@ public static function one(string $description, $options, $default = null, bool
Console::write("\n Quit,ByeBye.", true, true);
}

// return value
if ($opts['returnVal'] ?? false) {
$r = $options[$r];
}

if ($retFn = $opts['retFilter'] ?? null) {
$r = $retFn($r);
}

return $r;
}
}
21 changes: 21 additions & 0 deletions src/Component/InteractiveHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ abstract class InteractiveHandle
*/
protected $validator;

/**
* @var callable
*/
protected $ansFilter;

/**
* Class constructor.
*
Expand All @@ -48,4 +53,20 @@ public function setValidator(callable $validator): self
$this->validator = $validator;
return $this;
}

/**
* @return callable
*/
public function getAnsFilter(): callable
{
return $this->ansFilter;
}

/**
* @param callable $ansFilter
*/
public function setAnsFilter(callable $ansFilter): void
{
$this->ansFilter = $ansFilter;
}
}
2 changes: 1 addition & 1 deletion src/Concern/StyledOutputAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
*
* @method confirm(string $question, bool $default = true): bool
* @method unConfirm(string $question, bool $default = true): bool
* @method select(string $description, $options, $default = null, bool $allowExit = true): string
* @method string select(string $description, $options, $default = null, bool $allowExit = true, array $opts = [])
* @method checkbox(string $description, $options, $default = null, bool $allowExit = true): array
* @method ask(string $question, string $default = '', Closure $validator = null): string
* @method askPassword(string $prompt = 'Enter Password:'): string
Expand Down
14 changes: 8 additions & 6 deletions src/Util/Interact.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ public static function readFirst($message = null, bool $nl = false): string
*
* @param string $description 说明
* @param string|array $options 选项数据
* @param int|string $default 默认选项
* @param null $default 默认选项
* @param bool $allowExit 有退出选项 默认 true
* @param array $opts
*
* @return string
*/
public static function select(string $description, $options, $default = null, bool $allowExit = true): string
public static function select(string $description, $options, $default = null, bool $allowExit = true, array $opts = []): string
{
return self::choice($description, $options, $default, $allowExit);
return self::choice($description, $options, $default, $allowExit, $opts);
}

/**
Expand All @@ -99,14 +100,15 @@ public static function select(string $description, $options, $default = null, bo
* '1' => 'chengdu',
* '2' => 'beijing'
* ]
* @param string|int $default Default option
* @param null $default Default option
* @param bool $allowExit
* @param array $opts
*
* @return string
*/
public static function choice(string $description, $options, $default = null, bool $allowExit = true): string
public static function choice(string $description, $options, $default = null, bool $allowExit = true, array $opts = []): string
{
return Choose::one($description, $options, $default, $allowExit);
return Choose::one($description, $options, $default, $allowExit, $opts);
}

/**
Expand Down

0 comments on commit 1e14a53

Please sign in to comment.