1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dallgoot\Yaml; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Provides the methods available to interact with a Yaml Object : a Yaml Document |
7
|
|
|
* |
8
|
|
|
* @author Stéphane Rebai <[email protected]> |
9
|
|
|
* @license Apache 2.0 |
10
|
|
|
* @link https://github.com/dallgoot/yaml |
11
|
|
|
*/ |
12
|
|
|
class API |
13
|
|
|
{ |
14
|
|
|
/** @var null|boolean */ |
15
|
|
|
private $_hasDocStart; // null = no docstart, true = docstart before document comments, false = docstart after document comments |
16
|
|
|
/** @var null|YamlObject */ |
17
|
|
|
private $_obj; |
18
|
|
|
/** @var array */ |
19
|
|
|
private $_anchors = []; |
20
|
|
|
/** @var array */ |
21
|
|
|
private $_comments = []; |
22
|
|
|
/** @var array */ |
23
|
|
|
private $_tags = []; |
24
|
|
|
|
25
|
|
|
/** @var null|string */ |
26
|
|
|
public $value; |
27
|
|
|
|
28
|
|
|
const UNKNOWN_REFERENCE = "no reference named: '%s', known are : (%s)"; |
29
|
|
|
const UNAMED_REFERENCE = "reference MUST have a name"; |
30
|
|
|
const TAGHANDLE_DUPLICATE = "Tag handle '%s' already declared before, handle must be unique"; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Creates API object to be used for the document provided as argument |
34
|
|
|
* |
35
|
|
|
* @param YamlObject $obj the YamlObject as the target for all methods call that needs it |
36
|
|
|
*/ |
37
|
10 |
|
public function __construct(YamlObject $obj) |
38
|
|
|
{ |
39
|
10 |
|
$this->_obj = $obj; |
40
|
10 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Adds a reference. |
44
|
|
|
* |
45
|
|
|
* @param string $name The reference name |
46
|
|
|
* @param mixed $value The reference value |
47
|
|
|
* |
48
|
|
|
* @throws \UnexpectedValueException (description) |
49
|
|
|
* @return null |
50
|
|
|
*/ |
51
|
3 |
|
public function addReference(string $name, $value) |
52
|
|
|
{ |
53
|
3 |
|
if (empty($name)) { |
54
|
|
|
throw new \UnexpectedValueException(self::UNAMED_REFERENCE); |
55
|
|
|
} |
56
|
3 |
|
$this->_anchors[$name] = $value; |
57
|
3 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return the reference saved by $name |
61
|
|
|
* |
62
|
|
|
* @param string $name Name of the reference |
63
|
|
|
* |
64
|
|
|
* @return mixed Value of the reference |
65
|
|
|
* @throws \UnexpectedValueException if there's no reference by that $name |
66
|
|
|
*/ |
67
|
2 |
|
public function &getReference($name) |
68
|
|
|
{ |
69
|
2 |
|
if (array_key_exists($name, $this->_anchors)) { |
70
|
2 |
|
return $this->_anchors[$name]; |
71
|
|
|
} |
72
|
|
|
throw new \UnexpectedValueException(sprintf(self::UNKNOWN_REFERENCE, |
73
|
|
|
$name, implode(',',array_keys($this->_anchors))) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return array with all references as Keys and their values, declared for this YamlObject |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
3 |
|
public function getAllReferences():array |
83
|
|
|
{ |
84
|
3 |
|
return $this->_anchors; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Adds a comment. |
89
|
|
|
* |
90
|
|
|
* @param int $lineNumber The line number at which the comment should appear |
91
|
|
|
* @param string $value The comment |
92
|
|
|
* |
93
|
|
|
* @return null |
94
|
|
|
*/ |
95
|
2 |
|
public function addComment(int $lineNumber, string $value) |
96
|
|
|
{ |
97
|
2 |
|
$this->_comments[$lineNumber] = $value; |
98
|
2 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Gets the comment at $lineNumber |
102
|
|
|
* |
103
|
|
|
* @param int|null $lineNumber The line number |
104
|
|
|
* |
105
|
|
|
* @return string|array The comment at $lineNumber OR all comments. |
106
|
|
|
*/ |
107
|
2 |
|
public function getComment(int $lineNumber = null) |
108
|
|
|
{ |
109
|
2 |
|
if (array_key_exists((int) $lineNumber, $this->_comments)) { |
110
|
2 |
|
return $this->_comments[$lineNumber]; |
111
|
|
|
} |
112
|
2 |
|
return $this->_comments; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Sets the text when the content is *only* a literal |
117
|
|
|
* |
118
|
|
|
* @param string $value The value |
119
|
|
|
* |
120
|
|
|
* @return YamlObject |
121
|
|
|
*/ |
122
|
1 |
|
public function setText(string $value):YamlObject |
123
|
|
|
{ |
124
|
1 |
|
$this->value .= ltrim($value); |
125
|
1 |
|
return $this->_obj; |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* TODO: what to do with these tags ??? |
130
|
|
|
* Adds a tag. |
131
|
|
|
* |
132
|
|
|
* @param string $handle The handle declared for the tag |
133
|
|
|
* @param string $prefix The prefix/namespace/schema that defines the tag |
134
|
|
|
* |
135
|
|
|
* @return null |
136
|
|
|
*/ |
137
|
2 |
|
public function addTag(string $handle, string $prefix) |
138
|
|
|
{ |
139
|
|
|
// It is an error to specify more than one “TAG” directive for the same handle in the same document, even if both occurrences give the same prefix. |
140
|
2 |
|
if (array_key_exists($handle, $this->_tags)) { |
141
|
|
|
throw new \Exception(sprintf(self::TAGHANDLE_DUPLICATE, $handle), 1); |
142
|
|
|
} |
143
|
2 |
|
$this->_tags[$handle] = $prefix; |
144
|
2 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Determines if it has YAML document start string => '---'. |
148
|
|
|
* |
149
|
|
|
* @return boolean True if document has start, False otherwise. |
150
|
|
|
*/ |
151
|
2 |
|
public function hasDocStart():bool |
152
|
|
|
{ |
153
|
2 |
|
return is_bool($this->_hasDocStart); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Sets the document start. |
158
|
|
|
* |
159
|
|
|
* @param null|bool $value The value : null = no docstart, true = docstart before document comments, false = docstart after document comments |
160
|
|
|
* |
161
|
|
|
* @return null |
162
|
|
|
*/ |
163
|
2 |
|
public function setDocStart($value) |
164
|
|
|
{ |
165
|
2 |
|
$this->_hasDocStart = $value; |
166
|
2 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Is the whole YAML document (YamlObject) tagged ? |
170
|
|
|
* |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
2 |
|
public function isTagged() |
174
|
|
|
{ |
175
|
2 |
|
return !empty($this->_tags); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|