Passed
Push — master ( 42571c...f8295f )
by stéphane
09:20
created

YamlObject::addTag()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
1
<?php
2
namespace Dallgoot\Yaml;
3
4
/**
5
 *  The returned object representing a YAML document
6
 *
7
 * @author  Stéphane Rebai <[email protected]>
8
 * @license Apache 2.0
9
 * @link    https://github.com/dallgoot/yaml
10
 */
11
class YamlObject extends \ArrayIterator implements \JsonSerializable
12
{
13
    /** @var API */
0 ignored issues
show
Bug introduced by
The type Dallgoot\Yaml\API was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
    private $__yaml__object__api;
15
16
    const UNDEFINED_METHOD = self::class.": undefined method '%s', valid methods are (addReference,getReference,getAllReferences,addComment,getComment,setText,addTag,hasDocStart,isTagged)";
17
    const UNKNOWN_REFERENCE = "no reference named: '%s', known are : (%s)";
18
    const UNAMED_REFERENCE  = "reference MUST have a name";
19
    const TAGHANDLE_DUPLICATE = "Tag handle '%s' already declared before, handle must be unique";
20
21
    /**
22
     * Construct the YamlObject making sure the indices can be accessed directly
23
     * and creates the API object with a reference to this YamlObject.
24
     * @todo check indices access outside of foreach loop
25
     */
26 13
    public function __construct($buildingOptions)
27
    {
28 13
        parent::__construct([], 1); //1 = Array indices can be accessed as properties in read/write.
29 13
        $this->__yaml__object__api = new YamlProperties($buildingOptions);
0 ignored issues
show
Documentation Bug introduced by
It seems like new Dallgoot\Yaml\YamlProperties($buildingOptions) of type Dallgoot\Yaml\YamlProperties is incompatible with the declared type Dallgoot\Yaml\API of property $__yaml__object__api.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
30 13
    }
31
32
    /**
33
     * Returns a string representation of the YamlObject when
34
     * it has NO property NOR keys ie. is only a LITTERAL
35
     *
36
     * @return string String representation of the object.
37
     */
38 1
    public function __toString():string
39
    {
40 1
        return $this->__yaml__object__api->value ?? serialize($this);
41
    }
42
43
    public function getOptions()
44
    {
45
        return $this->__yaml__object__api->_options;
46
    }
47
    /**
48
     * Adds a reference.
49
     *
50
     * @param string $name  The reference name
51
     * @param mixed  $value The reference value
52
     *
53
     * @throws \UnexpectedValueException  (description)
54
     * @return null
55
     */
56 3
    public function &addReference(string $name, $value)
57
    {
58 3
        if (empty($name)) {
59
            throw new \UnexpectedValueException(self::UNAMED_REFERENCE);
60
        }
61
        // var_dump("DEBUG: '$name' added as reference");
62 3
        $this->__yaml__object__api->_anchors[$name] = $value;
63 3
        return $this->__yaml__object__api->_anchors[$name];
64
    }
65
66
    /**
67
     * Return the reference saved by $name
68
     *
69
     * @param string $name Name of the reference
70
     *
71
     * @return mixed Value of the reference
72
     * @throws \UnexpectedValueException    if there's no reference by that $name
73
     */
74 2
    public function &getReference($name)
75
    {
76 2
        if (array_key_exists($name, $this->__yaml__object__api->_anchors)) {
77 2
            return $this->__yaml__object__api->_anchors[$name];
78
        }
79
        throw new \UnexpectedValueException(sprintf(self::UNKNOWN_REFERENCE,
80
                                                    $name, implode(',',array_keys($this->__yaml__object__api->_anchors)))
81
                                                );
82
    }
83
84
    /**
85
     * Return array with all references as Keys and their values, declared for this YamlObject
86
     *
87
     * @return array
88
     */
89 3
    public function getAllReferences():array
90
    {
91 3
        return $this->__yaml__object__api->_anchors;
92
    }
93
94
    /**
95
     * Adds a comment.
96
     *
97
     * @param int    $lineNumber The line number at which the comment should appear
98
     * @param string $value      The comment
99
     *
100
     * @return null
101
     */
102 2
    public function addComment(int $lineNumber, string $value)
103
    {
104 2
        $this->__yaml__object__api->_comments[$lineNumber] = $value;
105 2
    }
106
107
    /**
108
     * Gets the comment at $lineNumber
109
     *
110
     * @param int|null $lineNumber The line number
111
     *
112
     * @return string|array The comment at $lineNumber OR all comments.
113
     */
114 2
    public function getComment(int $lineNumber = null)
115
    {
116 2
        if (array_key_exists((int) $lineNumber, $this->__yaml__object__api->_comments)) {
117 2
            return $this->__yaml__object__api->_comments[$lineNumber];
118
        }
119 2
        return $this->__yaml__object__api->_comments;
120
    }
121
122
    /**
123
     * Sets the text when the content is *only* a literal
124
     *
125
     * @param string $value The value
126
     *
127
     * @return YamlObject
128
     */
129 2
    public function setText(string $value):YamlObject
130
    {
131 2
        $this->__yaml__object__api->value .= ltrim($value);
132 2
        return $this;
133
    }
134
135
    /**
136
     * TODO:  what to do with these tags ???
137
     * Adds a tag.
138
     *
139
     * @param string $handle The handle declared for the tag
140
     * @param string $prefix The prefix/namespace/schema that defines the tag
141
     *
142
     * @return null
143
     */
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 2
        if (array_key_exists($handle, $this->__yaml__object__api->_tags)) {
148
            throw new \Exception(sprintf(self::TAGHANDLE_DUPLICATE, $handle), 1);
149
        }
150 2
        $this->__yaml__object__api->_tags[$handle] = $prefix;
151 2
    }
152
153
    /**
154
     * Determines if it has YAML document start string => '---'.
155
     *
156
     * @return boolean  True if document has start, False otherwise.
157
     */
158 2
    public function hasDocStart():bool
159
    {
160 2
        return is_bool($this->__yaml__object__api->_hasDocStart);
161
    }
162
163
    /**
164
     * Sets the document start.
165
     *
166
     * @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 2
    public function setDocStart($value)
171
    {
172 2
        $this->__yaml__object__api->_hasDocStart = $value;
173 2
    }
174
175
    /**
176
     * Is the whole YAML document (YamlObject) tagged ?
177
     *
178
     * @return bool
179
     */
180 2
    public function isTagged()
181
    {
182 2
        return !empty($this->__yaml__object__api->_tags);
183
    }
184
185
    /**
186
     * Filters unwanted property for JSON serialization
187
     *
188
     * @return mixed Array (of object properties or keys) OR string if YAML object only contains LITTERAL (in self::value)
189
     */
190 1
    public function jsonSerialize()
191
    {
192 1
        $prop = get_object_vars($this);
193 1
        unset($prop["__yaml__object__api"]);
194 1
        if (count($prop) > 0) return $prop;
195 1
        if (count($this) > 0) return iterator_to_array($this);
196 1
        return $this->__yaml__object__api->value ?? "_Empty YamlObject_";
197
    }
198
}
199