2022-03-25 17:02:09 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Str;
|
2022-08-14 13:35:37 -05:00
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
|
|
|
|
use Illuminate\Contracts\Support\Jsonable;
|
|
|
|
|
use JsonSerializable;
|
2022-03-25 17:02:09 -05:00
|
|
|
|
2022-08-14 13:35:37 -05:00
|
|
|
abstract class Model implements Arrayable, JsonSerializable, Jsonable
|
2022-03-25 17:02:09 -05:00
|
|
|
{
|
|
|
|
|
public function __construct(array $data = [])
|
|
|
|
|
{
|
|
|
|
|
foreach ($data as $k => $v) {
|
|
|
|
|
$this->__set($k, $v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function __set(string $key, $value)
|
|
|
|
|
{
|
|
|
|
|
$setter = 'set' . Str::studly($key);
|
|
|
|
|
if (method_exists($this, $setter)) {
|
|
|
|
|
$this->$setter($value);
|
|
|
|
|
} elseif (property_exists($this, $key)) {
|
|
|
|
|
$this->$key = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-14 13:35:37 -05:00
|
|
|
|
|
|
|
|
public function __get(string $key)
|
|
|
|
|
{
|
|
|
|
|
$getter = 'get' . Str::studly($key);
|
|
|
|
|
if (method_exists($this, $getter)) {
|
|
|
|
|
return $this->$getter($this->$key ?? null);
|
|
|
|
|
} elseif (property_exists($this, $key)) {
|
|
|
|
|
return $this->$key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new \OutOfBoundsException(sprintf(
|
|
|
|
|
'Undefined property %s::%s', static::class, $key
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function toArray()
|
|
|
|
|
{
|
|
|
|
|
return get_object_vars($this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function jsonSerialize()
|
|
|
|
|
{
|
|
|
|
|
return $this->toArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function toJson($options = 0)
|
|
|
|
|
{
|
|
|
|
|
return json_encode($this->jsonSerialize(), $options);
|
|
|
|
|
}
|
2022-03-25 17:02:09 -05:00
|
|
|
}
|