|
1
|
|
|
<?php |
|
2
|
|
|
namespace Dallgoot\Yaml; |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
use Dallgoot\Yaml\Types as T; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* the return Object representing a YAML file content |
|
|
|
|
|
|
8
|
|
|
* consider dumping datetime as date strings according to a format provided by user or default |
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
class API |
|
11
|
|
|
{ |
|
12
|
|
|
private $_references = []; |
|
13
|
|
|
private $_comments = []; |
|
14
|
|
|
// private $_documents = []; |
|
15
|
|
|
private $tags = []; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
public $type = T::MAPPING; |
|
18
|
|
|
public $value = null; |
|
19
|
|
|
|
|
20
|
|
|
const UNKNOWN_REFERENCE = self::class.": no reference named '%s'"; |
|
21
|
|
|
const UNAMED_REFERENCE = self::class.": reference MUST have a name"; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Adds a reference. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $name The name |
|
|
|
|
|
|
28
|
|
|
* @param mixed $value The value |
|
|
|
|
|
|
29
|
|
|
* @throws \UnexpectedValueException (description) |
|
|
|
|
|
|
30
|
|
|
*/ |
|
|
|
|
|
|
31
|
|
|
public function addReference($name, $value):void |
|
32
|
|
|
{ |
|
33
|
|
|
if (empty($name)) { |
|
34
|
|
|
throw new \UnexpectedValueException(self::UNAMED_REFERENCE, 1); |
|
35
|
|
|
} |
|
36
|
|
|
$this->_references[$name] = $value; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* return the reference saved by $name |
|
|
|
|
|
|
41
|
|
|
* @param string $name of the reference |
|
|
|
|
|
|
42
|
|
|
* @return mixed the value of the reference |
|
|
|
|
|
|
43
|
|
|
* @throws UnexpectedValueException if there's no reference by that $name |
|
|
|
|
|
|
44
|
|
|
*/ |
|
45
|
|
|
public function &getReference($name) |
|
46
|
|
|
{ |
|
47
|
|
|
if (array_key_exists($name, $this->_references)) { |
|
48
|
|
|
return $this->_references[$name]; |
|
49
|
|
|
} |
|
50
|
|
|
throw new \UnexpectedValueException(sprintf(self::UNKNOWN_REFERENCE, $name), 1); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getAllReferences():array |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
return $this->_references; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function addComment($index, $value):void |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
$this->_comments[(int) $index] = $value; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getComment(int $lineNumber = null) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
if (array_key_exists((int) $lineNumber, $this->_comments)) { |
|
66
|
|
|
return $this->_comments[$lineNumber]; |
|
67
|
|
|
} |
|
68
|
|
|
return $this->_comments; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function setText(string $value):void |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$this->value .= $value; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function addTag(string $value):void |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$this->tags[] = $value; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|