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

        if (count($values) > 0) {
            $question .= sprintf(
                ' (%s)',
                implode(', ', $values)
            );
        }

        $question .= '?';

        if (isset($default)) {
            $question .= sprintf(' [%s]:', $default);
        }

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

        if (strlen($answer) === 0 && $required && !isset($default)) {
            $this->log->warning('This setting is required. You need to set a value.');
            return $this->askForString($key, $required, $default, $values, $minLength);
        } elseif (strlen($answer) === 0 && isset($default)) {
            return $default;
        } elseif (count($values) && !in_array($answer, $values)) {
            $this->log->warning(
                'Wrong answer. Here are your options: %s',
                implode(', ', $values)
            );
            return $this->askForString($key, $required, $default, $values, $minLength);
        } elseif (strlen($answer) > 0 && strlen($answer) < $minLength) {
            $this->log->warning(
                'Value must have at least %s character(s)',
                $minLength
            );
            return $this->askForString($key, $required, $default, $values, $minLength);
        } elseif (strlen($answer) > 0) {
            return $answer;
        }

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

        if (count($values) > 0) {
            $question .= sprintf(
                ' (%s)',
                implode(', ', $values)
            );
        }

        $question .= '?';

        if (isset($default)) {
            $question .= sprintf(' [%s]:', $default);
        }

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

        if (strlen($answer) === 0 && $required && !isset($default)) {
            $this->log->warning('This setting is required. You need to set a value.');
            return $this->askForString($key, $required, $default, $values, $minLength);
        } elseif (strlen($answer) === 0 && isset($default)) {
            return $default;
        } elseif (count($values) && !in_array($answer, $values)) {
            $this->log->warning(
                'Wrong answer. Here are your options: %s',
                implode(', ', $values)
            );
            return $this->askForString($key, $required, $default, $values, $minLength);
        } elseif (strlen($answer) > 0 && strlen($answer) < $minLength) {
            $this->log->warning(
                'Value must have at least %s character(s)',
                $minLength
            );
            return $this->askForString($key, $required, $default, $values, $minLength);
        } elseif (strlen($answer) > 0) {
            return $answer;
        }

        return null;
    }
protected function askForString ( $ key , $ required = false , $ default = null , array $ values = [ ] , $ minLength = 0 ) { $ question = sprintf ( 'What is the value for configuration setting "%s"' , $ key ) ; if ( count ( $ values ) > 0 ) { $ question .= sprintf ( ' (%s)' , implode ( ', ' , $ values ) ) ; } $ question .= '?' ; if ( isset ( $ default ) ) { $ question .= sprintf ( ' [%s]:' , $ default ) ; } $ answer = IO :: in ( $ question ) ; if ( strlen ( $ answer ) === 0 && $ required && ! isset ( $ default ) ) { $ this -> log -> warning ( 'This setting is required. You need to set a value.' ) ; return $ this -> askForString ( $ key , $ required , $ default , $ values , $ minLength ) ; } elseif ( strlen ( $ answer ) === 0 && isset ( $ default ) ) { return $ default ; } elseif ( count ( $ values ) && ! in_array ( $ answer , $ values ) ) { $ this -> log -> warning ( 'Wrong answer. Here are your options: %s' , implode ( ', ' , $ values ) ) ; return $ this -> askForString ( $ key , $ required , $ default , $ values , $ minLength ) ; } elseif ( strlen ( $ answer ) > 0 && strlen ( $ answer ) < $ minLength ) { $ this -> log -> warning ( 'Value must have at least %s character(s)' , $ minLength ) ; return $ this -> askForString ( $ key , $ required , $ default , $ values , $ minLength ) ; } elseif ( strlen ( $ answer ) > 0 ) { return $ answer ; } return null ; }
Ask user for a value which should be a string

@param string $key Setting
@param bool $required Is this setting required? Defaults to false (no, it isn't)
@param string $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
@param int $minLength Optional minimum length of value

@return string|null Value or nothing

@throws Exception on error
Ask user for a value which should be a string