diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-24 00:49:51 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-24 00:49:51 +0900 |
| commit | 27c961ee7bee5c775e1902e59f74a9871d31321d (patch) | |
| tree | de18246316956b7fd91c07c616d855819351c387 /crates/shirabe-external-packages/src/symfony/console/question/question.rs | |
| parent | 340164c64e90d44b1bdf620b514166db1f76cc98 (diff) | |
| download | php-shirabe-27c961ee7bee5c775e1902e59f74a9871d31321d.tar.gz php-shirabe-27c961ee7bee5c775e1902e59f74a9871d31321d.tar.zst php-shirabe-27c961ee7bee5c775e1902e59f74a9871d31321d.zip | |
feat(question): model Question hierarchy via QuestionInterface trait
Introduce a QuestionInterface trait that the base Question and all its
subclasses (ChoiceQuestion, ConfirmationQuestion, StrictConfirmationQuestion)
implement, with as_choice/as_confirmation downcasts standing in for PHP's
instanceof. Consumers (QuestionHelper, SymfonyQuestionHelper, SymfonyStyle,
ConsoleIO) now take a QuestionInterface boundary generically.
This fixes the instanceof emulation, which previously went through
as_any().downcast_ref on a concrete &Question and always returned None, and
unblocks the select/confirm/choice paths that were left as todo!() because a
polymorphic ChoiceQuestion/ConfirmationQuestion could not be passed to ask.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-external-packages/src/symfony/console/question/question.rs')
| -rw-r--r-- | crates/shirabe-external-packages/src/symfony/console/question/question.rs | 93 |
1 files changed, 93 insertions, 0 deletions
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 9ec3aef..c93a71e 100644 --- a/crates/shirabe-external-packages/src/symfony/console/question/question.rs +++ b/crates/shirabe-external-packages/src/symfony/console/question/question.rs @@ -2,8 +2,53 @@ use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException; use crate::symfony::console::exception::logic_exception::LogicException; +use crate::symfony::console::question::choice_question::ChoiceQuestion; +use crate::symfony::console::question::confirmation_question::ConfirmationQuestion; use shirabe_php_shim::PhpMixed; +/// Polymorphic boundary for the Symfony Console Question hierarchy. +/// +/// PHP has no `QuestionInterface`; `Question` is a concrete class extended by +/// `ChoiceQuestion`/`ConfirmationQuestion`. Modelling those subclasses as +/// `inner: Question` composition loses subtype identity, so consumers that take +/// a `Question` and run `instanceof` checks are expressed here as a trait whose +/// `as_choice`/`as_confirmation` downcasts stand in for `instanceof`. +pub trait QuestionInterface: std::fmt::Debug { + fn get_question(&self) -> &str; + + fn get_default(&self) -> PhpMixed; + + fn is_multiline(&self) -> bool; + + fn is_hidden(&self) -> bool; + + fn is_hidden_fallback(&self) -> bool; + + fn get_autocompleter_values(&self) -> Option<Vec<PhpMixed>>; + + fn get_autocompleter_callback(&self) -> Option<&(dyn Fn(&str) -> Option<Vec<PhpMixed>>)>; + + fn get_validator( + &self, + ) -> Option<&(dyn Fn(Option<PhpMixed>) -> Result<PhpMixed, InvalidArgumentException>)>; + + fn get_max_attempts(&self) -> Option<i64>; + + fn get_normalizer(&self) -> Option<&(dyn Fn(PhpMixed) -> PhpMixed)>; + + fn is_trimmable(&self) -> bool; + + /// Models `$question instanceof ChoiceQuestion`. + fn as_choice(&self) -> Option<&ChoiceQuestion> { + None + } + + /// Models `$question instanceof ConfirmationQuestion`. + fn as_confirmation(&self) -> Option<&ConfirmationQuestion> { + None + } +} + /// Represents a Question. pub struct Question { question: String, @@ -275,3 +320,51 @@ impl Question { self } } + +impl QuestionInterface for Question { + fn get_question(&self) -> &str { + self.get_question() + } + + fn get_default(&self) -> PhpMixed { + self.get_default() + } + + fn is_multiline(&self) -> bool { + self.is_multiline() + } + + fn is_hidden(&self) -> bool { + self.is_hidden() + } + + fn is_hidden_fallback(&self) -> bool { + self.is_hidden_fallback() + } + + fn get_autocompleter_values(&self) -> Option<Vec<PhpMixed>> { + self.get_autocompleter_values() + } + + fn get_autocompleter_callback(&self) -> Option<&(dyn Fn(&str) -> Option<Vec<PhpMixed>>)> { + self.get_autocompleter_callback() + } + + fn get_validator( + &self, + ) -> Option<&(dyn Fn(Option<PhpMixed>) -> Result<PhpMixed, InvalidArgumentException>)> { + self.get_validator() + } + + fn get_max_attempts(&self) -> Option<i64> { + self.get_max_attempts() + } + + fn get_normalizer(&self) -> Option<&(dyn Fn(PhpMixed) -> PhpMixed)> { + self.get_normalizer() + } + + fn is_trimmable(&self) -> bool { + self.is_trimmable() + } +} |
