-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeFile.php
More file actions
64 lines (47 loc) · 1.21 KB
/
CodeFile.php
File metadata and controls
64 lines (47 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
declare(strict_types=1);
namespace Auroro\Code;
use Stringable;
final class CodeFile implements Stringable
{
public readonly ImportCollection $imports;
private ?string $header = null;
private readonly CodeWriter $writer;
public function __construct(
private readonly CodeStyle $style = new CodeStyle(),
) {
$this->imports = new ImportCollection();
$this->writer = new CodeWriter($this->style);
}
/**
* Set the file header comment/text.
*/
public function header(string $text): self
{
$this->header = $text;
return $this;
}
/**
* Get the body writer for building file content.
*/
public function body(): CodeWriter
{
return $this->writer;
}
public function __toString(): string
{
$sections = [];
if (null !== $this->header) {
$sections[] = $this->header;
}
$rendered = $this->imports->render();
if ('' !== $rendered) {
$sections[] = $rendered;
}
$body = (string) $this->writer;
if ('' !== $body) {
$sections[] = $body;
}
return rtrim(implode("\n\n", $sections));
}
}