aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/symfony/console/question/choice_question.rs
blob: 7ad29c8b468b35dc2ecfd725d868fc158114e9b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException;
use crate::symfony::console::exception::logic_exception::LogicException;
use crate::symfony::console::question::Question;
use indexmap::IndexMap;
use shirabe_php_shim::PhpMixed;

/// Represents a choice question.
#[derive(Debug)]
pub struct ChoiceQuestion {
    inner: Question,
    choices: IndexMap<String, Box<PhpMixed>>,
    multiselect: bool,
    prompt: String,
    error_message: String,
}

impl ChoiceQuestion {
    /// `$question` The question to ask to the user.
    /// `$choices` The list of available choices.
    /// `$default` The default answer to return.
    pub fn new(
        question: String,
        choices: IndexMap<String, Box<PhpMixed>>,
        default: Option<PhpMixed>,
    ) -> Result<Self, LogicException> {
        if choices.is_empty() {
            return Err(LogicException(shirabe_php_shim::LogicException {
                message: "Choice question must have at least 1 choice available.".to_string(),
                code: 0,
            }));
        }

        let mut this = Self {
            inner: Question::new(question, default),
            choices: choices.clone(),
            multiselect: false,
            prompt: " > ".to_string(),
            error_message: "Value \"%s\" is invalid".to_string(),
        };

        let validator = this.get_default_validator();
        this.inner.set_validator(Some(validator));
        // setAutocompleterValues never throws for an array argument.
        this.inner
            .set_autocompleter_values(Some(PhpMixed::Array(choices)))
            .expect("autocompleter cannot be set on a hidden question during construction");

        Ok(this)
    }

    /// Returns available choices.
    pub fn get_choices(&self) -> &IndexMap<String, Box<PhpMixed>> {
        &self.choices
    }

    /// Sets multiselect option.
    ///
    /// When multiselect is set to true, multiple choices can be answered.
    pub fn set_multiselect(&mut self, multiselect: bool) -> &mut Self {
        self.multiselect = multiselect;
        let validator = self.get_default_validator();
        self.inner.set_validator(Some(validator));

        self
    }

    /// Returns whether the choices are multiselect.
    pub fn is_multiselect(&self) -> bool {
        self.multiselect
    }

    /// Gets the prompt for choices.
    pub fn get_prompt(&self) -> &str {
        &self.prompt
    }

    /// Sets the prompt for choices.
    pub fn set_prompt(&mut self, prompt: String) -> &mut Self {
        self.prompt = prompt;

        self
    }

    /// Sets the error message for invalid values.
    ///
    /// The error message has a string placeholder (%s) for the invalid value.
    pub fn set_error_message(&mut self, error_message: String) -> &mut Self {
        self.error_message = error_message;
        let validator = self.get_default_validator();
        self.inner.set_validator(Some(validator));

        self
    }

    fn get_default_validator(
        &self,
    ) -> Box<dyn Fn(Option<PhpMixed>) -> Result<PhpMixed, InvalidArgumentException>> {
        let choices = self.choices.clone();
        let error_message = self.error_message.clone();
        let multiselect = self.multiselect;
        let is_assoc = Question::is_assoc(&PhpMixed::Array(self.choices.clone()));
        // PHP reads `$this->isTrimmable()` live inside the closure. A 'static boxed
        // closure cannot borrow `$this`, so the value is snapshotted at validator
        // creation time. setValidator is re-run on multiselect/errorMessage changes,
        // but a later setTrimmable would not be reflected. See review notes.
        let trimmable = self.inner.is_trimmable();

        Box::new(move |selected: Option<PhpMixed>| {
            let selected = selected.unwrap_or(PhpMixed::Null);

            let selected_choices: Vec<PhpMixed> = if multiselect {
                // Check for a separated comma values
                let mut matches: Vec<Option<String>> = Vec::new();
                if shirabe_php_shim::preg_match(
                    "/^[^,]+(?:,[^,]+)*$/",
                    &shirabe_php_shim::strval(&selected),
                    &mut matches,
                ) == 0
                {
                    return Err(InvalidArgumentException(
                        shirabe_php_shim::InvalidArgumentException {
                            message: shirabe_php_shim::sprintf(&error_message, &[selected.clone()]),
                            code: 0,
                        },
                    ));
                }

                shirabe_php_shim::explode(",", &shirabe_php_shim::strval(&selected))
                    .into_iter()
                    .map(PhpMixed::String)
                    .collect()
            } else {
                vec![selected.clone()]
            };

            let mut selected_choices = selected_choices;
            if trimmable {
                for v in selected_choices.iter_mut() {
                    *v = PhpMixed::String(shirabe_php_shim::trim(
                        &shirabe_php_shim::strval(v),
                        None,
                    ));
                }
            }

            let mut multiselect_choices: Vec<PhpMixed> = Vec::new();
            for value in &selected_choices {
                let mut results: Vec<String> = Vec::new();
                for (key, choice) in &choices {
                    if (**choice) == *value {
                        results.push(key.clone());
                    }
                }

                if results.len() > 1 {
                    return Err(InvalidArgumentException(
                        shirabe_php_shim::InvalidArgumentException {
                            message: shirabe_php_shim::sprintf(
                                "The provided answer is ambiguous. Value should be one of \"%s\".",
                                &[PhpMixed::String(shirabe_php_shim::implode(
                                    "\" or \"", &results,
                                ))],
                            ),
                            code: 0,
                        },
                    ));
                }

                // array_search($value, $choices)
                let result_key = shirabe_php_shim::array_search(
                    &shirabe_php_shim::strval(value),
                    &choices_as_str(&choices),
                );

                let mut result: PhpMixed;
                if !is_assoc {
                    if let Some(found_key) = &result_key {
                        // $result = $choices[$result];
                        result = (*choices[found_key]).clone();
                    } else if let Some(found) = choices.get(&shirabe_php_shim::strval(value)) {
                        // isset($choices[$value])
                        result = (**found).clone();
                    } else {
                        result = PhpMixed::Bool(false);
                    }
                } else if result_key.is_none() {
                    if let Some(_found) = choices.get(&shirabe_php_shim::strval(value)) {
                        // false === $result && isset($choices[$value])
                        result = value.clone();
                    } else {
                        result = PhpMixed::Bool(false);
                    }
                } else {
                    // associative, found: keep the matched key
                    result = PhpMixed::String(result_key.clone().unwrap());
                }

                // false === $result
                if matches!(result, PhpMixed::Bool(false)) {
                    return Err(InvalidArgumentException(
                        shirabe_php_shim::InvalidArgumentException {
                            message: shirabe_php_shim::sprintf(&error_message, &[value.clone()]),
                            code: 0,
                        },
                    ));
                }

                // For associative choices, consistently return the key as string:
                if is_assoc {
                    result = PhpMixed::String(shirabe_php_shim::strval(&result));
                }
                multiselect_choices.push(result);
            }

            if multiselect {
                return Ok(PhpMixed::List(
                    multiselect_choices.into_iter().map(Box::new).collect(),
                ));
            }

            Ok(shirabe_php_shim::current(PhpMixed::List(
                multiselect_choices.into_iter().map(Box::new).collect(),
            )))
        })
    }
}

/// array_search operates over the choice values as strings; this projects the
/// choices map's values into the string-keyed form the shim expects.
fn choices_as_str(choices: &IndexMap<String, Box<PhpMixed>>) -> IndexMap<String, String> {
    choices
        .iter()
        .map(|(k, v)| (k.clone(), shirabe_php_shim::strval(v)))
        .collect()
}