francis94c /
vimeo-livestream
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LiveStream\Resources; |
||
| 4 | |||
| 5 | use BadMethodCallException; |
||
| 6 | use stdClass; |
||
| 7 | |||
| 8 | class Resource |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Resource Payload |
||
| 12 | * |
||
| 13 | * @var object |
||
| 14 | */ |
||
| 15 | protected $data; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Class Constructor. |
||
| 19 | * |
||
| 20 | * @param boolean $init |
||
| 21 | */ |
||
| 22 | public function __construct(bool $init = true) |
||
| 23 | { |
||
| 24 | if ($init) $this->data = new stdClass(); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Factory Method. |
||
| 29 | * |
||
| 30 | * @param object $object |
||
| 31 | * @return \LiveStream\Resources\Resource |
||
| 32 | */ |
||
| 33 | public static function fromObject(object $object): Resource |
||
| 34 | { |
||
| 35 | if ($object == null) return null; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 36 | |||
| 37 | $instance = new static(false); |
||
| 38 | $instance->data = $object; |
||
| 39 | return $instance; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Magic Setter Method |
||
| 44 | * |
||
| 45 | * @param string $key |
||
| 46 | * @param mixed $value |
||
| 47 | * @return void |
||
| 48 | */ |
||
| 49 | public function __set(string $key, $value): void |
||
| 50 | { |
||
| 51 | $this->data->$key = $value; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Magic Getter Method |
||
| 56 | * |
||
| 57 | * @param string $key |
||
| 58 | * @return void |
||
| 59 | */ |
||
| 60 | public function __get(string $key) |
||
| 61 | { |
||
| 62 | return $this->data->$key ?? null; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Magic Method Isset. |
||
| 67 | * |
||
| 68 | * @param string $key |
||
| 69 | * @return boolean |
||
| 70 | */ |
||
| 71 | public function __isset(string $key) |
||
| 72 | { |
||
| 73 | return isset($this->data->$key); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Stictly for Setters and Getter. |
||
| 78 | * |
||
| 79 | * @param string $name |
||
| 80 | * @param array $arguments |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | public function __call(string $name, array $arguments) |
||
| 84 | { |
||
| 85 | if (substr($name, 0, 3) == 'get') { |
||
| 86 | return $this->get_called_value($name); |
||
|
0 ignored issues
–
show
Are you sure the usage of
$this->get_called_value($name) targeting LiveStream\Resources\Resource::get_called_value() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 87 | } elseif (substr($name, 0, 3) == 'set') { |
||
| 88 | $this->set_called_value($name, $arguments[0]); |
||
| 89 | return $this; // Method Chaining. |
||
|
0 ignored issues
–
show
|
|||
| 90 | } |
||
| 91 | |||
| 92 | throw new BadMethodCallException("Function '$name' does not exist."); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get called value from __call by function name. |
||
| 97 | * |
||
| 98 | * @param string $name |
||
| 99 | * @param string $function |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | private function get_called_value(string $function) |
||
| 103 | { |
||
| 104 | return $this->data->{lcfirst(substr($function, 3, strlen($function) - 3))} ?? null; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Undocumented function |
||
| 109 | * |
||
| 110 | * @param string $function |
||
| 111 | * @param [type] $value |
||
|
0 ignored issues
–
show
|
|||
| 112 | * @return void |
||
| 113 | */ |
||
| 114 | private function set_called_value(string $function, $value): void |
||
| 115 | { |
||
| 116 | $this->data->{lcfirst(substr($function, 3, strlen($function) - 3))} = $value; |
||
| 117 | } |
||
| 118 | } |
||
| 119 |