aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/symfony/console/input/input_argument.rs
blob: 4a732bd82b316254c804bb91bf9f345d3a08f57f (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
//! ref: composer/vendor/symfony/console/Input/InputArgument.php

use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException;
use crate::symfony::console::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(shirabe_php_shim::InvalidArgumentException {
                        message: format!("Argument mode \"{}\" is not valid.", m),
                        code: 0,
                    })
                    .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(shirabe_php_shim::LogicException {
                message: "Cannot set a default value except for InputArgument::OPTIONAL mode."
                    .to_string(),
                code: 0,
            })
            .into());
        }

        let default = if self.is_array() {
            match default {
                PhpMixed::Null => PhpMixed::List(vec![]),
                PhpMixed::List(_) => default,
                _ => {
                    return Err(LogicException(shirabe_php_shim::LogicException {
                        message: "A default value for an array argument must be an array."
                            .to_string(),
                        code: 0,
                    })
                    .into());
                }
            }
        } else {
            default
        };

        self.default = default;
        Ok(())
    }

    pub fn get_default(&self) -> &PhpMixed {
        &self.default
    }

    pub fn get_description(&self) -> &str {
        &self.description
    }
}