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 NodeScalar extends Node |
12
|
|
|
{ |
13
|
5 |
|
public function __construct(string $nodeString, int $line) |
14
|
|
|
{ |
15
|
5 |
|
parent::__construct($nodeString, $line); |
16
|
5 |
|
$value = trim($nodeString); |
17
|
5 |
|
if ($value !== '') { |
18
|
5 |
|
$hasComment = strpos($value, ' #'); |
19
|
5 |
|
if (!is_bool($hasComment)) { |
20
|
1 |
|
$realValue = trim(substr($value, 0, $hasComment)); |
21
|
1 |
|
$commentValue = trim(substr($value, $hasComment)); |
22
|
1 |
|
$realNode = NodeFactory::get($realValue, $line); |
23
|
1 |
|
$realNode->indent = null; |
24
|
1 |
|
$commentNode = NodeFactory::get($commentValue, $line); |
25
|
1 |
|
$commentNode->indent = null; |
26
|
1 |
|
$this->add($realNode); |
27
|
1 |
|
$this->add($commentNode); |
28
|
|
|
} |
29
|
|
|
} |
30
|
5 |
|
} |
31
|
|
|
|
32
|
2 |
|
public function build(&$parent = null) |
33
|
|
|
{ |
34
|
2 |
|
if (!is_null($this->tag)) { |
35
|
1 |
|
$tagged = TagFactory::transform($this->tag, $this); |
36
|
1 |
|
if ($tagged instanceof Node || $tagged instanceof NodeList) { |
37
|
|
|
return $tagged->build(); |
38
|
|
|
} |
39
|
1 |
|
return $tagged; |
40
|
|
|
} |
41
|
1 |
|
return is_null($this->value) ? Builder::getScalar(trim($this->raw)) : $this->value->build(); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function getTargetOnLessIndent(Node &$node):Node |
45
|
|
|
{ |
46
|
1 |
|
if ($node instanceof NodeScalar || $node instanceof NodeBlank ) { |
47
|
1 |
|
return $this->getParent(); |
48
|
|
|
} else { |
49
|
1 |
|
return $this->getParent($node->indent); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
public function getTargetOnMoreIndent(Node &$node):Node |
54
|
|
|
{ |
55
|
1 |
|
return $this->getParent(); |
56
|
|
|
} |
57
|
|
|
} |