Attr::__toString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Jaxon\Script\Call;
4
5
class Attr implements ParameterInterface
6
{
7
    /**
8
     * The attribute value
9
     *
10
     * @var array
11
     */
12
    private $aValue;
13
14
    /**
15
     * Get the parameter type
16
     *
17
     * @return string
18
     */
19
    public function getType(): string
20
    {
21
        return $this->aValue['_type'];
22
    }
23
24
    /**
25
     * Create an abject to get the value of an attribute
26
     *
27
     * @param string $sAttrName    The attribute name
28
     * @param bool $bHasParent
29
     *
30
     * @return Attr
31
     */
32
    static public function get(string $sAttrName, bool $bHasParent): Attr
33
    {
34
        $xAttr = new Attr();
35
        $xAttr->aValue = [
36
            '_type' => $bHasParent ? 'attr' : 'gvar',
37
            '_name' => $sAttrName,
38
        ];
39
        return $xAttr;
40
    }
41
42
    /**
43
     * Create an abject to set the value of an attribute
44
     *
45
     * @param string $sAttrName    The attribute name
46
     * @param mixed $xAttrValue    The attribute value
47
     * @param bool $bHasParent
48
     *
49
     * @return Attr
50
     */
51
    static public function set(string $sAttrName, $xAttrValue, bool $bHasParent)
52
    {
53
        $xAttr = new Attr();
54
        $xAttr->aValue = [
55
            '_type' => $bHasParent ? 'attr' : 'gvar',
56
            '_name' => $sAttrName,
57
            'value' => Parameter::make($xAttrValue),
58
        ];
59
        return $xAttr;
60
    }
61
62
    /**
63
     * Convert this call to array, when converting the response into json.
64
     *
65
     * @return array
66
     */
67
    public function jsonSerialize(): array
68
    {
69
        if(isset($this->aValue['value']))
70
        {
71
            $this->aValue['value'] = $this->aValue['value']->jsonSerialize();
72
        }
73
        return $this->aValue;
74
    }
75
76
    /**
77
     * Returns a string representation of this call
78
     *
79
     * @return string
80
     */
81
    public function __toString(): string
82
    {
83
        return !isset($this->aValue['value']) ? $this->aValue['_name'] :
84
            $this->aValue['_name'] . ' = ' . $this->aValue['value']->__toString();
85
    }
86
}
87