1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dallgoot\Yaml\Nodes\Generic; |
4
|
|
|
|
5
|
|
|
use Dallgoot\Yaml\Nodes\Generic\NodeGeneric; |
6
|
|
|
use Dallgoot\Yaml\NodeList; |
7
|
|
|
use Dallgoot\Yaml\Tag\TagFactory; |
8
|
|
|
use Dallgoot\Yaml\Types\Tagged; |
9
|
|
|
use Dallgoot\Yaml\Nodes\Blank; |
10
|
|
|
use Dallgoot\Yaml\Nodes\Quoted; |
11
|
|
|
use Dallgoot\Yaml\Nodes\Scalar; |
12
|
|
|
use Dallgoot\Yaml\Nodes\Root; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
* @author Stéphane Rebai <[email protected]> |
17
|
|
|
* @license Apache 2.0 |
18
|
|
|
* @link https://github.com/dallgoot/yaml |
19
|
|
|
* @todo implement Indentation indicator 8.1.1 |
20
|
|
|
*/ |
21
|
|
|
abstract class Literals extends NodeGeneric |
22
|
|
|
{ |
23
|
|
|
/** @var NodeList */ |
24
|
|
|
public $value; |
25
|
|
|
public $identifier; |
26
|
|
|
|
27
|
|
|
abstract protected function getFinalString(NodeList $list, int $refIndent = null): string; |
28
|
|
|
|
29
|
|
|
public function __construct(string $nodeString, int $line) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($nodeString, $line); |
32
|
|
|
if (isset($nodeString[1]) && in_array($nodeString[1], ['-', '+'])) { |
33
|
|
|
$this->identifier = $nodeString[1]; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function add(NodeGeneric $child): NodeGeneric |
38
|
|
|
{ |
39
|
|
|
if (is_null($this->value)) $this->value = new NodeList(); |
40
|
|
|
$candidate = $child; |
41
|
|
|
if (!$child->isOneOf('Scalar', 'Blank', 'Comment', 'Quoted')) { |
42
|
|
|
$candidate = new Scalar((string) $child->raw, $child->line); |
43
|
|
|
} |
44
|
|
|
return parent::add($candidate); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected static function litteralStripLeading(NodeList &$list): void |
48
|
|
|
{ |
49
|
|
|
$list->rewind(); |
50
|
|
|
while (!$list->isEmpty() && $list->bottom() instanceof Blank) { //remove leading blank |
51
|
|
|
$list->shift(); |
52
|
|
|
} |
53
|
|
|
$list->rewind(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected static function litteralStripTrailing(NodeList &$list): void |
57
|
|
|
{ |
58
|
|
|
$list->rewind(); |
59
|
|
|
while (!$list->isEmpty() && $list->top() instanceof Blank) { //remove trailing blank |
60
|
|
|
$list->pop(); |
61
|
|
|
} |
62
|
|
|
$list->rewind(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Builds a litteral (folded or not) |
67
|
|
|
* As per Documentation : 8.1.1.2. Block Chomping Indicator |
68
|
|
|
* Chomping controls how final line breaks and trailing empty lines are interpreted. |
69
|
|
|
* YAML provides three chomping methods: |
70
|
|
|
* Clip (default behavior) : FINAL_LINE_BREAK, NO TRAILING EMPTY LINES |
71
|
|
|
* Strip (“-” chomping indicator) NO FINAL_LINE_BREAK, NO TRAILING EMPTY LINES |
72
|
|
|
* Keep (“+” chomping indicator) FINAL_LINE_BREAK && TRAILING EMPTY LINES |
73
|
|
|
*/ |
74
|
|
|
public function build(&$parent = null) |
75
|
|
|
{ |
76
|
|
|
$result = ''; |
77
|
|
|
if (!is_null($this->tag)) { |
78
|
|
|
$output = TagFactory::transform($this->tag, $this->value); |
79
|
|
|
return $output instanceof Tagged ? $output : $output->build($parent); |
80
|
|
|
} |
81
|
|
|
if (!is_null($this->value)) { |
82
|
|
|
$tmp = $this->getFinalString($this->value->filterComment()); |
83
|
|
|
$result = $this->identifier === '-' ? $tmp : $tmp . "\n"; |
84
|
|
|
} |
85
|
|
|
if ($this->_parent instanceof Root) { |
86
|
|
|
$this->_parent->getYamlObject()->setText($result); |
|
|
|
|
87
|
|
|
return null; |
88
|
|
|
} else { |
89
|
|
|
return $result; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Gets the correct string for child value. |
95
|
|
|
* |
96
|
|
|
* @todo double check behaviour for KEY and ITEM |
97
|
|
|
*/ |
98
|
|
|
protected function getChildValue(NodeGeneric $child, ?int $refIndent = 0): string |
99
|
|
|
{ |
100
|
|
|
$value = $child->value; |
101
|
|
|
$start = ''; |
102
|
|
|
if (is_null($value)) { |
103
|
|
|
if ($child instanceof Quoted) { |
104
|
|
|
return $child->build(); |
105
|
|
|
} elseif ($child instanceof Blank) { |
106
|
|
|
return ''; |
107
|
|
|
} else { |
108
|
|
|
return ltrim($child->raw); |
109
|
|
|
} |
110
|
|
|
} elseif ($value instanceof Scalar) { |
111
|
|
|
$value = new NodeList($value); |
112
|
|
|
} elseif ($value instanceof NodeList && !($child instanceof Scalar)) { |
113
|
|
|
$start = ltrim($child->raw) . "\n"; |
114
|
|
|
} |
115
|
|
|
return $start . $this->getFinalString($value, $refIndent); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function isAwaitingChild(NodeGeneric $node): bool |
119
|
|
|
{ |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|