From dd3ef438adaa607cf99196b656df896008e11b19 Mon Sep 17 00:00:00 2001 From: Quique Porta Date: Mon, 8 Apr 2013 15:09:07 +0200 Subject: [PATCH 1/7] Modified the select HelperDialog to allow multiselect options. --- .../Component/Console/Helper/DialogHelper.php | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/DialogHelper.php b/src/Symfony/Component/Console/Helper/DialogHelper.php index 3eac8d1923a5e..0833851044402 100644 --- a/src/Symfony/Component/Console/Helper/DialogHelper.php +++ b/src/Symfony/Component/Console/Helper/DialogHelper.php @@ -34,12 +34,13 @@ class DialogHelper extends Helper * @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 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 */ - 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, $multiselect = false, $errorMessage = 'Value "%s" is invalid') { $width = max(array_map('strlen', array_keys($choices))); @@ -50,12 +51,37 @@ public function select(OutputInterface $output, $question, $choices, $default = $output->writeln($messages); - $result = $this->askAndValidate($output, '> ', function ($picked) use ($choices, $errorMessage) { - if (empty($choices[$picked])) { - throw new \InvalidArgumentException(sprintf($errorMessage, $picked)); + $result = $this->askAndValidate($output, '> ', function ($picked) use ($choices, $errorMessage, $multiselect) { + + // Collapse all spaces. + $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 $key => $value) { + if (empty($choices[$value])) { + throw new \InvalidArgumentException(sprintf($errorMessage, $value)); + } + $multiselectChoices[$value] = $choices[$value]; + } + + if ($multiselect){ + return $multiselectChoices; + } else { + return $picked; } - return $picked; }, $attempts, $default); return $result; From 50e0c1a8acb014c76f04f63c662f415940cbb6e6 Mon Sep 17 00:00:00 2001 From: Quique Porta Date: Mon, 8 Apr 2013 15:11:43 +0200 Subject: [PATCH 2/7] Fixed the test to work with the new multiselect DialogHelper --- src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php index 12f27005cb03e..877939f832941 100644 --- a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php @@ -30,7 +30,7 @@ public function testSelect() $dialog->setInputStream($this->getInputStream("\n1\nFabien\n1\nFabien\nFabien\n")); $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($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, false, 'Input "%s" is not a superhero!')); rewind($output->getStream()); $this->assertContains('Input "Fabien" is not a superhero!', stream_get_contents($output->getStream())); From 03412418ffd1791649b960a99a04ad2cb13f6f8f Mon Sep 17 00:00:00 2001 From: Quique Porta Date: Thu, 11 Apr 2013 14:09:38 +0200 Subject: [PATCH 3/7] Changed parameter to the end of the function declaration. Fixed the function returns. Fixed the Test --- .../Component/Console/Helper/DialogHelper.php | 13 ++++++------- .../Console/Tests/Helper/DialogHelperTest.php | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/DialogHelper.php b/src/Symfony/Component/Console/Helper/DialogHelper.php index 0833851044402..161d653d995d1 100644 --- a/src/Symfony/Component/Console/Helper/DialogHelper.php +++ b/src/Symfony/Component/Console/Helper/DialogHelper.php @@ -40,7 +40,7 @@ class DialogHelper extends Helper * * @throws \InvalidArgumentException */ - public function select(OutputInterface $output, $question, $choices, $default = null, $attempts = false, $multiselect = 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))); @@ -62,14 +62,13 @@ public function select(OutputInterface $output, $question, $choices, $default = throw new \InvalidArgumentException(sprintf($errorMessage, $picked)); } $selectedChoices = explode(",", $selectedChoices); - } - else { + } else { $selectedChoices = array($picked); } $multiselectChoices = array(); - foreach ($selectedChoices as $key => $value) { + foreach ($selectedChoices as $value) { if (empty($choices[$value])) { throw new \InvalidArgumentException(sprintf($errorMessage, $value)); } @@ -78,9 +77,9 @@ public function select(OutputInterface $output, $question, $choices, $default = if ($multiselect){ return $multiselectChoices; - } else { - return $picked; - } + } + + return $picked; }, $attempts, $default); diff --git a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php index 877939f832941..04c27f33a857a 100644 --- a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php @@ -30,7 +30,7 @@ public function testSelect() $dialog->setInputStream($this->getInputStream("\n1\nFabien\n1\nFabien\nFabien\n")); $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($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 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()); $this->assertContains('Input "Fabien" is not a superhero!', stream_get_contents($output->getStream())); From 8e8e54ef1b320a8247fc0cdc6030e085be0e36de Mon Sep 17 00:00:00 2001 From: Quique Porta Date: Sat, 13 Apr 2013 16:41:57 +0200 Subject: [PATCH 4/7] Now the multiselect option return an array with the keys of the choices --- src/Symfony/Component/Console/Helper/DialogHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Helper/DialogHelper.php b/src/Symfony/Component/Console/Helper/DialogHelper.php index 161d653d995d1..3669c756ef31f 100644 --- a/src/Symfony/Component/Console/Helper/DialogHelper.php +++ b/src/Symfony/Component/Console/Helper/DialogHelper.php @@ -72,7 +72,7 @@ public function select(OutputInterface $output, $question, $choices, $default = if (empty($choices[$value])) { throw new \InvalidArgumentException(sprintf($errorMessage, $value)); } - $multiselectChoices[$value] = $choices[$value]; + array_push($multiselectChoices, $value); } if ($multiselect){ From 14115a4b4840c24b2b9f156a7163818f817a22c2 Mon Sep 17 00:00:00 2001 From: Quique Porta Date: Wed, 17 Apr 2013 12:25:01 +0200 Subject: [PATCH 5/7] Created a Test for multiselect and removed the blank lines. --- src/Symfony/Component/Console/Helper/DialogHelper.php | 4 +--- .../Component/Console/Tests/Helper/DialogHelperTest.php | 6 +++++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/DialogHelper.php b/src/Symfony/Component/Console/Helper/DialogHelper.php index 3669c756ef31f..706d9e7b83316 100644 --- a/src/Symfony/Component/Console/Helper/DialogHelper.php +++ b/src/Symfony/Component/Console/Helper/DialogHelper.php @@ -52,11 +52,10 @@ public function select(OutputInterface $output, $question, $choices, $default = $output->writeln($messages); $result = $this->askAndValidate($output, '> ', function ($picked) use ($choices, $errorMessage, $multiselect) { - // Collapse all spaces. $selectedChoices = str_replace(" ","", $picked); - if ($multiselect){ + 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)); @@ -80,7 +79,6 @@ public function select(OutputInterface $output, $question, $choices, $default = } return $picked; - }, $attempts, $default); return $result; diff --git a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php index 04c27f33a857a..532a4259e76d3 100644 --- a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php @@ -27,7 +27,7 @@ public function testSelect() $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")); $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($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', false)); @@ -41,6 +41,10 @@ public function testSelect() } catch (\InvalidArgumentException $e) { $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)); } public function testAsk() From 0e93c884f8bb102d35cd75beb76239c0d6373084 Mon Sep 17 00:00:00 2001 From: Quique Porta Date: Wed, 17 Apr 2013 12:50:16 +0200 Subject: [PATCH 6/7] Added a test to check the default option in multiselect --- .../Component/Console/Tests/Helper/DialogHelperTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php index 532a4259e76d3..180fd56eebdda 100644 --- a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php @@ -27,7 +27,7 @@ public function testSelect() $heroes = array('Superman', 'Batman', 'Spiderman'); - $dialog->setInputStream($this->getInputStream("\n1\nFabien\n1\nFabien\n1\n0,2\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('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!', false)); @@ -43,8 +43,8 @@ public function testSelect() } $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() From 68ffc9bd10faf167627da19a58bd224000186a64 Mon Sep 17 00:00:00 2001 From: Quique Porta Date: Wed, 17 Apr 2013 16:28:01 +0200 Subject: [PATCH 7/7] Solved white spaces --- src/Symfony/Component/Console/Helper/DialogHelper.php | 2 +- .../Component/Console/Tests/Helper/DialogHelperTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/DialogHelper.php b/src/Symfony/Component/Console/Helper/DialogHelper.php index 706d9e7b83316..23558691a2981 100644 --- a/src/Symfony/Component/Console/Helper/DialogHelper.php +++ b/src/Symfony/Component/Console/Helper/DialogHelper.php @@ -53,7 +53,7 @@ public function select(OutputInterface $output, $question, $choices, $default = $result = $this->askAndValidate($output, '> ', function ($picked) use ($choices, $errorMessage, $multiselect) { // Collapse all spaces. - $selectedChoices = str_replace(" ","", $picked); + $selectedChoices = str_replace(" ", "", $picked); if ($multiselect) { // Check for a separated comma values diff --git a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php index 180fd56eebdda..618d0f061f355 100644 --- a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php @@ -43,8 +43,8 @@ public function testSelect() } $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)); + $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()