|
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
|
|
|
* @todo implement Indentation indicator 8.1.1 |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class NodeLiterals extends Node |
|
13
|
|
|
{ |
|
14
|
|
|
abstract protected function getFinalString(NodeList $list):string; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(string $nodeString, int $line) |
|
17
|
|
|
{ |
|
18
|
|
|
parent::__construct($nodeString, $line); |
|
19
|
|
|
if (isset($nodeString[1]) && in_array($nodeString[1], ['-', '+'])) { |
|
20
|
|
|
$this->identifier = $nodeString[1]; |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function add(Node $child):Node |
|
25
|
|
|
{ |
|
26
|
|
|
if (is_null($this->value)) $this->value = new NodeList(); |
|
27
|
|
|
$candidate = $child; |
|
28
|
|
|
if (!isOneOf($child, ['NodeScalar', 'NodeBlank', 'NodeComment', 'NodeQuoted'])) { |
|
29
|
|
|
$candidate = new NodeScalar($child->raw, $child->line); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
return parent::add($candidate); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected static function litteralStripLeading(NodeList &$list) |
|
35
|
|
|
{ |
|
36
|
|
|
$list->rewind(); |
|
37
|
|
|
while ($list->bottom() instanceof NodeBlank) {//remove leading blank |
|
38
|
|
|
$list->shift(); |
|
39
|
|
|
$list->rewind(); |
|
40
|
|
|
} |
|
41
|
|
|
$list->rewind(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected 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
|
|
|
* Builds a litteral (folded or not) |
|
55
|
|
|
* As per Documentation : 8.1.1.2. Block Chomping Indicator |
|
56
|
|
|
* Chomping controls how final line breaks and trailing empty lines are interpreted. |
|
57
|
|
|
* YAML provides three chomping methods: |
|
58
|
|
|
* Clip (default behavior) : FINAL_LINE_BREAK, NO TRAILING EMPTY LINES |
|
59
|
|
|
* Strip (“-” chomping indicator) NO FINAL_LINE_BREAK, NO TRAILING EMPTY LINES |
|
60
|
|
|
* Keep (“+” chomping indicator) FINAL_LINE_BREAK && TRAILING EMPTY LINES |
|
61
|
|
|
*/ |
|
62
|
|
|
public function build(&$parent = null) |
|
63
|
|
|
{ |
|
64
|
|
|
$result = ''; |
|
65
|
|
|
if (!is_null($this->_tag)) { |
|
66
|
|
|
return TagFactory::transform($this->_tag, $this->value)->build($parent); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
if (!is_null($this->value)) { |
|
69
|
|
|
$tmp = $this->getFinalString($this->value->filterComment()); |
|
|
|
|
|
|
70
|
|
|
$result = $this->identifier === '-' ? $tmp : $tmp."\n"; |
|
71
|
|
|
} |
|
72
|
|
|
if ($this->_parent instanceof NodeRoot) { |
|
73
|
|
|
$this->getRoot()->getYamlObject()->setText($result); |
|
|
|
|
|
|
74
|
|
|
} else { |
|
75
|
|
|
return $result; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function getChildValue(Node $child, int $refIndent):string |
|
80
|
|
|
{ |
|
81
|
|
|
$value = $child->value; |
|
82
|
|
|
$start = ''; |
|
83
|
|
|
if (is_null($value)) { |
|
84
|
|
|
return $child instanceof NodeQuoted ? $child->build() : ltrim($child->raw); |
|
85
|
|
|
} elseif ($value instanceof Node) { |
|
86
|
|
|
if ($child instanceof NodeKey || $child instanceof NodeItem) { |
|
87
|
|
|
$start = ltrim($child->raw)."\n"; |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
return $this->getFinalString(new NodeList($value), $refIndent); |
|
|
|
|
|
|
90
|
|
|
} elseif ($value instanceof NodeList) { |
|
|
|
|
|
|
91
|
|
|
if ($child instanceof NodeKey || $child instanceof NodeItem) { |
|
92
|
|
|
$start = ltrim($child->raw)."\n"; |
|
93
|
|
|
} |
|
94
|
|
|
return $start.$this->getFinalString($value, $refIndent); |
|
95
|
|
|
} |
|
96
|
|
|
return ''; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function isAwaitingChild(Node $node):bool |
|
100
|
|
|
{ |
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
} |