|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dallgoot\Yaml\Nodes; |
|
4
|
|
|
|
|
5
|
|
|
use Dallgoot\Yaml\NodeList; |
|
6
|
|
|
use Dallgoot\Yaml\Tag\TagFactory; |
|
7
|
|
|
use Dallgoot\Yaml\Types\Tagged; |
|
8
|
|
|
use Dallgoot\Yaml\Regex; |
|
9
|
|
|
use Dallgoot\Yaml\Nodes\Generic\NodeGeneric; |
|
10
|
|
|
use Dallgoot\Yaml\Nodes\Generic\Actions; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* |
|
14
|
|
|
* @author Stéphane Rebai <[email protected]> |
|
15
|
|
|
* @license Apache 2.0 |
|
16
|
|
|
* @link https://github.com/dallgoot/yaml |
|
17
|
|
|
* @todo handle tags like <tag:clarkevans.com,2002:invoice> |
|
18
|
|
|
*/ |
|
19
|
|
|
class Tag extends Actions |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
1 |
|
public function isAwaitingChild(NodeGeneric $node): bool |
|
23
|
|
|
{ |
|
24
|
1 |
|
return is_null($this->value); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
public function getTargetOnEqualIndent(NodeGeneric &$node): NodeGeneric |
|
28
|
|
|
{ |
|
29
|
1 |
|
if (is_null($this->value) && $this->indent > 0) { |
|
30
|
|
|
return $this; |
|
31
|
|
|
} else { |
|
32
|
1 |
|
return $this->getParent(); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Builds a tag and its value (also built) and encapsulates them in a Tag object. |
|
38
|
|
|
* |
|
39
|
|
|
* @param array|object|null $parent The parent |
|
40
|
|
|
* |
|
41
|
|
|
* @return Tagged|mixed The tag object of class Dallgoot\Yaml\Types\Tagged. |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function build(&$parent = null) |
|
44
|
|
|
{ |
|
45
|
1 |
|
if (is_null($this->value) && $this->getParent() instanceof Root) { |
|
46
|
|
|
if (!preg_match(Regex::TAG_PARTS, (string) $this->tag, $matches)) { |
|
47
|
|
|
throw new \UnexpectedValueException("Tag '$this->tag' is invalid", 1); |
|
48
|
|
|
} |
|
49
|
|
|
$handle = $matches['handle']; |
|
50
|
|
|
$tagname = $matches['tagname']; |
|
51
|
|
|
$this->getRoot()->getYamlObject()->addTag($handle, $tagname); |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
1 |
|
$value = $this->value; |
|
55
|
1 |
|
if (is_null($parent) && $value instanceof NodeGeneric && $value->isOneOf('Item', 'Key')) { |
|
56
|
|
|
$value = new NodeList( |
|
57
|
|
|
/** @scrutinizer ignore-type */ |
|
58
|
|
|
$value |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
try { |
|
62
|
1 |
|
$transformed = TagFactory::transform((string) $this->tag, $value, $parent); |
|
63
|
1 |
|
return $transformed; |
|
64
|
1 |
|
} catch (\UnexpectedValueException $e) { |
|
65
|
1 |
|
return new Tagged((string) $this->tag, is_null($value) ? null : ((object) $this->value)->build($parent)); |
|
66
|
|
|
} catch (\Throwable $e) { |
|
67
|
|
|
throw new \Exception("Tagged value could not be transformed for tag '$this->tag'", 1, $e);; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|