Passed
Push — master ( 5545f1...83deac )
by Kirill
03:22
created

Constant   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 96
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setName() 0 9 1
A setValue() 0 5 1
A render() 0 15 3
A mountIndents() 0 9 2
A __construct() 0 5 1
A getValue() 0 3 1
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