ArrayItem   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 20
c 1
b 0
f 0
dl 0
loc 47
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A toString() 0 10 4
A setValue() 0 6 1
A setIndentLevel() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Renderer\PhpFileRenderer\Exporter;
6
7
use Cycle\Schema\Renderer\PhpFileRenderer\Exporter\Rendering\Indentable;
8
use Cycle\Schema\Renderer\PhpFileRenderer\Exporter\Rendering\ValueRenderer;
9
10
final class ArrayItem implements ExporterItem, Indentable
11
{
12
    public ?string $key;
13
14
    /** @var mixed */
15
    private $value;
16
    private bool $wrapValue = true;
17
    private bool $wrapKey;
18
    private int $indentLevel = 0;
19
20
    /**
21
     * @param mixed $value
22
     */
23
    public function __construct($value, string $key = null, bool $wrapKey = true)
24
    {
25
        $this->key = $key;
26
        $this->value = $value;
27
        $this->wrapKey = $wrapKey;
28
    }
29
30
    public function setIndentLevel(int $indentLevel = 0): self
31
    {
32
        $this->indentLevel = $indentLevel;
33
        return $this;
34
    }
35
36
    public function toString(): string
37
    {
38
        $result = \str_repeat(self::INDENT, $this->indentLevel);
39
        if ($this->key !== null) {
40
            $result = $this->wrapKey ? "'{$this->key}' => " : "{$this->key} => ";
41
        }
42
        if ($this->value instanceof Indentable) {
43
            $this->value->setIndentLevel($this->indentLevel);
44
        }
45
        return $result . ValueRenderer::render($this->value, $this->wrapValue, $this->indentLevel);
46
    }
47
48
    /**
49
     * @param mixed $value
50
     */
51
    public function setValue($value, bool $wrapValue = true): self
52
    {
53
        $this->value = $value;
54
        $this->wrapValue = $wrapValue;
55
56
        return $this;
57
    }
58
}
59