| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace app\framework\Model;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Contracts\Support\Jsonable;
- use \ArrayAccess;
- use \JsonSerializable;
- class SimpleModel implements Arrayable, Jsonable, JsonSerializable, ArrayAccess
- {
- public $attributes = [];
- public $typeDefaults = [
- 'int' => 0,
- 'string' => '',
- 'timestamp' => 0,
- ];
- public $attributeTypes = [];
- public function formatAttributes()
- {
- $result = [];
- foreach ($this->attributeTypes as $key => $attributeType) {
- switch ($attributeType) {
- case 'timestamp':
- $result[$key] = date('Y-m-d H:i:s', $this->$key);
- break;
- default:
- break;
- }
- }
- $result = array_merge($this->attributes, $result);
- return $result;
- }
- public function __construct($attributes)
- {
- $this->attributes = array_merge($this->defaultAttributes(), $attributes);
- }
- protected function defaultAttributes()
- {
- $attributes = [];
- foreach ($this->attributes as $key => $attribute) {
- $attributes[$key] = $this->typeDefaults[$attribute];
- }
- return $attributes;
- }
- public function __get($key)
- {
- if (isset($this->attributes[$key])) {
- return $this->attributes[$key];
- }
- if (method_exists($this, 'get' . ucfirst($key) . 'Attribute')) {
- return $this->{'get' . ucfirst($key) . 'Attribute'}();
- }
- return null;
- }
- public function __set($key, $value)
- {
- if (isset($this->attributes[$key])) {
- $this->attributes[$key] = $value;
- }
- if (method_exists($this, 'set' . ucfirst($key) . 'Attribute')) {
- return $this->{'set' . ucfirst($key) . 'Attribute'}($value);
- }
- return null;
- }
- protected function touchAttributes()
- {
- foreach (array_keys($this->attributes) as $key) {
- $this->attributes[$key] = $this->$key;
- }
- }
- public function toArray()
- {
- $this->touchAttributes();
- return $this->formatAttributes();
- }
- public function toJson($options = 0)
- {
- return json_encode($this->toArray(), $options);
- }
- public function jsonSerialize()
- {
- return $this->toArray();
- }
- public function __toString()
- {
- return $this->toJson();
- }
- public function __isset($key)
- {
- return !is_null($this->attributes[$key]);
- }
- public function offsetUnset($offset)
- {
- unset($this->$offset);
- }
- public function offsetGet($offset)
- {
- return $this->$offset;
- }
- public function offsetExists($offset)
- {
- return isset($this->$offset);
- }
- public function offsetSet($offset, $value)
- {
- $this->$offset = $value;
- }
- }
|