|
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); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
if ($this instanceof NodeRaw) { |
|
75
|
|
|
return self::buildRaw($list); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
if ($list->count()) { |
|
78
|
|
|
if ($this->modifier !== '+') { |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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"); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
} |
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.