dallgoot /
yaml
| 1 | <?php |
||
| 2 | namespace Dallgoot\Yaml; |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 3 | |||
| 4 | use Dallgoot\Yaml\API as API; |
||
| 5 | |||
| 6 | /** |
||
|
0 ignored issues
–
show
|
|||
| 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
|
|||
| 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
|
|||
| 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
|
|||
| 29 | { |
||
| 30 | $reflectAPI = new \ReflectionClass(get_class($this->__yaml__object__api)); |
||
| 31 | $getName = function ($o) { return $o->name; }; |
||
|
0 ignored issues
–
show
|
|||
| 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
|
|||
| 41 | { |
||
| 42 | return $this->__yaml__object__api->value ?? serialize($this); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function jsonSerialize() |
||
|
0 ignored issues
–
show
|
|||
| 46 | { |
||
| 47 | $prop = get_object_vars($this); |
||
| 48 | unset($prop["__yaml__object__api"]); |
||
| 49 | if (count($prop) > 0) return $prop; |
||
|
1 ignored issue
–
show
|
|||
| 50 | if (count($this) > 0) return iterator_to_array($this); |
||
|
1 ignored issue
–
show
|
|||
| 51 | return $this->__yaml__object__api->value; |
||
| 52 | } |
||
| 53 | } |
||
| 54 |