|
| 1 | +use php_codegen::attribute::AttributeGroup; |
| 2 | +use php_codegen::class::Class; |
| 3 | +use php_codegen::comment::Document; |
| 4 | +use php_codegen::constant::ClassConstant; |
| 5 | +use php_codegen::constant::Constant; |
| 6 | +use php_codegen::data_type::DataType; |
| 7 | +use php_codegen::file::File; |
| 8 | +use php_codegen::function::Function; |
| 9 | +use php_codegen::literal::Value; |
| 10 | +use php_codegen::method::Method; |
| 11 | +use php_codegen::modifiers::Modifier; |
| 12 | +use php_codegen::parameter::Parameter; |
| 13 | +use php_codegen::property::Property; |
| 14 | +use php_codegen::Indentation; |
| 15 | + |
| 16 | +fn main() { |
| 17 | + let file = File::new() |
| 18 | + .namespaced("Foo") |
| 19 | + .declare("strict_types", 1) |
| 20 | + .uses("Foo\\Bar\\Baz as Qux") |
| 21 | + .uses("Throwable") |
| 22 | + .uses("DateTime") |
| 23 | + .uses("DateTimeImmutable") |
| 24 | + .uses_function("strlen") |
| 25 | + .uses_function("array_map") |
| 26 | + .uses_function("array_filter") |
| 27 | + .uses_constant("Foo\\HELLO") |
| 28 | + .uses_constant("Foo\\HEY") |
| 29 | + .constant(Constant::new("A").valued("Hello World!")) |
| 30 | + .constant(Constant::new("B").valued(())) |
| 31 | + .constant(Constant::new("C").valued(1)) |
| 32 | + .constant(Constant::new("D").valued(false)) |
| 33 | + .constant(Constant::new("E")) |
| 34 | + .constant(("F", 213.412)) |
| 35 | + .constant(("G", vec![1, 25])) |
| 36 | + .function( |
| 37 | + Function::new("hello") |
| 38 | + .document( |
| 39 | + Document::new() |
| 40 | + .text("This is a simple hello function.") |
| 41 | + .empty_line() |
| 42 | + .tag("param", "non-empty-string $firstname") |
| 43 | + .empty_line() |
| 44 | + .tag("return", "string") |
| 45 | + .empty_line() |
| 46 | + .simple_tag("pure"), |
| 47 | + ) |
| 48 | + .attributes(AttributeGroup::new().add("Qux", Some("foo: 1, bar: 2"))) |
| 49 | + .parameter( |
| 50 | + Parameter::new("firstname") |
| 51 | + .typed(DataType::String) |
| 52 | + .attributes( |
| 53 | + AttributeGroup::new() |
| 54 | + .add("Validation\\NotBlank", None) |
| 55 | + .add("Validation\\Length", Some("min: 2, max: 10")), |
| 56 | + ), |
| 57 | + ) |
| 58 | + .parameter( |
| 59 | + Parameter::new("lastname") |
| 60 | + .typed(DataType::String) |
| 61 | + .default(Value::Literal("Qux::Foo".to_string())), |
| 62 | + ) |
| 63 | + .returns(DataType::String) |
| 64 | + .body("return 'Hello ' . $firstname . ' ' . $lastname . '!';"), |
| 65 | + ) |
| 66 | + .function(Function::new("nothing").returns(DataType::Void)) |
| 67 | + .function( |
| 68 | + Function::new("format") |
| 69 | + .parameter( |
| 70 | + Parameter::new("template") |
| 71 | + .typed(DataType::String) |
| 72 | + ) |
| 73 | + .parameter( |
| 74 | + Parameter::new("args") |
| 75 | + .variadic() |
| 76 | + .typed(DataType::Union(vec![ |
| 77 | + DataType::Integer, |
| 78 | + DataType::Float, |
| 79 | + DataType::String, |
| 80 | + DataType::Null, |
| 81 | + ])), |
| 82 | + ) |
| 83 | + .returns(DataType::String) |
| 84 | + .body(vec![ |
| 85 | + "return sprintf($template, ...array_map(", |
| 86 | + " static fn ($arg) => is_float($arg) ? number_format($arg, 2) : $arg,", |
| 87 | + " array_filter($args, static fn ($arg) => $arg !== null)", |
| 88 | + "));", |
| 89 | + ]), |
| 90 | + ) |
| 91 | + .class( |
| 92 | + Class::new("Example") |
| 93 | + .document( |
| 94 | + Document::new() |
| 95 | + .text("This is an example class.") |
| 96 | + .empty_line() |
| 97 | + .simple_tag("immutable"), |
| 98 | + ) |
| 99 | + .modifier(Modifier::Abstract) |
| 100 | + .constant( |
| 101 | + ClassConstant::new("A") |
| 102 | + .valued("Hello World!") |
| 103 | + .modifier(Modifier::Final), |
| 104 | + ) |
| 105 | + .constant(ClassConstant::new("B").valued(()).protected()) |
| 106 | + .constant(ClassConstant::new("C").valued(1).private()) |
| 107 | + .constant(ClassConstant::new("D").valued(false).public()) |
| 108 | + .property(Property::new("foo").typed(DataType::String).private()) |
| 109 | + .property(Property::new("bar").typed(DataType::String).protected()) |
| 110 | + .property( |
| 111 | + Property::new("baz") |
| 112 | + .typed(DataType::Union(vec![DataType::String, DataType::Integer])) |
| 113 | + .public() |
| 114 | + .default("Hello World!"), |
| 115 | + ) |
| 116 | + .method( |
| 117 | + Method::new("hello") |
| 118 | + .returns(DataType::String) |
| 119 | + .parameter(Parameter::new("firstname").typed(DataType::String)) |
| 120 | + .parameter( |
| 121 | + Parameter::new("lastname") |
| 122 | + .typed(DataType::String) |
| 123 | + .default(Value::Literal("Qux::Foo".to_string())), |
| 124 | + ) |
| 125 | + .body("return 'Hello ' . $firstname . ' ' . $lastname . '!';") |
| 126 | + .attributes( |
| 127 | + AttributeGroup::new() |
| 128 | + .add("Qux", Some("foo: 1, bar: 2")) |
| 129 | + .add("Qux", Some("foo: 1, bar: 2")), |
| 130 | + ) |
| 131 | + .document( |
| 132 | + Document::new() |
| 133 | + .text("This is a simple hello function.") |
| 134 | + .empty_line() |
| 135 | + .tag("param", "non-empty-string $firstname") |
| 136 | + .empty_line() |
| 137 | + .tag("return", "string") |
| 138 | + .empty_line() |
| 139 | + .simple_tag("pure"), |
| 140 | + ), |
| 141 | + ) |
| 142 | + .method( |
| 143 | + Method::new("x") |
| 144 | + .public() |
| 145 | + .returns(DataType::Mixed) |
| 146 | + .body("return 'Hello!';") |
| 147 | + .attributes( |
| 148 | + AttributeGroup::new() |
| 149 | + .add("Foo", Some("foo: 1, bar: 2")) |
| 150 | + .add("Bar", Some("foo: 1, bar: 2")), |
| 151 | + ) |
| 152 | + .attributes(AttributeGroup::new().add("Baz", None).add("Qux", None)) |
| 153 | + .document( |
| 154 | + Document::new() |
| 155 | + .text("This is a simple x function.") |
| 156 | + .empty_line() |
| 157 | + .simple_tag("pure"), |
| 158 | + ), |
| 159 | + ) |
| 160 | + .method( |
| 161 | + Method::new("poop") |
| 162 | + .public() |
| 163 | + .modifier(Modifier::Abstract) |
| 164 | + .returns(DataType::Void) |
| 165 | + .document(Document::new().text("This is a simple poop function.")), |
| 166 | + ) |
| 167 | + .method( |
| 168 | + Method::new("helloWorld") |
| 169 | + .public() |
| 170 | + .modifier(Modifier::Final) |
| 171 | + .returns(DataType::Void) |
| 172 | + .document(Document::new().text("This is a simple echo function.")) |
| 173 | + .body(|indentation: Indentation, level| { |
| 174 | + indentation.indent("echo 'Hello World!';", level) |
| 175 | + }), |
| 176 | + ), |
| 177 | + ); |
| 178 | + |
| 179 | + print!("{file}"); |
| 180 | +} |
0 commit comments