Accessable.php 2.9 KB

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