|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dallgoot\Yaml; |
|
4
|
|
|
|
|
5
|
|
|
use Dallgoot\Yaml\Yaml as Y; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* TODO |
|
9
|
|
|
* |
|
10
|
|
|
* @author Stéphane Rebai <[email protected]> |
|
11
|
|
|
* @license Apache 2.0 |
|
12
|
|
|
* @link TODO : url to specific online doc |
|
13
|
|
|
*/ |
|
14
|
|
|
class API |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var null|bool */ |
|
17
|
|
|
private $_hasDocStart; // null = no docstart, true = docstart before document comments, false = docstart after document comments |
|
18
|
|
|
/** @var null|YamlObject */ |
|
19
|
|
|
private $_obj; |
|
20
|
|
|
private $_references = []; |
|
21
|
|
|
private $_comments = []; |
|
22
|
|
|
// private $_documents = []; |
|
23
|
|
|
private $_tags = []; |
|
24
|
|
|
/** @var null|int */ |
|
25
|
|
|
public $type = Y::MAPPING; |
|
26
|
|
|
/** @var null|string */ |
|
27
|
|
|
public $value; |
|
28
|
|
|
|
|
29
|
|
|
const UNKNOWN_REFERENCE = "no reference named: '%s'"; |
|
30
|
|
|
const UNAMED_REFERENCE = "reference MUST have a name"; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
public function __construct(YamlObject $obj) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->_obj = $obj; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Adds a reference. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $name The reference name |
|
42
|
|
|
* @param mixed $value The reference value |
|
43
|
|
|
* |
|
44
|
|
|
* @throws \UnexpectedValueException (description) |
|
45
|
|
|
*/ |
|
46
|
|
|
public function addReference(string $name, $value) |
|
47
|
|
|
{ |
|
48
|
|
|
if (empty($name)) { |
|
49
|
|
|
throw new \UnexpectedValueException(self::UNAMED_REFERENCE, 1); |
|
50
|
|
|
} |
|
51
|
|
|
$this->_references[$name] = $value; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Return the reference saved by $name |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $name Name of the reference |
|
58
|
|
|
* |
|
59
|
|
|
* @return mixed Value of the reference |
|
60
|
|
|
* @throws \UnexpectedValueException if there's no reference by that $name |
|
61
|
|
|
*/ |
|
62
|
|
|
public function &getReference($name) |
|
63
|
|
|
{ |
|
64
|
|
|
if (array_key_exists($name, $this->_references)) { |
|
65
|
|
|
return $this->_references[$name]; |
|
66
|
|
|
} |
|
67
|
|
|
throw new \UnexpectedValueException(sprintf(self::UNKNOWN_REFERENCE, $name), 1); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getAllReferences():array |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->_references; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Adds a comment. |
|
77
|
|
|
* |
|
78
|
|
|
* @param int $lineNumber The line number at which thecomment should appear |
|
79
|
|
|
* @param string $value The comment |
|
80
|
|
|
*/ |
|
81
|
|
|
public function addComment(int $lineNumber, $value) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->_comments[$lineNumber] = $value; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Gets the comment at $lineNumber |
|
88
|
|
|
* |
|
89
|
|
|
* @param int|null $lineNumber The line number |
|
90
|
|
|
* |
|
91
|
|
|
* @return string|array The comment at $lineNumber OR ALL comments. |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getComment(int $lineNumber = null) |
|
94
|
|
|
{ |
|
95
|
|
|
if (array_key_exists((int) $lineNumber, $this->_comments)) { |
|
96
|
|
|
return $this->_comments[$lineNumber]; |
|
97
|
|
|
} |
|
98
|
|
|
return $this->_comments; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sets the text when the content is *only* a litteral |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $value The value |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setText(string $value) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->value .= ltrim($value); |
|
109
|
|
|
return $this->_obj; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* TODO: what to do with these tags ??? |
|
114
|
|
|
* Adds a tag. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $value The value |
|
117
|
|
|
*/ |
|
118
|
|
|
public function addTag(string $value) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->_tags[] = $value; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Determines if it has YAML document start string => '---'. |
|
125
|
|
|
* |
|
126
|
|
|
* @return boolean True if has document start, False otherwise. |
|
127
|
|
|
*/ |
|
128
|
|
|
public function hasDocStart() |
|
129
|
|
|
{ |
|
130
|
|
|
return is_bool($this->_hasDocStart); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Sets the document start. |
|
135
|
|
|
* |
|
136
|
|
|
* @param null|bool $value The value : null = no docstart, true = docstart before document comments, false = docstart after document comments |
|
137
|
|
|
* |
|
138
|
|
|
* @return null |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setDocStart($value) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->_hasDocStart = $value; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function isTagged() |
|
146
|
|
|
{ |
|
147
|
|
|
return !empty($this->_tags); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|