1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 03.09.16 at 11:30 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonphp\generator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class property generation class. |
10
|
|
|
* |
11
|
|
|
* @author Vitaly Egorov <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class PropertyGenerator extends AbstractGenerator |
14
|
|
|
{ |
15
|
|
|
/** @var string Property name */ |
16
|
|
|
protected $name; |
17
|
|
|
|
18
|
|
|
/** @var string Property value */ |
19
|
|
|
protected $value; |
20
|
|
|
|
21
|
|
|
/** @var bool Flag that method is static */ |
22
|
|
|
protected $isStatic = false; |
23
|
|
|
|
24
|
|
|
/** @var string Method visibility */ |
25
|
|
|
protected $visibility = ClassGenerator::VISIBILITY_PUBLIC; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* PropertyGenerator constructor. |
29
|
|
|
* |
30
|
|
|
* @param string $name Property name |
31
|
|
|
* @param mixed $value Property value |
32
|
|
|
* @param AbstractGenerator|null $parent Parent generator |
33
|
|
|
*/ |
34
|
|
|
public function __construct(string $name, $value = null, AbstractGenerator $parent = null) |
35
|
|
|
{ |
36
|
|
|
$this->name = $name; |
37
|
|
|
$this->value = $value; |
38
|
|
|
|
39
|
|
|
parent::__construct($parent); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set protected property visibility. |
44
|
|
|
* |
45
|
|
|
* @return PropertyGenerator |
46
|
|
|
*/ |
47
|
|
|
public function defProtected() : PropertyGenerator |
48
|
|
|
{ |
49
|
|
|
return $this->defVisibility(ClassGenerator::VISIBILITY_PROTECTED); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set property visibility. |
54
|
|
|
* |
55
|
|
|
* @param string $visibility Property visibility |
56
|
|
|
* |
57
|
|
|
* @return PropertyGenerator |
58
|
|
|
*/ |
59
|
|
|
protected function defVisibility(string $visibility) : PropertyGenerator |
60
|
|
|
{ |
61
|
|
|
$this->visibility = $visibility; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set private property visibility. |
68
|
|
|
* |
69
|
|
|
* @return PropertyGenerator |
70
|
|
|
*/ |
71
|
|
|
public function defPrivate() : PropertyGenerator |
72
|
|
|
{ |
73
|
|
|
return $this->defVisibility(ClassGenerator::VISIBILITY_PRIVATE); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set method to be static. |
78
|
|
|
* |
79
|
|
|
* @return PropertyGenerator |
80
|
|
|
*/ |
81
|
|
|
public function defStatic() : PropertyGenerator |
82
|
|
|
{ |
83
|
|
|
$this->isStatic = true; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
* @throws \InvalidArgumentException |
91
|
|
|
*/ |
92
|
|
|
public function defLine(string $code) |
93
|
|
|
{ |
94
|
|
|
throw new \InvalidArgumentException('Property cannot have code lines'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function code(int $indentation = 0) : string |
101
|
|
|
{ |
102
|
|
|
$this->generatedCode .= $this->indentation($indentation) |
103
|
|
|
.$this->visibility |
104
|
|
|
.' ' |
105
|
|
|
.($this->isStatic ? 'static ' : '') |
106
|
|
|
.'$' |
107
|
|
|
.$this->name |
108
|
|
|
.';'; |
109
|
|
|
|
110
|
|
|
return $this->generatedCode; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|