User.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. use ArrayAccess;
  12. use JsonSerializable;
  13. /**
  14. * Class User.
  15. */
  16. class User implements ArrayAccess, UserInterface, JsonSerializable, \Serializable
  17. {
  18. use HasAttributes;
  19. /**
  20. * User constructor.
  21. *
  22. * @param array $attributes
  23. */
  24. public function __construct(array $attributes)
  25. {
  26. $this->attributes = $attributes;
  27. }
  28. /**
  29. * Get the unique identifier for the user.
  30. *
  31. * @return string
  32. */
  33. public function getId()
  34. {
  35. return $this->getAttribute('id');
  36. }
  37. /**
  38. * Get the username for the user.
  39. *
  40. * @return string
  41. */
  42. public function getUsername()
  43. {
  44. return $this->getAttribute('username', $this->getId());
  45. }
  46. /**
  47. * Get the nickname / username for the user.
  48. *
  49. * @return string
  50. */
  51. public function getNickname()
  52. {
  53. return $this->getAttribute('nickname');
  54. }
  55. /**
  56. * Get the full name of the user.
  57. *
  58. * @return string
  59. */
  60. public function getName()
  61. {
  62. return $this->getAttribute('name');
  63. }
  64. /**
  65. * Get the e-mail address of the user.
  66. *
  67. * @return string
  68. */
  69. public function getEmail()
  70. {
  71. return $this->getAttribute('email');
  72. }
  73. /**
  74. * Get the avatar / image URL for the user.
  75. *
  76. * @return string
  77. */
  78. public function getAvatar()
  79. {
  80. return $this->getAttribute('avatar');
  81. }
  82. /**
  83. * Set the token on the user.
  84. *
  85. * @param \Overtrue\Socialite\AccessTokenInterface $token
  86. *
  87. * @return $this
  88. */
  89. public function setToken(AccessTokenInterface $token)
  90. {
  91. $this->setAttribute('token', $token->getToken());
  92. $this->setAttribute('access_token', $token->getToken());
  93. if (\is_callable([$token, 'getRefreshToken'])) {
  94. $this->setAttribute('refresh_token', $token->getRefreshToken());
  95. }
  96. return $this;
  97. }
  98. /**
  99. * @param string $provider
  100. *
  101. * @return $this
  102. */
  103. public function setProviderName($provider)
  104. {
  105. $this->setAttribute('provider', $provider);
  106. return $this;
  107. }
  108. /**
  109. * @return string
  110. */
  111. public function getProviderName()
  112. {
  113. return $this->getAttribute('provider');
  114. }
  115. /**
  116. * Get the authorized token.
  117. *
  118. * @return \Overtrue\Socialite\AccessToken
  119. */
  120. public function getToken()
  121. {
  122. return new AccessToken([
  123. 'access_token' => $this->getAccessToken(),
  124. 'refresh_token' => $this->getAttribute('refresh_token')
  125. ]);
  126. }
  127. /**
  128. * Get user access token.
  129. *
  130. * @return string
  131. */
  132. public function getAccessToken()
  133. {
  134. return $this->getAttribute('token') ?: $this->getAttribute('access_token');
  135. }
  136. /**
  137. * Get user refresh token.
  138. *
  139. * @return string
  140. */
  141. public function getRefreshToken()
  142. {
  143. return $this->getAttribute('refresh_token');
  144. }
  145. /**
  146. * Get the original attributes.
  147. *
  148. * @return array
  149. */
  150. public function getOriginal()
  151. {
  152. return $this->getAttribute('original');
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. #[\ReturnTypeWillChange]
  158. public function jsonSerialize()
  159. {
  160. return $this->attributes;
  161. }
  162. /**
  163. * String representation of object.
  164. * @link https://php.net/manual/en/serializable.serialize.php
  165. * @return string|null The string representation of the object or null
  166. * @throws Exception Returning other type than string or null
  167. */
  168. #[\ReturnTypeWillChange]
  169. public function serialize()
  170. {
  171. return serialize($this->attributes);
  172. }
  173. /**
  174. * Constructs the object.
  175. *
  176. * @see https://php.net/manual/en/serializable.unserialize.php
  177. *
  178. * @param string $serialized <p>
  179. * The string representation of the object.
  180. * </p>
  181. *
  182. * @since 5.1.0
  183. */
  184. #[\ReturnTypeWillChange]
  185. public function unserialize($serialized)
  186. {
  187. $this->attributes = unserialize($serialized) ?: [];
  188. }
  189. public function __serialize()
  190. {
  191. $this->serialize();
  192. }
  193. public function __unserialize($serialized)
  194. {
  195. $this->unserialize($serialized);
  196. }
  197. }