php_codegen/
trait.rs

1use crate::attribute::AttributeGroup;
2use crate::comment::Document;
3use crate::constant::ClassConstant;
4use crate::method::Method;
5use crate::property::Property;
6use crate::usage::Usage;
7use crate::Generator;
8use crate::Indentation;
9
10#[derive(Debug)]
11pub struct Trait {
12    pub documentation: Option<Document>,
13    pub attributes: Vec<AttributeGroup>,
14    pub name: String,
15    pub usages: Vec<Usage>,
16    pub constants: Vec<ClassConstant>,
17    pub properties: Vec<Property>,
18    pub methods: Vec<Method>,
19}
20
21impl Trait {
22    pub fn new<T: ToString>(name: T) -> Self {
23        Self {
24            documentation: None,
25            attributes: vec![],
26            name: name.to_string(),
27            usages: vec![],
28            constants: vec![],
29            properties: vec![],
30            methods: vec![],
31        }
32    }
33
34    pub fn document(mut self, documentation: Document) -> Self {
35        self.documentation = Some(documentation);
36
37        self
38    }
39
40    pub fn attributes(mut self, attributes: AttributeGroup) -> Self {
41        self.attributes.push(attributes);
42
43        self
44    }
45
46    pub fn using<T: Into<Usage>>(mut self, usage: T) -> Self {
47        self.usages.push(usage.into());
48
49        self
50    }
51
52    pub fn constant<T: Into<ClassConstant>>(mut self, constant: T) -> Self {
53        self.constants.push(constant.into());
54
55        self
56    }
57
58    pub fn property(mut self, property: Property) -> Self {
59        self.properties.push(property);
60
61        self
62    }
63
64    pub fn method(mut self, method: Method) -> Self {
65        self.methods.push(method);
66
67        self
68    }
69}
70
71impl Generator for Trait {
72    fn generate(&self, indentation: Indentation, level: usize) -> String {
73        let mut code = String::new();
74
75        if let Some(documentation) = &self.documentation {
76            code.push_str(documentation.generate(indentation, level).as_str());
77        }
78
79        for attribute in &self.attributes {
80            code.push_str(attribute.generate(indentation, level).as_str());
81        }
82
83        code.push_str(format!("trait {}", self.name).as_str());
84
85        code.push_str("\n{\n");
86
87        code.push_str(self.usages.generate(indentation, level + 1).as_str());
88        code.push_str(self.constants.generate(indentation, level + 1).as_str());
89        code.push_str(self.properties.generate(indentation, level + 1).as_str());
90        code.push_str(self.methods.generate(indentation, level + 1).as_str());
91
92        code = code.trim_end().to_string();
93        code.push_str("\n}\n");
94
95        code
96    }
97}
98
99impl Generator for Vec<Trait> {
100    fn generate(&self, indentation: Indentation, level: usize) -> String {
101        let mut code = String::new();
102        if self.is_empty() {
103            return code;
104        }
105
106        for r#trait in self {
107            code.push_str(r#trait.generate(indentation, level).as_str());
108            code.push('\n');
109        }
110
111        code
112    }
113}