php_codegen/
parameter.rs

1use crate::attribute::AttributeGroup;
2use crate::data_type::DataType;
3use crate::literal::Value;
4use crate::modifiers::Modifier;
5use crate::modifiers::VisibilityModifier;
6use crate::Generator;
7use crate::Indentation;
8
9#[derive(Debug)]
10pub struct Parameter {
11    pub attributes: Vec<AttributeGroup>,
12    pub name: String,
13    pub data_type: Option<DataType>,
14    pub default: Option<Value>,
15    pub modifiers: Vec<Modifier>,
16    pub visibility: Option<VisibilityModifier>,
17    pub variadic: bool,
18}
19
20impl Parameter {
21    pub fn new<T: ToString>(name: T) -> Self {
22        Self {
23            name: name.to_string(),
24            data_type: None,
25            default: None,
26            modifiers: vec![],
27            attributes: vec![],
28            visibility: None,
29            variadic: false,
30        }
31    }
32
33    pub fn attributes(mut self, attributes: AttributeGroup) -> Self {
34        self.attributes.push(attributes);
35
36        self
37    }
38
39    pub fn typed(mut self, data_type: DataType) -> Self {
40        self.data_type = Some(data_type);
41
42        self
43    }
44
45    pub fn variadic(mut self) -> Self {
46        self.variadic = true;
47
48        self
49    }
50
51    pub fn default<T: Into<Value>>(mut self, default: T) -> Self {
52        self.default = Some(default.into());
53
54        self
55    }
56
57    pub fn modifier(mut self, modifier: Modifier) -> Self {
58        self.modifiers.push(modifier);
59
60        self
61    }
62
63    pub fn public(mut self) -> Self {
64        self.visibility = Some(VisibilityModifier::Public);
65
66        self
67    }
68
69    pub fn protected(mut self) -> Self {
70        self.visibility = Some(VisibilityModifier::Protected);
71
72        self
73    }
74
75    pub fn private(mut self) -> Self {
76        self.visibility = Some(VisibilityModifier::Private);
77
78        self
79    }
80
81    pub fn visibility(mut self, visibility: VisibilityModifier) -> Self {
82        self.visibility = Some(visibility);
83
84        self
85    }
86}
87
88impl Generator for Parameter {
89    fn generate(&self, indentation: Indentation, level: usize) -> String {
90        let mut code = String::new();
91
92        for attribute in &self.attributes {
93            code.push_str(&attribute.generate(indentation, level));
94        }
95
96        code.push_str(&indentation.value(level));
97
98        if let Some(visibility) = &self.visibility {
99            code.push_str(&format!("{} ", visibility.generate(indentation, level)));
100        }
101
102        for modifier in &self.modifiers {
103            code.push_str(&format!("{} ", modifier.generate(indentation, level)));
104        }
105
106        if let Some(data_type) = &self.data_type {
107            code.push_str(&format!("{} ", data_type.generate(indentation, level)));
108        }
109
110        if self.variadic {
111            code.push_str("...");
112        }
113
114        code.push_str(&format!("${}", &self.name));
115
116        if let Some(default) = &self.default {
117            code.push_str(&format!(" = {}", default.generate(indentation, level)));
118        }
119
120        code
121    }
122}
123
124impl Generator for Vec<Parameter> {
125    fn generate(&self, indentation: Indentation, level: usize) -> String {
126        let mut code = String::new();
127
128        if self.is_empty() {
129            code.push_str("()");
130        } else {
131            code.push_str("(\n");
132            code.push_str(
133                &self
134                    .iter()
135                    .map(|parameter| parameter.generate(indentation, level + 1))
136                    .collect::<Vec<String>>()
137                    .join(",\n"),
138            );
139
140            code.push_str(",\n");
141            code.push_str(&indentation.indent(")", level));
142        }
143
144        code
145    }
146}