php_codegen/
interface.rs

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