| @@ 57-85 (lines=29) @@ | ||
| 54 | ||
| 55 | $output->writeln($messages); |
|
| 56 | ||
| 57 | $result = $this->askAndValidate($output, '> ', function ($picked) use ($choices, $errorMessage, $multiselect) { |
|
| 58 | // Collapse all spaces. |
|
| 59 | $selectedChoices = str_replace(' ', '', $picked); |
|
| 60 | ||
| 61 | if ($multiselect) { |
|
| 62 | // Check for a separated comma values |
|
| 63 | if (!preg_match('/^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$/', $selectedChoices, $matches)) { |
|
| 64 | throw new \InvalidArgumentException(sprintf($errorMessage, $picked)); |
|
| 65 | } |
|
| 66 | $selectedChoices = explode(',', $selectedChoices); |
|
| 67 | } else { |
|
| 68 | $selectedChoices = array($picked); |
|
| 69 | } |
|
| 70 | ||
| 71 | $multiselectChoices = array(); |
|
| 72 | ||
| 73 | foreach ($selectedChoices as $value) { |
|
| 74 | if (empty($choices[$value])) { |
|
| 75 | throw new \InvalidArgumentException(sprintf($errorMessage, $value)); |
|
| 76 | } |
|
| 77 | array_push($multiselectChoices, $value); |
|
| 78 | } |
|
| 79 | ||
| 80 | if ($multiselect) { |
|
| 81 | return $multiselectChoices; |
|
| 82 | } |
|
| 83 | ||
| 84 | return $picked; |
|
| 85 | }, $attempts, $default); |
|
| 86 | ||
| 87 | return $result; |
|
| 88 | } |
|
| @@ 121-148 (lines=28) @@ | ||
| 118 | $errorMessage = $this->errorMessage; |
|
| 119 | $multiselect = $this->multiselect; |
|
| 120 | ||
| 121 | return function ($selected) use ($choices, $errorMessage, $multiselect) { |
|
| 122 | // Collapse all spaces. |
|
| 123 | $selectedChoices = str_replace(' ', '', $selected); |
|
| 124 | ||
| 125 | if ($multiselect) { |
|
| 126 | // Check for a separated comma values |
|
| 127 | if (!preg_match('/^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$/', $selectedChoices, $matches)) { |
|
| 128 | throw new \InvalidArgumentException(sprintf($errorMessage, $selected)); |
|
| 129 | } |
|
| 130 | $selectedChoices = explode(',', $selectedChoices); |
|
| 131 | } else { |
|
| 132 | $selectedChoices = array($selected); |
|
| 133 | } |
|
| 134 | ||
| 135 | $multiselectChoices = array(); |
|
| 136 | foreach ($selectedChoices as $value) { |
|
| 137 | if (empty($choices[$value])) { |
|
| 138 | throw new \InvalidArgumentException(sprintf($errorMessage, $value)); |
|
| 139 | } |
|
| 140 | array_push($multiselectChoices, $choices[$value]); |
|
| 141 | } |
|
| 142 | ||
| 143 | if ($multiselect) { |
|
| 144 | return $multiselectChoices; |
|
| 145 | } |
|
| 146 | ||
| 147 | return $choices[$selected]; |
|
| 148 | }; |
|
| 149 | } |
|
| 150 | } |
|
| 151 | ||