Accessable.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Supports\Traits;
  4. trait Accessable
  5. {
  6. /**
  7. * __get.
  8. *
  9. * @author yansongda <me@yansongda.cn>
  10. *
  11. * @return mixed
  12. */
  13. public function __get(string $key)
  14. {
  15. return $this->get($key);
  16. }
  17. /**
  18. * __set.
  19. *
  20. * @author yansongda <me@yansongda.cn>
  21. *
  22. * @param mixed $value
  23. *
  24. * @return mixed
  25. */
  26. public function __set(string $key, $value)
  27. {
  28. return $this->set($key, $value);
  29. }
  30. /**
  31. * get.
  32. *
  33. * @author yansongda <me@yansongda.cn>
  34. *
  35. * @param mixed $default
  36. *
  37. * @return mixed
  38. */
  39. public function get(?string $key = null, $default = null)
  40. {
  41. if (is_null($key)) {
  42. return method_exists($this, 'toArray') ? $this->toArray() : $default;
  43. }
  44. $method = 'get';
  45. foreach (explode('_', $key) as $item) {
  46. $method .= ucfirst($item);
  47. }
  48. if (method_exists($this, $method)) {
  49. return $this->{$method}();
  50. }
  51. return $default;
  52. }
  53. /**
  54. * set.
  55. *
  56. * @author yansongda <me@yansongda.cn>
  57. *
  58. * @param mixed $value
  59. *
  60. * @return $this
  61. */
  62. public function set(string $key, $value)
  63. {
  64. $method = 'set';
  65. foreach (explode('_', $key) as $item) {
  66. $method .= ucfirst($item);
  67. }
  68. if (method_exists($this, $method)) {
  69. return $this->{$method}($value);
  70. }
  71. return $this;
  72. }
  73. /**
  74. * Whether a offset exists.
  75. *
  76. * @see https://php.net/manual/en/arrayaccess.offsetexists.php
  77. *
  78. * @param mixed $offset an offset to check for
  79. *
  80. * @return bool true on success or false on failure.
  81. *
  82. * The return value will be casted to boolean if non-boolean was returned.
  83. */
  84. public function offsetExists($offset)
  85. {
  86. return !is_null($this->get($offset));
  87. }
  88. /**
  89. * Offset to retrieve.
  90. *
  91. * @see https://php.net/manual/en/arrayaccess.offsetget.php
  92. *
  93. * @param mixed $offset the offset to retrieve
  94. *
  95. * @return mixed can return all value types
  96. */
  97. public function offsetGet($offset)
  98. {
  99. return $this->get($offset);
  100. }
  101. /**
  102. * Offset to set.
  103. *
  104. * @see https://php.net/manual/en/arrayaccess.offsetset.php
  105. *
  106. * @param mixed $offset the offset to assign the value to
  107. * @param mixed $value the value to set
  108. *
  109. * @return void
  110. */
  111. public function offsetSet($offset, $value)
  112. {
  113. $this->set($offset, $value);
  114. }
  115. /**
  116. * Offset to unset.
  117. *
  118. * @see https://php.net/manual/en/arrayaccess.offsetunset.php
  119. *
  120. * @param mixed $offset the offset to unset
  121. *
  122. * @return void
  123. */
  124. public function offsetUnset($offset)
  125. {
  126. }
  127. }