Skip to content

Commit

Permalink
up: update some for string to bool value
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 12, 2022
1 parent 2fe57cd commit 190cf6f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/FlagType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Toolkit\PFlag;

use Toolkit\PFlag\Exception\FlagException;
use Toolkit\Stdlib\Str;
use function is_scalar;
use function is_string;
Expand Down Expand Up @@ -127,7 +128,7 @@ public static function getDefault(string $type): float|bool|int|array|string|nul
return match ($type) {
self::INT => 0,
self::BOOL => false,
self::FLOAT => (float)0,
self::FLOAT => 0.0,
self::STRING => '',
self::INTS, self::ARRAY, self::STRINGS => [],
default => null,
Expand All @@ -146,10 +147,20 @@ public static function fmtBasicTypeValue(string $type, mixed $value): mixed
return $value;
}

// convert to bool
if ($type === self::BOOL) {
$value = is_string($value) ? Str::tryToBool($value) : (bool)$value;

if (is_string($value)) {
throw new FlagException("convert value '$value' to bool failed");
}
return $value;
}

// format value by type
return match ($type) {
self::INT, self::INTS => (int)$value,
self::BOOL => is_string($value) ? Str::toBool2($value) : (bool)$value,
// self::BOOL => is_string($value) ? Str::toBool2($value) : (bool)$value,
self::FLOAT => (float)$value,
self::STRING, self::STRINGS => (string)$value,
default => $value,
Expand Down
3 changes: 3 additions & 0 deletions src/FlagsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,14 @@ public function popFirstRawArg(): string
}

/**
* @param bool $more
*
* @return array
*/
public function getInfo(bool $more = false): array
{
$info = [
'driver' => static::class,
'flags' => $this->flags,
'rawArgs' => $this->rawArgs,
'remainArgs' => $this->remainArgs,
Expand Down

0 comments on commit 190cf6f

Please sign in to comment.