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 Doctrine\Inflector\Rules\English\InflectorFactory; |
15
|
|
|
use ReflectionException; |
16
|
|
|
use Spiral\Reactor\AbstractDeclaration; |
17
|
|
|
use Spiral\Reactor\NamedInterface; |
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
|
|
|
* Class constant declaration. |
25
|
|
|
*/ |
26
|
|
|
class Constant extends AbstractDeclaration implements NamedInterface |
27
|
|
|
{ |
28
|
|
|
use NamedTrait; |
29
|
|
|
use CommentTrait; |
30
|
|
|
use SerializerTrait; |
31
|
|
|
use AccessTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var mixed |
35
|
|
|
*/ |
36
|
|
|
private $value; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $name |
40
|
|
|
* @param string $value |
41
|
|
|
* @param string|array $comment |
42
|
|
|
*/ |
43
|
|
|
public function __construct(string $name, $value, $comment = '') |
44
|
|
|
{ |
45
|
|
|
$this->setName($name); |
46
|
|
|
$this->value = $value; |
47
|
|
|
$this->initComment($comment); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function setName(string $name): Constant |
54
|
|
|
{ |
55
|
|
|
$this->name = strtoupper( |
56
|
|
|
(new InflectorFactory()) |
57
|
|
|
->build() |
58
|
|
|
->tableize(strtolower($name)) |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Array values allowed (but works in PHP7 only). |
66
|
|
|
* |
67
|
|
|
* @param mixed $value |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
|
|
public function setValue($value): Constant |
71
|
|
|
{ |
72
|
|
|
$this->value = $value; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
|
|
public function getValue() |
81
|
|
|
{ |
82
|
|
|
return $this->value; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
* @throws ReflectionException |
88
|
|
|
*/ |
89
|
|
|
public function render(int $indentLevel = 0): string |
90
|
|
|
{ |
91
|
|
|
$result = ''; |
92
|
|
|
if (!$this->docComment->isEmpty()) { |
93
|
|
|
$result .= $this->docComment->render($indentLevel) . "\n"; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$result .= $this->addIndent("{$this->access} const {$this->getName()} = ", $indentLevel); |
97
|
|
|
|
98
|
|
|
$value = $this->getSerializer()->serialize($this->value); |
99
|
|
|
if (is_array($this->value)) { |
100
|
|
|
$value = $this->mountIndents($value, $indentLevel); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $result . "{$value};"; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Mount indentation to value. Attention, to be applied to arrays only! |
108
|
|
|
* |
109
|
|
|
* @param string $serialized |
110
|
|
|
* @param int $indentLevel |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
private function mountIndents(string $serialized, int $indentLevel): string |
114
|
|
|
{ |
115
|
|
|
$lines = explode("\n", $serialized); |
116
|
|
|
foreach ($lines as &$line) { |
117
|
|
|
$line = $this->addIndent($line, $indentLevel); |
118
|
|
|
unset($line); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return ltrim(implode("\n", $lines)); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|