Skip to content

Commit f89398e

Browse files
committed
chore: update examples
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent 3157ae6 commit f89398e

File tree

4 files changed

+330
-231
lines changed

4 files changed

+330
-231
lines changed

examples/complete.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Foo;
6+
7+
use Foo\Bar\Baz as Qux;
8+
use Throwable;
9+
use DateTime;
10+
use DateTimeImmutable;
11+
12+
use function strlen;
13+
use function array_map;
14+
use function array_filter;
15+
16+
use const Foo\HELLO;
17+
use const Foo\HEY;
18+
19+
20+
const A = "Hello World!";
21+
const B = null;
22+
const C = 1;
23+
const D = false;
24+
const E = null;
25+
const F = 213.412;
26+
const G = [1, 25];
27+
28+
/**
29+
* This is a simple hello function.
30+
*
31+
* @param non-empty-string $firstname
32+
*
33+
* @return string
34+
*
35+
* @pure
36+
*/
37+
#[Qux(foo: 1, bar: 2)]
38+
function hello(
39+
#[Validation\NotBlank, Validation\Length(min: 2, max: 10)]
40+
string $firstname,
41+
string $lastname = Qux::Foo,
42+
): string {
43+
return 'Hello ' . $firstname . ' ' . $lastname . '!';
44+
}
45+
46+
function nothing(): void {
47+
// empty body
48+
}
49+
50+
function format(
51+
string $template,
52+
int|float|string|null ...$args,
53+
): string {
54+
return sprintf($template, ...array_map(
55+
static fn ($arg) => is_float($arg) ? number_format($arg, 2) : $arg,
56+
array_filter($args, static fn ($arg) => $arg !== null)
57+
));
58+
}
59+
60+
/**
61+
* This is an example class.
62+
*
63+
* @immutable
64+
*/
65+
abstract class Example
66+
{
67+
final const A = "Hello World!";
68+
69+
protected const B = null;
70+
71+
private const C = 1;
72+
73+
public const D = false;
74+
75+
private string $foo;
76+
77+
protected string $bar;
78+
79+
public string|int $baz = "Hello World!";
80+
81+
/**
82+
* This is a simple hello function.
83+
*
84+
* @param non-empty-string $firstname
85+
*
86+
* @return string
87+
*
88+
* @pure
89+
*/
90+
#[Qux(foo: 1, bar: 2), Qux(foo: 1, bar: 2)]
91+
function hello(
92+
string $firstname,
93+
string $lastname = Qux::Foo,
94+
): string {
95+
return 'Hello ' . $firstname . ' ' . $lastname . '!';
96+
}
97+
98+
/**
99+
* This is a simple x function.
100+
*
101+
* @pure
102+
*/
103+
#[Foo(foo: 1, bar: 2), Bar(foo: 1, bar: 2)]
104+
#[Baz, Qux]
105+
public function x(): mixed {
106+
return 'Hello!';
107+
}
108+
109+
/**
110+
* This is a simple poop function.
111+
*/
112+
public abstract function poop(): void;
113+
114+
/**
115+
* This is a simple echo function.
116+
*/
117+
public final function helloWorld(): void {
118+
echo 'Hello World!';
119+
}
120+
}
121+

examples/complete.rs

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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

Comments
 (0)