Passed
Branch master (f496ba)
by stéphane
02:11
created

YamlObject.php (15 issues)

1
<?php
2
namespace Dallgoot\Yaml;
0 ignored issues
show
Missing file doc comment
Loading history...
3
4
use Dallgoot\Yaml\API as API;
5
6
/**
0 ignored issues
show
Missing short description in doc comment
Loading history...
7
  * @method void addReference(string $name, $value)
8
  * @method mixed getReference(string $name)
9
  * @method array getAllReferences()
10
  * @method void addComment($index, $value)
11
  * @method string|array getComment($lineNumber)
12
  * @method void setText(string $value)
13
  * @method void addTag(string $value)
14
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @package tag in class comment
Loading history...
Missing @author tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
15
class YamlObject extends \ArrayIterator implements \JsonSerializable
16
{
17
    private $__yaml__object__api;
18
19
    private const UNDEFINED_METHOD = self::class.": undefined method '%s' ! valid methods are %s";
20
21
    public function __construct()
0 ignored issues
show
Missing function doc comment
Loading history...
22
    {
23
        parent::__construct([], 1);//1 = Array indices can be accessed as properties in read/write.
24
        $this->__yaml__object__api = new API();
25
    }
26
27
    //TODO: determine accessible methods : private OR public
28
    public function __call($funcName, $arguments)
0 ignored issues
show
You must use "/**" style comments for a function comment
Loading history...
29
    {
30
        $reflectAPI = new \ReflectionClass(get_class($this->__yaml__object__api));
31
        $getName = function ($o) { return $o->name; };
0 ignored issues
show
Opening brace must be the last content on the line
Loading history...
Closing brace must be on a line by itself
Loading history...
32
        $publicApi  = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PUBLIC));
33
        $privateApi = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PRIVATE));
34
        if (!in_array($funcName, $publicApi) && !in_array($funcName, $privateApi)) {
35
                throw new \BadMethodCallException(sprintf(self::UNDEFINED_METHOD, $funcName, implode(",", $publicApi)), 1);
36
        }
37
        return call_user_func_array([$this->__yaml__object__api, $funcName], $arguments);
38
    }
39
40
    public function __toString():string
0 ignored issues
show
Missing function doc comment
Loading history...
41
    {
42
        return $this->__yaml__object__api->value ?? serialize($this);
43
    }
44
45
    public function jsonSerialize()
0 ignored issues
show
Missing function doc comment
Loading history...
46
    {
47
        $prop = get_object_vars($this);
48
        unset($prop["__yaml__object__api"]);
49
        if (count($prop) > 0) return $prop;
1 ignored issue
show
Inline control structures are discouraged
Loading history...
50
        if (count($this) > 0) return iterator_to_array($this);
1 ignored issue
show
Inline control structures are discouraged
Loading history...
51
        return $this->__yaml__object__api->value;
52
    }
53
}
54