Skip to content

Commit 1e14a53

Browse files
committed
update some for choose logic
1 parent 04b0126 commit 1e14a53

File tree

4 files changed

+81
-18
lines changed

4 files changed

+81
-18
lines changed

src/Component/Interact/Choose.php

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,54 @@
1717
*/
1818
class Choose extends InteractiveHandle
1919
{
20+
/**
21+
* @var array
22+
*/
23+
protected $data = [];
24+
25+
/**
26+
* @var bool
27+
*/
28+
protected $allowExit = true;
29+
30+
/**
31+
* The default selected key
32+
*
33+
* @var string
34+
*/
35+
protected $default;
36+
37+
/**
38+
* The selected key
39+
*
40+
* @var string
41+
*/
42+
protected $selected;
43+
44+
/**
45+
* @var string
46+
*/
47+
protected $selectedVal;
48+
2049
/**
2150
* Choose one of several options
2251
*
23-
* @param string $description
24-
* @param string|array $options Option data
25-
* e.g
26-
* [
27-
* // option => value
28-
* '1' => 'chengdu',
29-
* '2' => 'beijing'
30-
* ]
31-
* @param string|int $default Default option
32-
* @param bool $allowExit
52+
* @param string $description
53+
* @param string|array $options Option data
54+
* e.g
55+
* [
56+
* // option => value
57+
* '1' => 'chengdu',
58+
* '2' => 'beijing'
59+
* ]
60+
* @param null|int|string $default Default option
61+
* @param bool $allowExit
62+
* @param array $opts
63+
* @psalm-param array{returnVal: bool, retFilter: callable} $opts
3364
*
3465
* @return string
3566
*/
36-
public static function one(string $description, $options, $default = null, bool $allowExit = true): string
67+
public static function one(string $description, $options, $default = null, bool $allowExit = true, array $opts = []): string
3768
{
3869
if (!$description = trim($description)) {
3970
Show::error('Please provide a description text!', 1);
@@ -71,6 +102,15 @@ public static function one(string $description, $options, $default = null, bool
71102
Console::write("\n Quit,ByeBye.", true, true);
72103
}
73104

105+
// return value
106+
if ($opts['returnVal'] ?? false) {
107+
$r = $options[$r];
108+
}
109+
110+
if ($retFn = $opts['retFilter'] ?? null) {
111+
$r = $retFn($r);
112+
}
113+
74114
return $r;
75115
}
76116
}

src/Component/InteractiveHandle.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ abstract class InteractiveHandle
2828
*/
2929
protected $validator;
3030

31+
/**
32+
* @var callable
33+
*/
34+
protected $ansFilter;
35+
3136
/**
3237
* Class constructor.
3338
*
@@ -48,4 +53,20 @@ public function setValidator(callable $validator): self
4853
$this->validator = $validator;
4954
return $this;
5055
}
56+
57+
/**
58+
* @return callable
59+
*/
60+
public function getAnsFilter(): callable
61+
{
62+
return $this->ansFilter;
63+
}
64+
65+
/**
66+
* @param callable $ansFilter
67+
*/
68+
public function setAnsFilter(callable $ansFilter): void
69+
{
70+
$this->ansFilter = $ansFilter;
71+
}
5172
}

src/Concern/StyledOutputAwareTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
*
5656
* @method confirm(string $question, bool $default = true): bool
5757
* @method unConfirm(string $question, bool $default = true): bool
58-
* @method select(string $description, $options, $default = null, bool $allowExit = true): string
58+
* @method string select(string $description, $options, $default = null, bool $allowExit = true, array $opts = [])
5959
* @method checkbox(string $description, $options, $default = null, bool $allowExit = true): array
6060
* @method ask(string $question, string $default = '', Closure $validator = null): string
6161
* @method askPassword(string $prompt = 'Enter Password:'): string

src/Util/Interact.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,15 @@ public static function readFirst($message = null, bool $nl = false): string
7878
*
7979
* @param string $description 说明
8080
* @param string|array $options 选项数据
81-
* @param int|string $default 默认选项
81+
* @param null $default 默认选项
8282
* @param bool $allowExit 有退出选项 默认 true
83+
* @param array $opts
8384
*
8485
* @return string
8586
*/
86-
public static function select(string $description, $options, $default = null, bool $allowExit = true): string
87+
public static function select(string $description, $options, $default = null, bool $allowExit = true, array $opts = []): string
8788
{
88-
return self::choice($description, $options, $default, $allowExit);
89+
return self::choice($description, $options, $default, $allowExit, $opts);
8990
}
9091

9192
/**
@@ -99,14 +100,15 @@ public static function select(string $description, $options, $default = null, bo
99100
* '1' => 'chengdu',
100101
* '2' => 'beijing'
101102
* ]
102-
* @param string|int $default Default option
103+
* @param null $default Default option
103104
* @param bool $allowExit
105+
* @param array $opts
104106
*
105107
* @return string
106108
*/
107-
public static function choice(string $description, $options, $default = null, bool $allowExit = true): string
109+
public static function choice(string $description, $options, $default = null, bool $allowExit = true, array $opts = []): string
108110
{
109-
return Choose::one($description, $options, $default, $allowExit);
111+
return Choose::one($description, $options, $default, $allowExit, $opts);
110112
}
111113

112114
/**

0 commit comments

Comments
 (0)