Passed
Push — master ( affae1...bf1da0 )
by stéphane
10:14
created

Tag::buildValue()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 6
nop 1
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
1
<?php
2
namespace Dallgoot\Yaml;
3
4
use Dallgoot\Yaml\{Yaml as Y};
5
/**
6
 * TODO
7
 *
8
 * @author  Stéphane Rebai <[email protected]>
9
 * @license Apache 2.0
10
 * @link    TODO : url to specific online doc
11
 */
12
class Tag
13
{
14
    /** @var string */
15
    public $tagName;
16
    /** @var Node|null|string */
17
    public $value;
18
    /** @var null|Node|NodeList */
19
    private $raw;
20
21
    const LEGACY_TAGS = ['!str', '!binary', '!set', '!omap', 'php/object', '!inline', '!long'];
22
23
    /**
24
     * Tag constructor.
25
     * @param string $tagName the name of the tag like '!!str' (WITHOUT the first "!")
26
     * @param mixed $value  any PHP variable type
27
     * @throws \Exception if $tagName is an invalid string or absent
28
     */
29
    public function __construct(string $tagName, $raw)
30
    {
31
        if (empty($tagName)) {
32
            throw new \Exception(self::class.": a tag MUST have a name", 1);
33
        }
34
        $this->tagName = $tagName;
35
        $this->raw = $raw;
36
    }
37
38
    /**
39
     * Return the tagged value according to Tag type
40
     *
41
     * @param      scalar|Node|NodeList  $value  The value
42
     *
43
     * @return     mixed
44
     *
45
     * @todo implement others legacy types if needed  + Symfony type 'php/object' (unserialize ???)
46
     */
47
    public function buildValue(object &$parent = null)
48
    {
49
        $value = $this->raw;
50
        if ($value instanceof Node) {
51
            $value = new NodeList($this->raw);
0 ignored issues
show
Bug introduced by
It seems like $this->raw can also be of type Dallgoot\Yaml\NodeList; however, parameter $node of Dallgoot\Yaml\NodeList::__construct() does only seem to accept Dallgoot\Yaml\Node|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            $value = new NodeList(/** @scrutinizer ignore-type */ $this->raw);
Loading history...
52
        }
53
        $value->forceType();
54
        // $parent = Builder::$_root;
55
        switch ($this->tagName) {
56
            case '!set': $value->type = Y::SET;break;
57
            case '!omap': $value->type = Y::SEQUENCE;break;
58
            // assumed to be !str,!binary
59
            default: $parent = null;
60
                     $value->type = Y::RAW;
61
        }
62
        $this->value = Builder::buildNodeList($value, $parent);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $list of Dallgoot\Yaml\Builder::buildNodeList() does only seem to accept Dallgoot\Yaml\NodeList, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        $this->value = Builder::buildNodeList(/** @scrutinizer ignore-type */ $value, $parent);
Loading history...
63
        return $this->value;
64
    }
65
66
    public function isKnown():bool
67
    {
68
        return in_array($this->tagName, self::LEGACY_TAGS);
69
    }
70
71
    /**
72
     * Should verify if the tag is correct
73
     *
74
     * @param      string  $providedName  The provided name
75
     * @todo is this required ???
76
     */
77
    // private function checkNameValidity(string $providedName)
78
    // {
79
        /* TODO  implement and throw Exception if invalid (setName method ???)
80
         *The suffix must not contain any “!” character. This would cause the tag shorthand to be interpreted as having a named tag handle. In addition, the suffix must not contain the “[”, “]”, “{”, “}” and “,” characters. These characters would cause ambiguity with flow collection structures. If the suffix needs to specify any of the above restricted characters, they must be escaped using the “%” character. This behavior is consistent with the URI character escaping rules (specifically, section 2.3 of RFC2396).
81
        */
82
    // }
83
}
84