dallgoot /
yaml
| 1 | <?php |
||
| 2 | namespace Dallgoot\Yaml; |
||
| 3 | |||
| 4 | use Dallgoot\Yaml\API as API; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * |
||
| 8 | */ |
||
| 9 | class YamlObject extends \ArrayIterator implements \JsonSerializable |
||
| 10 | { |
||
| 11 | private $__yaml__object__api; |
||
| 12 | |||
| 13 | private const UNDEFINED_METHOD = self::class.": undefined method '%s' ! valid methods are %s"; |
||
| 14 | |||
| 15 | public function __construct() |
||
| 16 | { |
||
| 17 | parent::__construct([], 1);//1 = Array indices can be accessed as properties in read/write. |
||
| 18 | $this->__yaml__object__api = new API(); |
||
| 19 | } |
||
| 20 | |||
| 21 | public function __call($funcName, $arguments) |
||
| 22 | { |
||
| 23 | $reflectAPI = new \ReflectionClass(get_class($this->__yaml__object__api)); |
||
| 24 | $getName = function ($o) { return $o->name; }; |
||
| 25 | $publicApi = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PUBLIC)); |
||
| 26 | $privateApi = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PRIVATE)); |
||
| 27 | if (!in_array($funcName, $publicApi) && |
||
| 28 | (!in_array($funcName, $privateApi) || $this->__yaml__object__api->_locked)) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 29 | throw new \BadMethodCallException(sprintf(self::UNDEFINED_METHOD, $funcName, implode(",", $publicApi)), 1); |
||
| 30 | } |
||
| 31 | return call_user_func_array([$this->__yaml__object__api, $funcName], $arguments); |
||
| 32 | } |
||
| 33 | |||
| 34 | public function __toString():string |
||
| 35 | { |
||
| 36 | return $this->__yaml__object__api->value ?? serialize($this); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function jsonSerialize() |
||
| 40 | { |
||
| 41 | $prop = get_object_vars($this); |
||
| 42 | unset($prop["__yaml__object__api"]); |
||
| 43 | if (count($prop) > 0) return $prop; |
||
| 44 | if (count($this) > 0) return iterator_to_array($this); |
||
| 45 | return $this->__yaml__object__api->value; |
||
| 46 | } |
||
| 47 | } |
||
| 48 |