Completed
Push — master ( c45521...844759 )
by stéphane
08:22
created

NodeLit::buildLitt()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 3
nop 2
dl 0
loc 27
rs 8.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Dallgoot\Yaml;
4
5
/**
6
 *
7
 * @author  Stéphane Rebai <[email protected]>
8
 * @license Apache 2.0
9
 * @link    TODO : url to specific online doc
10
 */
11
class NodeLit extends NodeLiterals
12
{
13
14
    public static function buildLitt(NodeList &$list, $modifier = null):string
15
    {
16
        $result = '';
17
        if ($list->count()) {
18
            if ($modifier !== '+') {
19
                 self::litteralStripLeading($list);
20
                 self::litteralStripTrailing($list);
21
            }
22
            $first = $list->shift();
23
            $refIndent = $first->indent ?? 0;
24
            $result = substr($first->raw, $first->indent);
25
            $list->setIteratorMode(NodeList::IT_MODE_DELETE);
26
            foreach ($list as $key => $child) {
27
                $noMoreContent = $list->has('NodeBlank');
28
                if ($child->value instanceof NodeList) {
29
                    $val = self::buildLitt($child->value);
30
                } else {
31
                    if ($child instanceof NodeBlank) {
32
                        $val = $noMoreContent ? "\n" : "";
33
                    } else {
34
                        $val = substr($child->raw, $refIndent);
35
                    }
36
                }
37
                $result .= "\n".$val;
38
            }
39
        }
40
        return $result;
41
    }
42
43
}