protected function askForMixed($key, $required = false, $default = null, array $values = []) {
        $question = sprintf(
            'What is the value for configuration setting "%s"',
            $key
        );

        $options = [];

        if (count($values) > 0) {
            foreach ($values as $value) {
                switch (gettype($value)) {
                    case 'boolean':
                        $options[] = ($value) ? 'yes' : 'no';
                        break;
                    default:
                        $options[] = $value;
                        break;
                }
            }

            $question .= sprintf(
                ' (%s)',
                implode(', ', $options)
            );
        }

        $question .= '?';

        if (isset($default)) {
            switch (gettype($default)) {
                case 'boolean':
                    $question .= sprintf(' [%s]:', ($default) ? 'yes' : 'no');
                    break;
                default:
                    $question .= sprintf(' [%s]:', $default);
                    break;
            }
        }

        $answer = trim(IO::in($question));

        switch ($answer) {
            case 'yes':
            case 'y':
            case '1':
            case 'true':
                return true;
            case 'no':
            case 'n':
            case '0':
            case 'false':
                return false;
            case '':
                if (isset($default)) {
                    return $default;
                } elseif ($required) {
                    $this->log->warning('Excuse me, what do you mean?');

                    return $this->askForMixed($key, $required, $default, $values);
                }
                break;
            default:
                if (count($values) && !in_array($answer, $values)) {
                    $this->log->warning(
                        'Wrong answer. Here are your options: %s',
                        implode(', ', $options)
                    );

                    return $this->askForMixed($key, $required, $default, $values);
                }

                return $answer;
        }

        return null;
    }
protected function askForMixed($key, $required = false, $default = null, array $values = []) {
        $question = sprintf(
            'What is the value for configuration setting "%s"',
            $key
        );

        $options = [];

        if (count($values) > 0) {
            foreach ($values as $value) {
                switch (gettype($value)) {
                    case 'boolean':
                        $options[] = ($value) ? 'yes' : 'no';
                        break;
                    default:
                        $options[] = $value;
                        break;
                }
            }

            $question .= sprintf(
                ' (%s)',
                implode(', ', $options)
            );
        }

        $question .= '?';

        if (isset($default)) {
            switch (gettype($default)) {
                case 'boolean':
                    $question .= sprintf(' [%s]:', ($default) ? 'yes' : 'no');
                    break;
                default:
                    $question .= sprintf(' [%s]:', $default);
                    break;
            }
        }

        $answer = trim(IO::in($question));

        switch ($answer) {
            case 'yes':
            case 'y':
            case '1':
            case 'true':
                return true;
            case 'no':
            case 'n':
            case '0':
            case 'false':
                return false;
            case '':
                if (isset($default)) {
                    return $default;
                } elseif ($required) {
                    $this->log->warning('Excuse me, what do you mean?');

                    return $this->askForMixed($key, $required, $default, $values);
                }
                break;
            default:
                if (count($values) && !in_array($answer, $values)) {
                    $this->log->warning(
                        'Wrong answer. Here are your options: %s',
                        implode(', ', $options)
                    );

                    return $this->askForMixed($key, $required, $default, $values);
                }

                return $answer;
        }

        return null;
    }
protected function askForMixed ( $ key , $ required = false , $ default = null , array $ values = [ ] ) { $ question = sprintf ( 'What is the value for configuration setting "%s"' , $ key ) ; $ options = [ ] ; if ( count ( $ values ) > 0 ) { foreach ( $ values as $ value ) { switch ( gettype ( $ value ) ) { case 'boolean' : $ options [ ] = ( $ value ) ? 'yes' : 'no' ; break ; default : $ options [ ] = $ value ; break ; } } $ question .= sprintf ( ' (%s)' , implode ( ', ' , $ options ) ) ; } $ question .= '?' ; if ( isset ( $ default ) ) { switch ( gettype ( $ default ) ) { case 'boolean' : $ question .= sprintf ( ' [%s]:' , ( $ default ) ? 'yes' : 'no' ) ; break ; default : $ question .= sprintf ( ' [%s]:' , $ default ) ; break ; } } $ answer = trim ( IO :: in ( $ question ) ) ; switch ( $ answer ) { case 'yes' : case 'y' : case '1' : case 'true' : return true ; case 'no' : case 'n' : case '0' : case 'false' : return false ; case '' : if ( isset ( $ default ) ) { return $ default ; } elseif ( $ required ) { $ this -> log -> warning ( 'Excuse me, what do you mean?' ) ; return $ this -> askForMixed ( $ key , $ required , $ default , $ values ) ; } break ; default : if ( count ( $ values ) && ! in_array ( $ answer , $ values ) ) { $ this -> log -> warning ( 'Wrong answer. Here are your options: %s' , implode ( ', ' , $ options ) ) ; return $ this -> askForMixed ( $ key , $ required , $ default , $ values ) ; } return $ answer ; } return null ; }
Ask user for a value, but the data type doesn't matter

@param string $key Setting
@param bool $required Is this setting required? Defaults to false (no, it isn't)
@param mixed $default Optional default value which will be used if user doesn't provide a value
@param array $values Optional possible values; no other values are accepted

@return bool|string|null Value or nothing

@throws Exception on error
Ask user for a value but the data type doesn t matter