src/Cli.php
protected function validateType(&$value, $type, $name = '', $def = null) {
        switch ($type) {
            case 'boolean':
                if (is_bool($value)) {
                    $valid = true;
                } elseif ($value === 0) {
                    // 0 doesn't work well with in_array() so check it separately.
                    $value = false;
                    $valid = true;
                } elseif (in_array($value, [null, '', '0', 'false', 'no', 'disabled'])) {
                    $value = false;
                    $valid = true;
                } elseif (in_array($value, [1, '1', 'true', 'yes', 'enabled'])) {
                    $value = true;
                    $valid = true;
                } else {
                    $valid = false;
                }
                break;
            case 'integer':
                if (is_numeric($value)) {
                    $value = (int)$value;
                    $valid = true;
                } else {
                    $valid = false;
                }
                break;
            case 'string':
                $value = (string)$value;
                $valid = true;
                break;
            default:
                throw new \Exception("Unknown type: $type.", 400);
        }

        if (!$valid && $name) {
            $short = static::val('short', (array)$def);
            $nameStr = "--$name".($short ? " (-$short)" : '');
            echo $this->red("The value of $nameStr is not a valid $type.".PHP_EOL);
        }

        return $valid;
    }
protected function validateType(&$value, $type, $name = '', $def = null) {
        switch ($type) {
            case 'boolean':
                if (is_bool($value)) {
                    $valid = true;
                } elseif ($value === 0) {
                    // 0 doesn't work well with in_array() so check it separately.
                    $value = false;
                    $valid = true;
                } elseif (in_array($value, [null, '', '0', 'false', 'no', 'disabled'])) {
                    $value = false;
                    $valid = true;
                } elseif (in_array($value, [1, '1', 'true', 'yes', 'enabled'])) {
                    $value = true;
                    $valid = true;
                } else {
                    $valid = false;
                }
                break;
            case 'integer':
                if (is_numeric($value)) {
                    $value = (int)$value;
                    $valid = true;
                } else {
                    $valid = false;
                }
                break;
            case 'string':
                $value = (string)$value;
                $valid = true;
                break;
            default:
                throw new \Exception("Unknown type: $type.", 400);
        }

        if (!$valid && $name) {
            $short = static::val('short', (array)$def);
            $nameStr = "--$name".($short ? " (-$short)" : '');
            echo $this->red("The value of $nameStr is not a valid $type.".PHP_EOL);
        }

        return $valid;
    }
protected function validateType ( & $ value , $ type , $ name = '' , $ def = null ) { switch ( $ type ) { case 'boolean' : if ( is_bool ( $ value ) ) { $ valid = true ; } elseif ( $ value === 0 ) { // 0 doesn't work well with in_array() so check it separately. $ value = false ; $ valid = true ; } elseif ( in_array ( $ value , [ null , '' , '0' , 'false' , 'no' , 'disabled' ] ) ) { $ value = false ; $ valid = true ; } elseif ( in_array ( $ value , [ 1 , '1' , 'true' , 'yes' , 'enabled' ] ) ) { $ value = true ; $ valid = true ; } else { $ valid = false ; } break ; case 'integer' : if ( is_numeric ( $ value ) ) { $ value = ( int ) $ value ; $ valid = true ; } else { $ valid = false ; } break ; case 'string' : $ value = ( string ) $ value ; $ valid = true ; break ; default : throw new \ Exception ( "Unknown type: $type." , 400 ) ; } if ( ! $ valid && $ name ) { $ short = static :: val ( 'short' , ( array ) $ def ) ; $ nameStr = "--$name" . ( $ short ? " (-$short)" : '' ) ; echo $ this -> red ( "The value of $nameStr is not a valid $type." . PHP_EOL ) ; } return $ valid ; }
Validate the type of a value and coerce it into the proper type.

@param mixed &$value The value to validate.
@param string $type One of: bool, int, string.
@param string $name The name of the option if you want to print an error message.
@param array|null $def The option def if you want to print an error message.
@return bool Returns `true` if the value is the correct type.
@throws \Exception Throws an exception when {@see $type} is not a known value.
Validate the type of a value and coerce it into the proper type .