Completed
Push — master ( c6ecf3...3eacba )
by stéphane
02:27
created

YamlObject::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Dallgoot\Yaml;
3
4
/**
5
 *  The returned object representing a YAML file content
6
 *
7
 * @author  Stéphane Rebai <[email protected]>
8
 * @license Apache 2.0
9
 * @link    TODO : url to specific online doc
10
 *
11
 * @method void addReference(string $name, $value)
12
 * @method mixed getReference(string $name)
13
 * @method array getAllReferences()
14
 * @method void addComment($index, $value)
15
 * @method string|array getComment($lineNumber)
16
 * @method void setText(string $value)
17
 * @method void addTag(string $value)
18
 * @method bool hasDocStart()
19
 */
20
class YamlObject extends \ArrayIterator implements \JsonSerializable
21
{
22
    /** @var API */
23
    private $__yaml__object__api;
24
25
    private const UNDEFINED_METHOD = self::class.": undefined method '%s', valid methods are %s";
26
27
    /**
28
     * Construct the YamlObject making sure the indices can be accessed directly
29
     * and creates the API object with a reference to this YamlObject.
30
     */
31 5
    public function __construct()
32
    {
33 5
        parent::__construct([], 1); //1 = Array indices can be accessed as properties in read/write.
34 5
        $this->__yaml__object__api = new API($this);
35 5
    }
36
37
    /**
38
     * Transfer method calls to Yaml::API object
39
     *
40
     * @param string $funcName  The function name
41
     * @param mixed  $arguments The arguments
42
     *
43
     * @throws \BadMethodCallException if method isn't part of the public API
44
     * @return mixed                    the return value of the API::method called
45
     */
46 3
    public function __call($funcName, $arguments)
47
    {
48 3
        $reflectAPI = new \ReflectionClass(get_class($this->__yaml__object__api));
49
        $getName    = function ($o) { return $o->name; };
50 3
        $publicApi  = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PUBLIC));
51 3
        if (!in_array($funcName, $publicApi) ) {
52 1
            throw new \BadMethodCallException(sprintf(self::UNDEFINED_METHOD, $funcName, implode(",", $publicApi)));
53
        }
54 2
        return call_user_func_array([$this->__yaml__object__api, $funcName], $arguments);
55
    }
56
57
    /**
58
     * Returns a string representation of the YamlObject when
59
     * it has NO property NOR keys ie. is only a LITTERAL
60
     *
61
     * @return string String representation of the object.
62
     */
63 1
    public function __toString():string
64
    {
65 1
        return $this->__yaml__object__api->value ?? serialize($this);
66
    }
67
68
    /**
69
     * Filters unwanted property for JSON serialization
70
     *
71
     * @return mixed Array (of object properties or keys) OR string if YAML object only contains LITTERAL (in self::value)
72
     */
73 1
    public function jsonSerialize()
74
    {
75 1
        $prop = get_object_vars($this);
76 1
        unset($prop["__yaml__object__api"]);
77 1
        if (count($prop) > 0) return $prop;
78 1
        if (count($this) > 0) return iterator_to_array($this);
79 1
        return $this->__yaml__object__api->value ?? "_Empty YamlObject_";
80
    }
81
}
82