Test Failed
Push — master ( 529ac3...bfbdcf )
by stéphane
06:08
created

API::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
    /** @var int*/
25
    private $_options;
26
    /** @var null|string */
27
    public $value;
28
29
    const UNKNOWN_REFERENCE = "no reference named: '%s', known are : (%s)";
30
    const UNAMED_REFERENCE  = "reference MUST have a name";
31
    const TAGHANDLE_DUPLICATE = "Tag handle '%s' already declared before, handle must be unique";
32
33
    /**
34
     * Creates API object to be used for the document provided as argument
35
     *
36
     * @param YamlObject $obj the YamlObject as the target for all methods call that needs it
37 11
     */
38
    public function __construct(YamlObject $obj, int $buildingOptions)
39 11
    {
40 11
        $this->_obj = $obj;
41
        $this->_options = $buildingOptions;
42
    }
43
44
    public function getOptions()
45
    {
46
        return $this->_options;
47
    }
48
    /**
49
     * Adds a reference.
50
     *
51 3
     * @param string $name  The reference name
52
     * @param mixed  $value The reference value
53 3
     *
54
     * @throws \UnexpectedValueException  (description)
55
     * @return null
56 3
     */
57 3
    public function addReference(string $name, $value)
58
    {
59
        if (empty($name)) {
60
            throw new \UnexpectedValueException(self::UNAMED_REFERENCE);
61
        }
62
        // var_dump("DEBUG: '$name' added as reference");
63
        $this->_anchors[$name] = $value;
64
    }
65
66
    /**
67 2
     * Return the reference saved by $name
68
     *
69 2
     * @param string $name Name of the reference
70 2
     *
71
     * @return mixed Value of the reference
72
     * @throws \UnexpectedValueException    if there's no reference by that $name
73
     */
74
    public function &getReference($name)
75
    {
76
        if (array_key_exists($name, $this->_anchors)) {
77
            return $this->_anchors[$name];
78
        }
79
        throw new \UnexpectedValueException(sprintf(self::UNKNOWN_REFERENCE,
80
                                                    $name, implode(',',array_keys($this->_anchors)))
81
                                                );
82 3
    }
83
84 3
    /**
85
     * Return array with all references as Keys and their values, declared for this YamlObject
86
     *
87
     * @return array
88
     */
89
    public function getAllReferences():array
90
    {
91
        return $this->_anchors;
92
    }
93
94
    /**
95 2
     * Adds a comment.
96
     *
97 2
     * @param int    $lineNumber The line number at which the comment should appear
98 2
     * @param string $value      The comment
99
     *
100
     * @return null
101
     */
102
    public function addComment(int $lineNumber, string $value)
103
    {
104
        $this->_comments[$lineNumber] = $value;
105
    }
106
107 2
    /**
108
     * Gets the comment at $lineNumber
109 2
     *
110 2
     * @param int|null $lineNumber The line number
111
     *
112 2
     * @return string|array The comment at $lineNumber OR all comments.
113
     */
114
    public function getComment(int $lineNumber = null)
115
    {
116
        if (array_key_exists((int) $lineNumber, $this->_comments)) {
117
            return $this->_comments[$lineNumber];
118
        }
119
        return $this->_comments;
120
    }
121
122 1
    /**
123
     * Sets the text when the content is *only* a literal
124 1
     *
125 1
     * @param string $value The value
126
     *
127
     * @return YamlObject
128
     */
129
    public function setText(string $value):YamlObject
130
    {
131
        $this->value .= ltrim($value);
132
        return $this->_obj;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_obj could return the type null which is incompatible with the type-hinted return Dallgoot\Yaml\YamlObject. Consider adding an additional type-check to rule them out.
Loading history...
133
    }
134
135
    /**
136
     * TODO:  what to do with these tags ???
137 2
     * Adds a tag.
138
     *
139
     * @param string $handle The handle declared for the tag
140 2
     * @param string $prefix The prefix/namespace/schema that defines the tag
141
     *
142
     * @return null
143 2
     */
144 2
    public function addTag(string $handle, string $prefix)
145
    {
146
        //  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.
147
        if (array_key_exists($handle, $this->_tags)) {
148
            throw new \Exception(sprintf(self::TAGHANDLE_DUPLICATE, $handle), 1);
149
        }
150
        $this->_tags[$handle] = $prefix;
151 2
    }
152
153 2
    /**
154
     * Determines if it has YAML document start string => '---'.
155
     *
156
     * @return boolean  True if document has start, False otherwise.
157
     */
158
    public function hasDocStart():bool
159
    {
160
        return is_bool($this->_hasDocStart);
161
    }
162
163 2
    /**
164
     * Sets the document start.
165 2
     *
166 2
     * @param null|bool $value The value : null = no docstart, true = docstart before document comments, false = docstart after document comments
167
     *
168
     * @return null
169
     */
170
    public function setDocStart($value)
171
    {
172
        $this->_hasDocStart = $value;
173 2
    }
174
175 2
    /**
176
     * Is the whole YAML document (YamlObject) tagged ?
177
     *
178
     * @return bool
179
     */
180
    public function isTagged()
181
    {
182
        return !empty($this->_tags);
183
    }
184
185
}
186