1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Murtukov\PHPCodeGenerator; |
6
|
|
|
|
7
|
|
|
class PhpTrait extends OOPStructure |
8
|
|
|
{ |
9
|
1 |
|
public function addProperty(string $name, string $modifier = Modifier::PUBLIC, string $type = '', $defaulValue = Property::NO_PARAM): self |
10
|
|
|
{ |
11
|
1 |
|
return $this->append(new Property($name, $modifier, $type, $defaulValue)); |
12
|
|
|
} |
13
|
|
|
|
14
|
1 |
|
public function createProperty(string $name, string $modifier = Modifier::PUBLIC, string $type = '', $defaulValue = Property::NO_PARAM) |
15
|
|
|
{ |
16
|
1 |
|
$property = new Property($name, $modifier, $type, $defaulValue); |
17
|
|
|
|
18
|
1 |
|
$this->append($property); |
19
|
|
|
|
20
|
1 |
|
return $property; |
21
|
|
|
} |
22
|
|
|
|
23
|
1 |
|
public function addMethod(string $name, string $modifier = 'public', string $returnType = ''): self |
24
|
|
|
{ |
25
|
1 |
|
return $this->append(new Method($name, $modifier, $returnType))->emptyLine(); |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
public function createMethod(string $name, string $modifier = 'public', string $returnType = ''): Method |
29
|
|
|
{ |
30
|
1 |
|
$method = new Method($name, $modifier, $returnType); |
31
|
|
|
|
32
|
1 |
|
$this->append($method)->emptyLine(); |
33
|
|
|
|
34
|
1 |
|
return $method; |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
public function createConstructor(string $modifier = 'public'): Method |
38
|
|
|
{ |
39
|
1 |
|
$constructor = new Method('__construct', $modifier, ''); |
40
|
|
|
|
41
|
1 |
|
$this->append($constructor)->emptyLine(); |
42
|
|
|
|
43
|
1 |
|
return $constructor; |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
public function generate(): string |
47
|
|
|
{ |
48
|
|
|
return <<<CODE |
49
|
4 |
|
{$this->buildDocBlock()}trait $this->name |
50
|
|
|
{ |
51
|
4 |
|
{$this->generateContent()} |
52
|
|
|
} |
53
|
|
|
CODE; |
54
|
|
|
} |
55
|
|
|
} |