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

NodeLiterals::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 2
dl 0
loc 5
rs 10
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 NodeLiterals extends Node
12
{
13
    public function __construct(string $nodeString, int $line)
14
    {
15
        parent::__construct($nodeString, $line);
16
        if (isset($nodeString[1]) && in_array($nodeString[1], ['-', '+'])) {
17
            $this->identifier = $nodeString[1];
18
        }
19
    }
20
21
    public function add(Node $child)
22
    {
23
        $realValue = new NodeScalar(ltrim($child->raw), $child->line);
24
        $this->value = new NodeList();
25
        parent::add($realValue);
26
    }
27
28
    public function isAwaitingChildren()
29
    {
30
        return is_null($this->value);
31
    }
32
33
34
    public static function litteralStripLeading(NodeList &$list)
35
    {
36
        $list->rewind();
37
        while ($list->bottom() instanceof NodeBlank) {//remove trailing blank
38
            $list->shift();
39
            $list->rewind();
40
        }
41
    }
42
43
44
    public static function litteralStripTrailing(NodeList &$list)
45
    {
46
        $list->rewind();
47
        while ($list->top() instanceof NodeBlank) {//remove trailing blank
48
            $list->pop();
49
        }
50
        $list->rewind();
51
    }
52
53
54
    /**
55
     * Builds a litteral (folded or not) or any NodeList that has YAML::RAW type (like a multiline value)
56
     * As per Documentation : 8.1.1.2. Block Chomping Indicator
57
     * Chomping controls how final line breaks and trailing empty lines are interpreted.
58
     * YAML provides three chomping methods:
59
     *   Clip (default behavior)  : FINAL_LINE_BREAK, NO TRAILING EMPTY LINES
60
     *   Strip (“-” chomping indicator)  NO FINAL_LINE_BREAK, NO TRAILING EMPTY LINES
61
     *   Keep (“+” chomping indicator)  FINAL_LINE_BREAK && TRAILING EMPTY LINES
62
     *
63
     * @param NodeList $list The children
64
     *
65
     * @return string    The litteral.
66
     * @todo   Example 6.1. Indentation Spaces  spaces must be considered as content
67
     */
68
    public function buildLitteral(NodeList &$list):string
69
    {
70
        $result = '';
71
        if ($this instanceof NodeLit) {
72
            return self::buildLitt($list);
0 ignored issues
show
Bug introduced by
The method buildLitt() does not exist on Dallgoot\Yaml\NodeLiterals. Did you maybe mean buildLitteral()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
            return self::/** @scrutinizer ignore-call */ buildLitt($list);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
        }
74
        if ($this instanceof NodeRaw) {
75
            return self::buildRaw($list);
0 ignored issues
show
Bug introduced by
The method buildRaw() does not exist on Dallgoot\Yaml\NodeLiterals. Did you maybe mean build()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            return self::/** @scrutinizer ignore-call */ buildRaw($list);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
        }
77
        if ($list->count()) {
78
            if ($this->modifier !== '+') {
0 ignored issues
show
Bug Best Practice introduced by
The property modifier does not exist on Dallgoot\Yaml\NodeLiterals. Did you maybe forget to declare it?
Loading history...
79
                 self::litteralStripLeading($list);
80
                 self::litteralStripTrailing($list);
81
            }
82
            $first = $list->shift();
83
            $refIndent = $first->indent ?? 0;
84
            // $refSeparator = [ Y::RAW => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][$type];
85
            $refSeparator = ' ';
86
            $result = substr($first->raw, $first->indent);
87
            foreach ($list as $child) {
88
                if ($this instanceof NodeLitFolded) {
89
                    if($child->indent > $refIndent || ($child instanceof NodeBlank)) {
90
                        $separator = "\n";
91
                    } else {
92
                        $separator = !empty($result) && $result[-1] === "\n" ? '' : $refSeparator;
93
                    }
94
                } else {
95
                    $separator = $refSeparator;
96
                }
97
                $val = '';
98
                if ($child->value instanceof NodeList) {
99
                    $val = "\n".self::buildLitteral($child->value);
0 ignored issues
show
Bug Best Practice introduced by
The method Dallgoot\Yaml\NodeLiterals::buildLitteral() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
                    $val = "\n".self::/** @scrutinizer ignore-call */ buildLitteral($child->value);
Loading history...
100
                } else {
101
                    if ($child instanceof NodeScalar) {
102
                        $val = $child->value;
103
                    } /*else {
104
                        $cursor = $child;
105
                        $val    = substr($child->raw, $child->indent);
106
                        // while ($cursor->value instanceof Node) {
107
                        //     $val .= substr($cursor->raw, $cursor->indent);
108
                        //     $cursor = $cursor->value;
109
                        // }
110
                        // $val .= $cursor->value;
111
                    }*/
112
                }
113
                $result .= $separator .$val;
114
            }
115
        }
116
        return $result.($this->modifier === '-' ? "" : "\n");
0 ignored issues
show
Bug introduced by
The property modifier does not seem to exist on Dallgoot\Yaml\NodeLitFolded.
Loading history...
117
    }
118
119
120
121
}