Skip to content

Commit e90ec31

Browse files
committed
feat: add an new simple flags parser
1 parent f74ec9c commit e90ec31

File tree

7 files changed

+587
-74
lines changed

7 files changed

+587
-74
lines changed

src/Flag/Flag.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ abstract class Flag implements InputFlagInterface
6060
*
6161
* @var string
6262
*/
63-
private $type = self::TYPE_UNKNOWN;
63+
private $type = FlagType::UNKNOWN;
6464

6565
/**
6666
* The default value
@@ -119,7 +119,7 @@ public function __construct(string $name, string $desc = '', int $mode = 0, $def
119119
public function init(): void
120120
{
121121
if ($this->isArray()) {
122-
$this->type = self::TYPE_ARRAY;
122+
$this->type = FlagType::ARRAY;
123123
}
124124
}
125125

@@ -152,19 +152,19 @@ public function setValue($value): void
152152
{
153153
// filter value by type
154154
switch ($this->type) {
155-
case self::TYPE_INT:
155+
case FlagType::INT:
156156
$value = (int)$value;
157157
break;
158-
case self::TYPE_BOOL:
158+
case FlagType::BOOL:
159159
$value = (bool)$value;
160160
break;
161-
case self::TYPE_FLOAT:
161+
case FlagType::FLOAT:
162162
$value = (float)$value;
163163
break;
164-
case self::TYPE_STRING:
164+
case FlagType::STRING:
165165
$value = (string)$value;
166166
break;
167-
// case self::TYPE_ARRAY:
167+
// case FlagType::ARRAY:
168168
// $value = (string)$value;
169169
// break;
170170
default:

src/Flag/FlagType.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php declare(strict_types=1);
2+
3+
4+
namespace Inhere\Console\Flag;
5+
6+
use function stripos;
7+
8+
/**
9+
* Class FlagConst
10+
* @package Inhere\Console\Flag
11+
*/
12+
class FlagType
13+
{
14+
public const INT = 'int';
15+
16+
public const BOOL = 'bool';
17+
18+
public const FLOAT = 'float';
19+
20+
public const STRING = 'string';
21+
22+
// ------ complex types ------
23+
24+
public const ARRAY = 'array';
25+
26+
public const OBJECT = 'object';
27+
28+
public const CALLABLE = 'callable';
29+
30+
// ------ extend types ------
31+
32+
public const INTS = 'int[]';
33+
34+
public const STRINGS = 'string[]';
35+
36+
public const MIXED = 'mixed';
37+
38+
public const CUSTOM = 'custom';
39+
40+
public const UNKNOWN = 'unknown';
41+
42+
public const ARRAY_TYPES = [
43+
self::ARRAY => 2,
44+
self::INTS => 3,
45+
self::STRINGS => 3,
46+
];
47+
48+
public const TYPES_MAP = [
49+
self::INT => 1,
50+
self::BOOL => 1,
51+
self::FLOAT => 1,
52+
self::STRING => 1,
53+
54+
// ------ complex types ------
55+
self::ARRAY => 2,
56+
self::OBJECT => 2,
57+
self::CALLABLE => 2,
58+
59+
// ------ extend types ------
60+
self::INTS => 3,
61+
self::STRINGS => 3,
62+
self::MIXED => 3,
63+
self::CUSTOM => 3,
64+
self::UNKNOWN => 3,
65+
];
66+
67+
/**
68+
* @param string $type
69+
*
70+
* @return bool
71+
*/
72+
public static function isValid(string $type): bool
73+
{
74+
return isset(self::TYPES_MAP[$type]);
75+
}
76+
77+
/**
78+
* @param string $type
79+
* @param mixed $value
80+
*
81+
* @return bool|float|int|mixed|string
82+
*/
83+
public static function fmtBasicTypeValue(string $type, $value)
84+
{
85+
// filter value by type
86+
switch ($type) {
87+
case self::INT:
88+
case self::INTS:
89+
$value = (int)$value;
90+
break;
91+
case self::BOOL:
92+
$value = (bool)$value;
93+
break;
94+
case self::FLOAT:
95+
$value = (float)$value;
96+
break;
97+
case self::STRING:
98+
case self::STRINGS:
99+
$value = (string)$value;
100+
break;
101+
// case FlagType::ARRAY:
102+
// $value = (string)$value;
103+
// break;
104+
default:
105+
// nothing
106+
break;
107+
}
108+
109+
return $value;
110+
}
111+
112+
// These words will be as a Boolean value
113+
private const TRUE_WORDS = '|on|yes|true|';
114+
115+
private const FALSE_WORDS = '|off|no|false|';
116+
117+
public static function str2bool(string $val): bool
118+
{
119+
// check it is a bool value.
120+
if (false !== stripos(self::TRUE_WORDS, "|$val|")) {
121+
return true;
122+
}
123+
124+
if (false !== stripos(self::FALSE_WORDS, "|$val|")) {
125+
return false;
126+
}
127+
128+
// TODO throws error
129+
return false;
130+
}
131+
}

src/Flag/Flags.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function parse(array $args = null): array
9191
}
9292

9393
$this->parsed = true;
94-
$this->rawFlags = $this->args = $args;
94+
$this->rawFlags = $this->rawArgs = $args;
9595

9696
while (true) {
9797
[$goon, $status] = $this->parseOne();
@@ -105,7 +105,7 @@ public function parse(array $args = null): array
105105
}
106106

107107
// binding remaining args.
108-
if ($this->autoBindArgs && $this->args) {
108+
if ($this->autoBindArgs && $this->rawArgs) {
109109
$this->bindingArguments();
110110
}
111111

@@ -123,13 +123,13 @@ public function parse(array $args = null): array
123123
*/
124124
protected function parseOne(): array
125125
{
126-
$count = count($this->args);
126+
$count = count($this->rawArgs);
127127
if ($count === 0) {
128128
return [false, self::STATUS_OK];
129129
}
130130

131-
$args = $this->args;
132-
$arg = array_shift($this->args);
131+
$args = $this->rawArgs;
132+
$arg = array_shift($this->rawArgs);
133133

134134
// empty, continue.
135135
if ('' === $arg) {
@@ -138,7 +138,7 @@ protected function parseOne(): array
138138

139139
// is not an option flag. exit.
140140
if ($arg[0] !== '-') {
141-
$this->args = $args; // revert args on exit
141+
$this->rawArgs = $args; // revert args on exit
142142
return [false, self::STATUS_OK];
143143
}
144144

@@ -187,16 +187,16 @@ protected function parseOne(): array
187187

188188
$opt->setValue($boolVal);
189189
} else {
190-
if (!$hasVal && count($this->args) > 0) {
190+
if (!$hasVal && count($this->rawArgs) > 0) {
191191
// value is next arg
192192
$hasVal = true;
193-
$ntArg = $this->args[0];
193+
$ntArg = $this->rawArgs[0];
194194

195195
// is not an option value.
196196
if ($ntArg[0] === '-') {
197197
$hasVal = false;
198198
} else {
199-
$value = array_shift($this->args);
199+
$value = array_shift($this->rawArgs);
200200
}
201201
}
202202

@@ -225,7 +225,7 @@ public function reset(bool $clearDefined = false): void
225225
// clear match results
226226
$this->parsed = false;
227227
$this->matched = [];
228-
$this->rawArgs = $this->args = [];
228+
$this->rawArgs = $this->rawArgs = [];
229229
}
230230

231231
// These words will be as a Boolean value
@@ -274,7 +274,7 @@ public static function filterBoolV2(string $val): ?bool
274274
*/
275275
public function bindingArguments(): void
276276
{
277-
if (!$this->args) {
277+
if (!$this->rawArgs) {
278278
return;
279279
}
280280

0 commit comments

Comments
 (0)