Completed
Push — master ( a22bfe...2e3ab0 )
by Mike
02:35
created

Node::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of phpDocumentor.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @link https://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Guides\Nodes;
15
16
use function implode;
17
use function is_callable;
18
use function is_string;
19
use function strlen;
20
use function substr;
21
use function trim;
22
23
abstract class Node
24
{
25
    /** @var Node|callable|string|null */
26
    protected $value;
27
28
    /** @var string[] */
29
    protected $classes = [];
30
31
    /** @var mixed[] */
32
    private $options;
33
34
    /**
35
     * @param Node|callable|string|null $value
36
     */
37
    public function __construct($value = null)
38
    {
39
        $this->value = $value;
40
    }
41
42
    /**
43
     * @return Node|callable|string|null
44
     */
45
    public function getValue()
46
    {
47
        return $this->value;
48
    }
49
50
    /**
51
     * @param Node|callable|string|null $value
52
     */
53
    public function setValue($value) : void
54
    {
55
        $this->value = $value;
56
    }
57
58
    /**
59
     * @return string[]
60
     */
61
    public function getClasses() : array
62
    {
63
        return $this->classes;
64
    }
65
66
    public function getClassesString() : string
67
    {
68
        return implode(' ', $this->classes);
69
    }
70
71
    /**
72
     * @param string[] $classes
73
     */
74
    public function setClasses(array $classes) : void
75
    {
76
        $this->classes = $classes;
77
    }
78
79
    public function getValueString() : string
80
    {
81
        if ($this->value === null) {
82
            return '';
83
        }
84
85
        if ($this->value instanceof self) {
86
            return $this->value->getValueString();
87
        }
88
89
        if (is_string($this->value)) {
90
            return $this->value;
91
        }
92
93
        if (is_callable($this->value)) {
94
            return ($this->value)();
95
        }
96
97
        return '';
98
    }
99
100
    /**
101
     * @param array<string, mixed> $options
102
     */
103
    public function withOptions(array $options) : self
104
    {
105
        $result = clone $this;
106
        $result->options = $options;
107
108
        return $result;
109
    }
110
111
    /**
112
     * @return array<string, mixed>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
113
     */
114
    public function getOptions() : array
115
    {
116
        return $this->options;
117
    }
118
119
    /**
120
     * @param mixed|null $default
121
     *
122
     * @return mixed|null
123
     */
124
    public function getOption(string $name, $default = null)
125
    {
126
        return $this->options[$name] ?? $default;
127
    }
128
129
    /**
130
     * @param string[] $lines
131
     */
132
    protected function normalizeLines(array $lines) : string
133
    {
134
        if ($lines !== []) {
135
            $firstLine = $lines[0];
136
137
            $length = strlen($firstLine);
138
            for ($k = 0; $k < $length; $k++) {
139
                if (trim($firstLine[$k]) !== '') {
140
                    break;
141
                }
142
            }
143
144
            foreach ($lines as &$line) {
145
                $line = substr($line, $k);
146
            }
147
        }
148
149
        return implode("\n", $lines);
150
    }
151
}
152