aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-symfony-console/src/input/input_argument.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-09 11:19:03 +0900
committernsfisis <nsfisis@gmail.com>2026-08-09 11:19:03 +0900
commit6051927c8fa32cfffa102d2a170c5a6cf747a1b9 (patch)
tree3fe6769c90cb03ca8f1b9b825e38ad459182361e /crates/shirabe-symfony-console/src/input/input_argument.rs
parente3e8806aec771e482899ed3470e920f7b291fa95 (diff)
downloadphp-shirabe-6051927c8fa32cfffa102d2a170c5a6cf747a1b9.tar.gz
php-shirabe-6051927c8fa32cfffa102d2a170c5a6cf747a1b9.tar.zst
php-shirabe-6051927c8fa32cfffa102d2a170c5a6cf747a1b9.zip
refactor(symfony-console): extract symfony/console into the shirabe-symfony-console crate
Move `Symfony\Component\Console` out of shirabe-external-packages and into its own crate, so the path is `shirabe_symfony_console::application::Application` instead of `shirabe_external_packages::symfony::console::application::Application`. The `delegate_to_inner!` and `delegate_command_trait_impls_to_inner!` macros move with it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-symfony-console/src/input/input_argument.rs')
-rw-r--r--crates/shirabe-symfony-console/src/input/input_argument.rs96
1 files changed, 96 insertions, 0 deletions
diff --git a/crates/shirabe-symfony-console/src/input/input_argument.rs b/crates/shirabe-symfony-console/src/input/input_argument.rs
new file mode 100644
index 00000000..ebc8e223
--- /dev/null
+++ b/crates/shirabe-symfony-console/src/input/input_argument.rs
@@ -0,0 +1,96 @@
+//! ref: composer/vendor/symfony/console/Input/InputArgument.php
+
+use crate::exception::invalid_argument_exception::InvalidArgumentException;
+use crate::exception::logic_exception::LogicException;
+use shirabe_php_shim::PhpMixed;
+
+#[derive(Debug, Clone)]
+pub struct InputArgument {
+ name: String,
+ mode: i64,
+ default: PhpMixed,
+ description: String,
+}
+
+impl InputArgument {
+ pub const REQUIRED: i64 = 1;
+ pub const OPTIONAL: i64 = 2;
+ pub const IS_ARRAY: i64 = 4;
+
+ pub fn new(
+ name: String,
+ mode: Option<i64>,
+ description: String,
+ default: PhpMixed,
+ ) -> anyhow::Result<Self> {
+ let mode = match mode {
+ None => Self::OPTIONAL,
+ Some(m) if !(1..=7).contains(&m) => {
+ return Err(InvalidArgumentException::new(format!(
+ "Argument mode \"{}\" is not valid.",
+ m
+ ))
+ .into());
+ }
+ Some(m) => m,
+ };
+
+ let mut argument = InputArgument {
+ name,
+ mode,
+ description,
+ default: PhpMixed::Null,
+ };
+
+ argument.set_default(default)?;
+
+ Ok(argument)
+ }
+
+ pub fn get_name(&self) -> &str {
+ &self.name
+ }
+
+ pub fn is_required(&self) -> bool {
+ Self::REQUIRED == (Self::REQUIRED & self.mode)
+ }
+
+ pub fn is_array(&self) -> bool {
+ Self::IS_ARRAY == (Self::IS_ARRAY & self.mode)
+ }
+
+ pub fn set_default(&mut self, default: PhpMixed) -> anyhow::Result<()> {
+ if self.is_required() && !matches!(default, PhpMixed::Null) {
+ return Err(LogicException::new(
+ "Cannot set a default value except for InputArgument::OPTIONAL mode.".to_string(),
+ )
+ .into());
+ }
+
+ let default = if self.is_array() {
+ match default {
+ PhpMixed::Null => PhpMixed::List(vec![]),
+ PhpMixed::List(_) => default,
+ _ => {
+ return Err(LogicException::new(
+ "A default value for an array argument must be an array.".to_string(),
+ )
+ .into());
+ }
+ }
+ } else {
+ default
+ };
+
+ self.default = default;
+ Ok(())
+ }
+
+ pub fn get_default(&self) -> &PhpMixed {
+ &self.default
+ }
+
+ pub fn get_description(&self) -> &str {
+ &self.description
+ }
+}