Skip to content

Commit

Permalink
feat: add an new simple flags parser
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 22, 2021
1 parent f74ec9c commit e90ec31
Show file tree
Hide file tree
Showing 7 changed files with 587 additions and 74 deletions.
14 changes: 7 additions & 7 deletions src/Flag/Flag.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ abstract class Flag implements InputFlagInterface
*
* @var string
*/
private $type = self::TYPE_UNKNOWN;
private $type = FlagType::UNKNOWN;

/**
* The default value
Expand Down Expand Up @@ -119,7 +119,7 @@ public function __construct(string $name, string $desc = '', int $mode = 0, $def
public function init(): void
{
if ($this->isArray()) {
$this->type = self::TYPE_ARRAY;
$this->type = FlagType::ARRAY;
}
}

Expand Down Expand Up @@ -152,19 +152,19 @@ public function setValue($value): void
{
// filter value by type
switch ($this->type) {
case self::TYPE_INT:
case FlagType::INT:
$value = (int)$value;
break;
case self::TYPE_BOOL:
case FlagType::BOOL:
$value = (bool)$value;
break;
case self::TYPE_FLOAT:
case FlagType::FLOAT:
$value = (float)$value;
break;
case self::TYPE_STRING:
case FlagType::STRING:
$value = (string)$value;
break;
// case self::TYPE_ARRAY:
// case FlagType::ARRAY:
// $value = (string)$value;
// break;
default:
Expand Down
131 changes: 131 additions & 0 deletions src/Flag/FlagType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php declare(strict_types=1);


namespace Inhere\Console\Flag;

use function stripos;

/**
* Class FlagConst
* @package Inhere\Console\Flag
*/
class FlagType
{
public const INT = 'int';

public const BOOL = 'bool';

public const FLOAT = 'float';

public const STRING = 'string';

// ------ complex types ------

public const ARRAY = 'array';

public const OBJECT = 'object';

public const CALLABLE = 'callable';

// ------ extend types ------

public const INTS = 'int[]';

public const STRINGS = 'string[]';

public const MIXED = 'mixed';

public const CUSTOM = 'custom';

public const UNKNOWN = 'unknown';

public const ARRAY_TYPES = [
self::ARRAY => 2,
self::INTS => 3,
self::STRINGS => 3,
];

public const TYPES_MAP = [
self::INT => 1,
self::BOOL => 1,
self::FLOAT => 1,
self::STRING => 1,

// ------ complex types ------
self::ARRAY => 2,
self::OBJECT => 2,
self::CALLABLE => 2,

// ------ extend types ------
self::INTS => 3,
self::STRINGS => 3,
self::MIXED => 3,
self::CUSTOM => 3,
self::UNKNOWN => 3,
];

/**
* @param string $type
*
* @return bool
*/
public static function isValid(string $type): bool
{
return isset(self::TYPES_MAP[$type]);
}

/**
* @param string $type
* @param mixed $value
*
* @return bool|float|int|mixed|string
*/
public static function fmtBasicTypeValue(string $type, $value)
{
// filter value by type
switch ($type) {
case self::INT:
case self::INTS:
$value = (int)$value;
break;
case self::BOOL:
$value = (bool)$value;
break;
case self::FLOAT:
$value = (float)$value;
break;
case self::STRING:
case self::STRINGS:
$value = (string)$value;
break;
// case FlagType::ARRAY:
// $value = (string)$value;
// break;
default:
// nothing
break;
}

return $value;
}

// These words will be as a Boolean value
private const TRUE_WORDS = '|on|yes|true|';

private const FALSE_WORDS = '|off|no|false|';

public static function str2bool(string $val): bool
{
// check it is a bool value.
if (false !== stripos(self::TRUE_WORDS, "|$val|")) {
return true;
}

if (false !== stripos(self::FALSE_WORDS, "|$val|")) {
return false;
}

// TODO throws error
return false;
}
}
22 changes: 11 additions & 11 deletions src/Flag/Flags.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function parse(array $args = null): array
}

$this->parsed = true;
$this->rawFlags = $this->args = $args;
$this->rawFlags = $this->rawArgs = $args;

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

// binding remaining args.
if ($this->autoBindArgs && $this->args) {
if ($this->autoBindArgs && $this->rawArgs) {
$this->bindingArguments();
}

Expand All @@ -123,13 +123,13 @@ public function parse(array $args = null): array
*/
protected function parseOne(): array
{
$count = count($this->args);
$count = count($this->rawArgs);
if ($count === 0) {
return [false, self::STATUS_OK];
}

$args = $this->args;
$arg = array_shift($this->args);
$args = $this->rawArgs;
$arg = array_shift($this->rawArgs);

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

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

Expand Down Expand Up @@ -187,16 +187,16 @@ protected function parseOne(): array

$opt->setValue($boolVal);
} else {
if (!$hasVal && count($this->args) > 0) {
if (!$hasVal && count($this->rawArgs) > 0) {
// value is next arg
$hasVal = true;
$ntArg = $this->args[0];
$ntArg = $this->rawArgs[0];

// is not an option value.
if ($ntArg[0] === '-') {
$hasVal = false;
} else {
$value = array_shift($this->args);
$value = array_shift($this->rawArgs);
}
}

Expand Down Expand Up @@ -225,7 +225,7 @@ public function reset(bool $clearDefined = false): void
// clear match results
$this->parsed = false;
$this->matched = [];
$this->rawArgs = $this->args = [];
$this->rawArgs = $this->rawArgs = [];
}

// These words will be as a Boolean value
Expand Down Expand Up @@ -274,7 +274,7 @@ public static function filterBoolV2(string $val): ?bool
*/
public function bindingArguments(): void
{
if (!$this->args) {
if (!$this->rawArgs) {
return;
}

Expand Down
Loading

0 comments on commit e90ec31

Please sign in to comment.