|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Reactor\Partial; |
|
13
|
|
|
|
|
14
|
|
|
use ReflectionException; |
|
15
|
|
|
use Spiral\Reactor\AbstractDeclaration; |
|
16
|
|
|
use Spiral\Reactor\NamedInterface; |
|
17
|
|
|
use Spiral\Reactor\ReplaceableInterface; |
|
18
|
|
|
use Spiral\Reactor\Traits\AccessTrait; |
|
19
|
|
|
use Spiral\Reactor\Traits\CommentTrait; |
|
20
|
|
|
use Spiral\Reactor\Traits\NamedTrait; |
|
21
|
|
|
use Spiral\Reactor\Traits\SerializerTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Declares property element. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Property extends AbstractDeclaration implements ReplaceableInterface, NamedInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use NamedTrait; |
|
29
|
|
|
use CommentTrait; |
|
30
|
|
|
use SerializerTrait; |
|
31
|
|
|
use AccessTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
private $hasDefault = false; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var mixed |
|
40
|
|
|
*/ |
|
41
|
|
|
private $defaultValue; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $name |
|
45
|
|
|
* @param mixed $defaultValue |
|
46
|
|
|
* @param string|array $comment |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(string $name, $defaultValue = null, $comment = '') |
|
49
|
|
|
{ |
|
50
|
|
|
$this->setName($name); |
|
51
|
|
|
if ($defaultValue !== null) { |
|
52
|
|
|
$this->setDefaultValue($defaultValue); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->initComment($comment); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Has default value. |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
|
public function hasDefaultValue(): bool |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->hasDefault; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set default value. |
|
70
|
|
|
* |
|
71
|
|
|
* @param mixed $value |
|
72
|
|
|
* @return self |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setDefaultValue($value): Property |
|
75
|
|
|
{ |
|
76
|
|
|
$this->hasDefault = true; |
|
77
|
|
|
$this->defaultValue = $value; |
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Remove default value. |
|
84
|
|
|
* |
|
85
|
|
|
* @return self |
|
86
|
|
|
*/ |
|
87
|
|
|
public function removeDefaultValue(): Property |
|
88
|
|
|
{ |
|
89
|
|
|
$this->hasDefault = false; |
|
90
|
|
|
$this->defaultValue = null; |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getDefaultValue() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->defaultValue; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Replace comments. |
|
105
|
|
|
* |
|
106
|
|
|
* @param array|string $search |
|
107
|
|
|
* @param array|string $replace |
|
108
|
|
|
*/ |
|
109
|
|
|
public function replace($search, $replace): void |
|
110
|
|
|
{ |
|
111
|
|
|
$this->docComment->replace($search, $replace); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
* @throws ReflectionException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function render(int $indentLevel = 0): string |
|
119
|
|
|
{ |
|
120
|
|
|
$result = ''; |
|
121
|
|
|
if (!$this->docComment->isEmpty()) { |
|
122
|
|
|
$result .= $this->docComment->render($indentLevel) . "\n"; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$result .= $this->addIndent("{$this->access} \${$this->getName()}", $indentLevel); |
|
126
|
|
|
|
|
127
|
|
|
if ($this->hasDefault) { |
|
128
|
|
|
$value = $this->getSerializer()->serialize($this->defaultValue); |
|
129
|
|
|
|
|
130
|
|
|
if (is_array($this->defaultValue)) { |
|
131
|
|
|
$value = $this->mountIndents($value, $indentLevel); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$result .= " = {$value};"; |
|
135
|
|
|
} else { |
|
136
|
|
|
$result .= ';'; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $result; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Mount indentation to value. Attention, to be applied to arrays only! |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $serialized |
|
146
|
|
|
* @param int $indentLevel |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
private function mountIndents(string $serialized, int $indentLevel): string |
|
150
|
|
|
{ |
|
151
|
|
|
$lines = explode("\n", $serialized); |
|
152
|
|
|
foreach ($lines as &$line) { |
|
153
|
|
|
$line = $this->addIndent($line, $indentLevel); |
|
154
|
|
|
unset($line); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return ltrim(implode("\n", $lines)); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|