|
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
|
|
|
*/ |
|
91
|
|
|
public function code(int $indentation = 0) : string |
|
92
|
|
|
{ |
|
93
|
|
|
$this->generatedCode .= $this->indentation($indentation) |
|
94
|
|
|
.$this->visibility |
|
95
|
|
|
.' ' |
|
96
|
|
|
.($this->isStatic ? 'static ' : '') |
|
97
|
|
|
.'$' |
|
98
|
|
|
.$this->name |
|
99
|
|
|
.';'; |
|
100
|
|
|
|
|
101
|
|
|
return $this->generatedCode; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|