[DialogHelper] Multiselect : added an option to the "select" function that makes the possibility of multiselect options.
This commit is contained in:
parent
83e078a035
commit
c8a5e021dc
@ -34,12 +34,13 @@ class DialogHelper extends Helper
|
|||||||
* @param Boolean $default The default answer if the user enters nothing
|
* @param Boolean $default The default answer if the user enters nothing
|
||||||
* @param Boolean|integer $attempts Max number of times to ask before giving up (false by default, which means infinite)
|
* @param Boolean|integer $attempts Max number of times to ask before giving up (false by default, which means infinite)
|
||||||
* @param string $errorMessage Message which will be shown if invalid value from choice list would be picked
|
* @param string $errorMessage Message which will be shown if invalid value from choice list would be picked
|
||||||
|
* @param Boolean $multiselect Select more than one value separated by comma
|
||||||
*
|
*
|
||||||
* @return integer|string The selected value (the key of the choices array)
|
* @return integer|string|array The selected value or values (the key of the choices array)
|
||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function select(OutputInterface $output, $question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid')
|
public function select(OutputInterface $output, $question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false)
|
||||||
{
|
{
|
||||||
$width = max(array_map('strlen', array_keys($choices)));
|
$width = max(array_map('strlen', array_keys($choices)));
|
||||||
|
|
||||||
@ -50,9 +51,31 @@ class DialogHelper extends Helper
|
|||||||
|
|
||||||
$output->writeln($messages);
|
$output->writeln($messages);
|
||||||
|
|
||||||
$result = $this->askAndValidate($output, '> ', function ($picked) use ($choices, $errorMessage) {
|
$result = $this->askAndValidate($output, '> ', function ($picked) use ($choices, $errorMessage, $multiselect) {
|
||||||
if (empty($choices[$picked])) {
|
// Collapse all spaces.
|
||||||
throw new \InvalidArgumentException(sprintf($errorMessage, $picked));
|
$selectedChoices = str_replace(" ", "", $picked);
|
||||||
|
|
||||||
|
if ($multiselect) {
|
||||||
|
// Check for a separated comma values
|
||||||
|
if(!preg_match('/^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$/', $selectedChoices, $matches)) {
|
||||||
|
throw new \InvalidArgumentException(sprintf($errorMessage, $picked));
|
||||||
|
}
|
||||||
|
$selectedChoices = explode(",", $selectedChoices);
|
||||||
|
} else {
|
||||||
|
$selectedChoices = array($picked);
|
||||||
|
}
|
||||||
|
|
||||||
|
$multiselectChoices = array();
|
||||||
|
|
||||||
|
foreach ($selectedChoices as $value) {
|
||||||
|
if (empty($choices[$value])) {
|
||||||
|
throw new \InvalidArgumentException(sprintf($errorMessage, $value));
|
||||||
|
}
|
||||||
|
array_push($multiselectChoices, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($multiselect){
|
||||||
|
return $multiselectChoices;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $picked;
|
return $picked;
|
||||||
|
@ -27,10 +27,10 @@ class DialogHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$heroes = array('Superman', 'Batman', 'Spiderman');
|
$heroes = array('Superman', 'Batman', 'Spiderman');
|
||||||
|
|
||||||
$dialog->setInputStream($this->getInputStream("\n1\nFabien\n1\nFabien\nFabien\n"));
|
$dialog->setInputStream($this->getInputStream("\n1\nFabien\n1\nFabien\n1\n0,2\n\n"));
|
||||||
$this->assertEquals('2', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, '2'));
|
$this->assertEquals('2', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, '2'));
|
||||||
$this->assertEquals('1', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes));
|
$this->assertEquals('1', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes));
|
||||||
$this->assertEquals('1', $dialog->select($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!'));
|
$this->assertEquals('1', $dialog->select($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', false));
|
||||||
|
|
||||||
rewind($output->getStream());
|
rewind($output->getStream());
|
||||||
$this->assertContains('Input "Fabien" is not a superhero!', stream_get_contents($output->getStream()));
|
$this->assertContains('Input "Fabien" is not a superhero!', stream_get_contents($output->getStream()));
|
||||||
@ -41,6 +41,10 @@ class DialogHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
} catch (\InvalidArgumentException $e) {
|
} catch (\InvalidArgumentException $e) {
|
||||||
$this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
|
$this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->assertEquals(array('1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true));
|
||||||
|
$this->assertEquals(array('0', '2'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true));
|
||||||
|
$this->assertEquals(array('0', '1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, '0,1', false, 'Input "%s" is not a superhero!', true));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAsk()
|
public function testAsk()
|
||||||
|
Reference in New Issue
Block a user