酒店预订平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

136 行
2.3 KiB

  1. <?php
  2. /*
  3. * This file is part of the overtrue/socialite.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Overtrue\Socialite;
  11. /**
  12. * Trait HasAttributes.
  13. */
  14. trait HasAttributes
  15. {
  16. /**
  17. * @var array
  18. */
  19. protected $attributes = [];
  20. /**
  21. * Return the attributes.
  22. *
  23. * @return array
  24. */
  25. public function getAttributes()
  26. {
  27. return $this->attributes;
  28. }
  29. /**
  30. * Return the extra attribute.
  31. *
  32. * @param string $name
  33. * @param string $default
  34. *
  35. * @return mixed
  36. */
  37. public function getAttribute($name, $default = null)
  38. {
  39. return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
  40. }
  41. /**
  42. * Set extra attributes.
  43. *
  44. * @param string $name
  45. * @param mixed $value
  46. *
  47. * @return $this
  48. */
  49. public function setAttribute($name, $value)
  50. {
  51. $this->attributes[$name] = $value;
  52. return $this;
  53. }
  54. /**
  55. * Map the given array onto the user's properties.
  56. *
  57. * @param array $attributes
  58. *
  59. * @return $this
  60. */
  61. public function merge(array $attributes)
  62. {
  63. $this->attributes = array_merge($this->attributes, $attributes);
  64. return $this;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function offsetExists($offset)
  70. {
  71. return array_key_exists($offset, $this->attributes);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function offsetGet($offset)
  77. {
  78. return $this->getAttribute($offset);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function offsetSet($offset, $value)
  84. {
  85. $this->setAttribute($offset, $value);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function offsetUnset($offset)
  91. {
  92. unset($this->attributes[$offset]);
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function __get($property)
  98. {
  99. return $this->getAttribute($property);
  100. }
  101. /**
  102. * Return array.
  103. *
  104. * @return array
  105. */
  106. public function toArray()
  107. {
  108. return $this->getAttributes();
  109. }
  110. /**
  111. * Return JSON.
  112. *
  113. * @return string
  114. */
  115. public function toJSON()
  116. {
  117. return json_encode($this->getAttributes(), JSON_UNESCAPED_UNICODE);
  118. }
  119. }