1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Murtukov\PHPCodeGenerator; |
6
|
|
|
|
7
|
|
|
class Comment extends AbstractGenerator implements BlockInterface |
8
|
|
|
{ |
9
|
|
|
public const TYPE_STAR = 'Star'; |
10
|
|
|
public const TYPE_DOCBLOCK = 'DocBlock'; |
11
|
|
|
public const TYPE_HASH = 'Hash'; |
12
|
|
|
public const TYPE_SLASH = 'Slash'; |
13
|
|
|
|
14
|
|
|
protected string $type; |
15
|
|
|
protected array $lines = []; |
16
|
|
|
|
17
|
11 |
|
private function __construct(string $text = '', string $type = self::TYPE_STAR) |
18
|
|
|
{ |
19
|
11 |
|
$this->addText($text); |
20
|
11 |
|
$this->type = $type; |
21
|
11 |
|
} |
22
|
|
|
|
23
|
4 |
|
public static function block(string $text = ''): self |
24
|
|
|
{ |
25
|
4 |
|
return new self($text, self::TYPE_STAR); |
26
|
|
|
} |
27
|
|
|
|
28
|
3 |
|
public static function hash(string $text = ''): self |
29
|
|
|
{ |
30
|
3 |
|
return new self($text, self::TYPE_HASH); |
31
|
|
|
} |
32
|
|
|
|
33
|
6 |
|
public static function docBlock(string $text = ''): self |
34
|
|
|
{ |
35
|
6 |
|
return new self($text, self::TYPE_DOCBLOCK); |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
public static function slash(string $text = ''): self |
39
|
|
|
{ |
40
|
2 |
|
return new self($text, self::TYPE_SLASH); |
41
|
|
|
} |
42
|
|
|
|
43
|
12 |
|
public function generate(): string |
44
|
|
|
{ |
45
|
12 |
|
return $this->{"build$this->type"}(); |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
private function buildStar() |
49
|
|
|
{ |
50
|
3 |
|
$lines = implode("\n * ", $this->lines); |
51
|
|
|
|
52
|
|
|
return <<<CODE |
53
|
3 |
|
/* |
54
|
3 |
|
* $lines |
55
|
|
|
*/ |
56
|
|
|
CODE; |
57
|
|
|
} |
58
|
|
|
|
59
|
7 |
|
private function buildDocBlock() |
60
|
|
|
{ |
61
|
7 |
|
$lines = implode("\n * ", $this->lines); |
62
|
|
|
|
63
|
|
|
return <<<CODE |
64
|
7 |
|
/** |
65
|
7 |
|
* $lines |
66
|
|
|
*/ |
67
|
|
|
CODE; |
68
|
|
|
} |
69
|
|
|
|
70
|
3 |
|
private function buildHash() |
71
|
|
|
{ |
72
|
3 |
|
return '# '.implode("\n# ", $this->lines); |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
private function buildSlash() |
76
|
|
|
{ |
77
|
2 |
|
return '// '.implode("\n// ", $this->lines); |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
public function addLine(string $text): self |
81
|
|
|
{ |
82
|
3 |
|
$this->lines[] = $text; |
83
|
|
|
|
84
|
3 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
public function addEmptyLine(): self |
88
|
|
|
{ |
89
|
2 |
|
$this->lines[] = ''; |
90
|
|
|
|
91
|
2 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
11 |
|
public function addText(string $text): self |
95
|
|
|
{ |
96
|
11 |
|
if ('' === $text) { |
97
|
1 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
10 |
|
$parts = explode("\n", $text); |
101
|
10 |
|
$this->lines = [...$this->lines, ...$parts]; |
102
|
|
|
|
103
|
10 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|