From 1e44f5723e4c0e0903d00a61f254901e612fe5e1 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 12 Jun 2026 01:01:35 +0900 Subject: feat(symfony-console): port Symfony Console and make the workspace compile Co-Authored-By: Claude Opus 4.8 --- .../src/symfony/console/question/question.rs | 274 +++++++++++++++++++-- 1 file changed, 248 insertions(+), 26 deletions(-) (limited to 'crates/shirabe-external-packages/src/symfony/console/question/question.rs') diff --git a/crates/shirabe-external-packages/src/symfony/console/question/question.rs b/crates/shirabe-external-packages/src/symfony/console/question/question.rs index 552f780..754f286 100644 --- a/crates/shirabe-external-packages/src/symfony/console/question/question.rs +++ b/crates/shirabe-external-packages/src/symfony/console/question/question.rs @@ -1,53 +1,275 @@ +use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException; +use crate::symfony::console::exception::logic_exception::LogicException; use shirabe_php_shim::PhpMixed; -#[derive(Debug)] -pub struct Question; +/// Represents a Question. +pub struct Question { + question: String, + attempts: Option, + hidden: bool, + hidden_fallback: bool, + autocompleter_callback: Option Option>>>, + validator: Option) -> Result>>, + default: Option, + normalizer: Option PhpMixed>>, + trimmable: bool, + multiline: bool, +} + +impl std::fmt::Debug for Question { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Question") + .field("question", &self.question) + .field("attempts", &self.attempts) + .field("hidden", &self.hidden) + .field("hidden_fallback", &self.hidden_fallback) + .field("default", &self.default) + .field("trimmable", &self.trimmable) + .field("multiline", &self.multiline) + .finish_non_exhaustive() + } +} impl Question { - pub fn new(_question: &str, _default: Option) -> Self { - todo!() + /// `$question` The question to ask to the user. + /// `$default` The default answer to return if the user enters nothing. + pub fn new(question: String, default: Option) -> Self { + Self { + question, + attempts: None, + hidden: false, + hidden_fallback: true, + autocompleter_callback: None, + validator: None, + default, + normalizer: None, + trimmable: true, + multiline: false, + } } - pub fn set_validator( - &mut self, - _validator: Option) -> anyhow::Result>>, - ) { - todo!() + /// Returns the question. + pub fn get_question(&self) -> &str { + &self.question } - pub fn set_max_attempts(&mut self, _attempts: Option) { - todo!() + /// Returns the default answer. + pub fn get_default(&self) -> PhpMixed { + self.default.clone().unwrap_or(PhpMixed::Null) } - pub fn set_normalizer(&mut self, _normalizer: Box PhpMixed>) { - todo!() + /// Returns whether the user response accepts newline characters. + pub fn is_multiline(&self) -> bool { + self.multiline } - pub fn set_hidden(&mut self, _hidden: bool) { - todo!() + /// Sets whether the user response should accept newline characters. + pub fn set_multiline(&mut self, multiline: bool) -> &mut Self { + self.multiline = multiline; + + self } - pub fn set_hidden_fallback(&mut self, _fallback: bool) { - todo!() + /// Returns whether the user response must be hidden. + pub fn is_hidden(&self) -> bool { + self.hidden } - pub fn get_question(&self) -> String { - todo!() + /// Sets whether the user response must be hidden or not. + /// + /// Throws LogicException in case the autocompleter is also used. + pub fn set_hidden(&mut self, hidden: bool) -> Result<&mut Self, LogicException> { + if self.autocompleter_callback.is_some() { + return Err(LogicException(shirabe_php_shim::LogicException { + message: "A hidden question cannot use the autocompleter.".to_string(), + code: 0, + })); + } + + self.hidden = hidden; + + Ok(self) } - pub fn get_default(&self) -> Option { - todo!() + /// In case the response cannot be hidden, whether to fallback on non-hidden question or not. + pub fn is_hidden_fallback(&self) -> bool { + self.hidden_fallback } - pub fn is_hidden(&self) -> bool { - todo!() + /// Sets whether to fallback on non-hidden question if the response cannot be hidden. + pub fn set_hidden_fallback(&mut self, fallback: bool) -> &mut Self { + self.hidden_fallback = fallback; + + self } - pub fn get_validator(&self) -> Option<&dyn Fn(Option) -> anyhow::Result> { - todo!() + /// Gets values for the autocompleter. + pub fn get_autocompleter_values(&self) -> Option> { + let callback = self.get_autocompleter_callback(); + + match callback { + Some(callback) => callback(""), + None => None, + } + } + + /// Sets values for the autocompleter. + /// + /// Throws LogicException. + pub fn set_autocompleter_values( + &mut self, + values: Option, + ) -> Result<&mut Self, LogicException> { + let callback: Option Option>>> = match values { + // PHP: `if (\is_array($values))`. Both PhpMixed::List and ::Array model PHP arrays. + Some(values) if matches!(values, PhpMixed::List(_) | PhpMixed::Array(_)) => { + let values = if Self::is_assoc(&values) { + let array = match &values { + PhpMixed::Array(array) => array, + _ => unreachable!(), + }; + // array_merge(array_keys($values), array_values($values)) + let mut merged: Vec = + array.keys().map(|k| PhpMixed::String(k.clone())).collect(); + merged.extend(array.values().map(|v| (**v).clone())); + merged + } else { + // array_values($values) + match &values { + PhpMixed::List(list) => list.iter().map(|v| (**v).clone()).collect(), + PhpMixed::Array(array) => array.values().map(|v| (**v).clone()).collect(), + _ => unreachable!(), + } + }; + + Some(Box::new(move |_input: &str| Some(values.clone()))) + } + // PHP: `elseif ($values instanceof \Traversable)`. In PHP this caches the + // iterator result; here non-array iterables are not modeled, so treat any + // remaining value as the Traversable branch. + Some(values) => { + // PHP: `iterator_to_array($values, false)` caches a Traversable. + // Non-array iterables are not modeled by PhpMixed; extract any + // list/array elements, otherwise treat as an empty iterator. + let cached: Vec = match values { + PhpMixed::List(list) => list.into_iter().map(|v| *v).collect(), + PhpMixed::Array(array) => array.into_values().map(|v| *v).collect(), + _ => Vec::new(), + }; + Some(Box::new(move |_input: &str| Some(cached.clone()))) + } + None => None, + }; + + self.set_autocompleter_callback(callback) + } + + /// Gets the callback function used for the autocompleter. + pub fn get_autocompleter_callback(&self) -> Option<&(dyn Fn(&str) -> Option>)> { + self.autocompleter_callback.as_deref() + } + + /// Sets the callback function used for the autocompleter. + /// + /// The callback is passed the user input as argument and should return an iterable of + /// corresponding suggestions. + pub fn set_autocompleter_callback( + &mut self, + callback: Option Option>>>, + ) -> Result<&mut Self, LogicException> { + if self.hidden && callback.is_some() { + return Err(LogicException(shirabe_php_shim::LogicException { + message: "A hidden question cannot use the autocompleter.".to_string(), + code: 0, + })); + } + + self.autocompleter_callback = callback; + + Ok(self) } + /// Sets a validator for the question. + pub fn set_validator( + &mut self, + validator: Option< + Box) -> Result>, + >, + ) -> &mut Self { + self.validator = validator; + + self + } + + /// Gets the validator for the question. + pub fn get_validator( + &self, + ) -> Option<&(dyn Fn(Option) -> Result)> { + self.validator.as_deref() + } + + /// Sets the maximum number of attempts. + /// + /// Null means an unlimited number of attempts. + /// + /// Throws InvalidArgumentException in case the number of attempts is invalid. + pub fn set_max_attempts( + &mut self, + attempts: Option, + ) -> Result<&mut Self, InvalidArgumentException> { + if let Some(attempts) = attempts { + if attempts < 1 { + return Err(InvalidArgumentException( + shirabe_php_shim::InvalidArgumentException { + message: "Maximum number of attempts must be a positive value.".to_string(), + code: 0, + }, + )); + } + } + + self.attempts = attempts; + + Ok(self) + } + + /// Gets the maximum number of attempts. + /// + /// Null means an unlimited number of attempts. pub fn get_max_attempts(&self) -> Option { - todo!() + self.attempts + } + + /// Sets a normalizer for the response. + /// + /// The normalizer can be a callable (a string), a closure or a class implementing __invoke. + pub fn set_normalizer(&mut self, normalizer: Box PhpMixed>) -> &mut Self { + self.normalizer = Some(normalizer); + + self + } + + /// Gets the normalizer for the response. + /// + /// The normalizer can ba a callable (a string), a closure or a class implementing __invoke. + pub fn get_normalizer(&self) -> Option<&(dyn Fn(PhpMixed) -> PhpMixed)> { + self.normalizer.as_deref() + } + + // PHP: `(bool) \count(array_filter(array_keys($array), 'is_string'))`. + // PhpMixed models a PHP array as either `List` (sequential int keys) or `Array` + // (string-keyed map), so the "has string keys" test reduces to the `Array` variant. + pub(crate) fn is_assoc(array: &PhpMixed) -> bool { + matches!(array, PhpMixed::Array(_)) + } + + pub fn is_trimmable(&self) -> bool { + self.trimmable + } + + pub fn set_trimmable(&mut self, trimmable: bool) -> &mut Self { + self.trimmable = trimmable; + + self } } -- cgit v1.3.1