You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

166 lines
3.0 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. use ArrayAccess;
  12. use JsonSerializable;
  13. /**
  14. * Class User.
  15. */
  16. class User implements ArrayAccess, UserInterface, JsonSerializable
  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);
  92. return $this;
  93. }
  94. /**
  95. * @param string $provider
  96. *
  97. * @return $this
  98. */
  99. public function setProviderName($provider)
  100. {
  101. $this->setAttribute('provider', $provider);
  102. return $this;
  103. }
  104. /**
  105. * @return string
  106. */
  107. public function getProviderName()
  108. {
  109. return $this->getAttribute('provider');
  110. }
  111. /**
  112. * Get the authorized token.
  113. *
  114. * @return \Overtrue\Socialite\AccessToken
  115. */
  116. public function getToken()
  117. {
  118. return $this->getAttribute('token');
  119. }
  120. /**
  121. * Alias of getToken().
  122. *
  123. * @return \Overtrue\Socialite\AccessToken
  124. */
  125. public function getAccessToken()
  126. {
  127. return $this->getToken();
  128. }
  129. /**
  130. * Get the original attributes.
  131. *
  132. * @return array
  133. */
  134. public function getOriginal()
  135. {
  136. return $this->getAttribute('original');
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function jsonSerialize()
  142. {
  143. return array_merge($this->attributes, ['token' => $this->token->getAttributes()]);
  144. }
  145. }