Skip to content

Commit 190cf6f

Browse files
committed
up: update some for string to bool value
1 parent 2fe57cd commit 190cf6f

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/FlagType.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Toolkit\PFlag;
1111

12+
use Toolkit\PFlag\Exception\FlagException;
1213
use Toolkit\Stdlib\Str;
1314
use function is_scalar;
1415
use function is_string;
@@ -127,7 +128,7 @@ public static function getDefault(string $type): float|bool|int|array|string|nul
127128
return match ($type) {
128129
self::INT => 0,
129130
self::BOOL => false,
130-
self::FLOAT => (float)0,
131+
self::FLOAT => 0.0,
131132
self::STRING => '',
132133
self::INTS, self::ARRAY, self::STRINGS => [],
133134
default => null,
@@ -146,10 +147,20 @@ public static function fmtBasicTypeValue(string $type, mixed $value): mixed
146147
return $value;
147148
}
148149

150+
// convert to bool
151+
if ($type === self::BOOL) {
152+
$value = is_string($value) ? Str::tryToBool($value) : (bool)$value;
153+
154+
if (is_string($value)) {
155+
throw new FlagException("convert value '$value' to bool failed");
156+
}
157+
return $value;
158+
}
159+
149160
// format value by type
150161
return match ($type) {
151162
self::INT, self::INTS => (int)$value,
152-
self::BOOL => is_string($value) ? Str::toBool2($value) : (bool)$value,
163+
// self::BOOL => is_string($value) ? Str::toBool2($value) : (bool)$value,
153164
self::FLOAT => (float)$value,
154165
self::STRING, self::STRINGS => (string)$value,
155166
default => $value,

src/FlagsParser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,11 +504,14 @@ public function popFirstRawArg(): string
504504
}
505505

506506
/**
507+
* @param bool $more
508+
*
507509
* @return array
508510
*/
509511
public function getInfo(bool $more = false): array
510512
{
511513
$info = [
514+
'driver' => static::class,
512515
'flags' => $this->flags,
513516
'rawArgs' => $this->rawArgs,
514517
'remainArgs' => $this->remainArgs,

0 commit comments

Comments
 (0)