SimpleModel.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\framework\Model;
  3. use Illuminate\Contracts\Support\Arrayable;
  4. use Illuminate\Contracts\Support\Jsonable;
  5. use \ArrayAccess;
  6. use \JsonSerializable;
  7. class SimpleModel implements Arrayable, Jsonable, JsonSerializable, ArrayAccess
  8. {
  9. public $attributes = [];
  10. public $typeDefaults = [
  11. 'int' => 0,
  12. 'string' => '',
  13. 'timestamp' => 0,
  14. ];
  15. public $attributeTypes = [];
  16. public function formatAttributes()
  17. {
  18. $result = [];
  19. foreach ($this->attributeTypes as $key => $attributeType) {
  20. switch ($attributeType) {
  21. case 'timestamp':
  22. $result[$key] = date('Y-m-d H:i:s', $this->$key);
  23. break;
  24. default:
  25. break;
  26. }
  27. }
  28. $result = array_merge($this->attributes, $result);
  29. return $result;
  30. }
  31. public function __construct($attributes)
  32. {
  33. $this->attributes = array_merge($this->defaultAttributes(), $attributes);
  34. }
  35. protected function defaultAttributes()
  36. {
  37. $attributes = [];
  38. foreach ($this->attributes as $key => $attribute) {
  39. $attributes[$key] = $this->typeDefaults[$attribute];
  40. }
  41. return $attributes;
  42. }
  43. public function __get($key)
  44. {
  45. if (isset($this->attributes[$key])) {
  46. return $this->attributes[$key];
  47. }
  48. if (method_exists($this, 'get' . ucfirst($key) . 'Attribute')) {
  49. return $this->{'get' . ucfirst($key) . 'Attribute'}();
  50. }
  51. return null;
  52. }
  53. public function __set($key, $value)
  54. {
  55. if (isset($this->attributes[$key])) {
  56. $this->attributes[$key] = $value;
  57. }
  58. if (method_exists($this, 'set' . ucfirst($key) . 'Attribute')) {
  59. return $this->{'set' . ucfirst($key) . 'Attribute'}($value);
  60. }
  61. return null;
  62. }
  63. protected function touchAttributes()
  64. {
  65. foreach (array_keys($this->attributes) as $key) {
  66. $this->attributes[$key] = $this->$key;
  67. }
  68. }
  69. public function toArray()
  70. {
  71. $this->touchAttributes();
  72. return $this->formatAttributes();
  73. }
  74. public function toJson($options = 0)
  75. {
  76. return json_encode($this->toArray(), $options);
  77. }
  78. public function jsonSerialize()
  79. {
  80. return $this->toArray();
  81. }
  82. public function __toString()
  83. {
  84. return $this->toJson();
  85. }
  86. public function __isset($key)
  87. {
  88. return !is_null($this->attributes[$key]);
  89. }
  90. public function offsetUnset($offset)
  91. {
  92. unset($this->$offset);
  93. }
  94. public function offsetGet($offset)
  95. {
  96. return $this->$offset;
  97. }
  98. public function offsetExists($offset)
  99. {
  100. return isset($this->$offset);
  101. }
  102. public function offsetSet($offset, $value)
  103. {
  104. $this->$offset = $value;
  105. }
  106. }