User.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Overtrue\Socialite;
  3. use ArrayAccess;
  4. use JsonSerializable;
  5. use Overtrue\Socialite\Contracts\ProviderInterface;
  6. use Overtrue\Socialite\Contracts\UserInterface;
  7. use Overtrue\Socialite\Traits\HasAttributes;
  8. class User implements ArrayAccess, UserInterface, JsonSerializable, \Serializable
  9. {
  10. use HasAttributes;
  11. /**
  12. * @var \Overtrue\Socialite\Contracts\ProviderInterface|null
  13. */
  14. protected ?ProviderInterface $provider;
  15. public function __construct(array $attributes, ProviderInterface $provider = null)
  16. {
  17. $this->attributes = $attributes;
  18. $this->provider = $provider;
  19. }
  20. public function getId()
  21. {
  22. return $this->getAttribute('id') ?? $this->getEmail();
  23. }
  24. public function getNickname(): ?string
  25. {
  26. return $this->getAttribute('nickname') ?? $this->getName();
  27. }
  28. public function getName(): ?string
  29. {
  30. return $this->getAttribute('name');
  31. }
  32. public function getEmail(): ?string
  33. {
  34. return $this->getAttribute('email');
  35. }
  36. public function getAvatar(): ?string
  37. {
  38. return $this->getAttribute('avatar');
  39. }
  40. public function setAccessToken(string $token): self
  41. {
  42. $this->setAttribute('access_token', $token);
  43. return $this;
  44. }
  45. public function getAccessToken(): ?string
  46. {
  47. return $this->getAttribute('access_token');
  48. }
  49. public function setRefreshToken(?string $refreshToken): self
  50. {
  51. $this->setAttribute('refresh_token', $refreshToken);
  52. return $this;
  53. }
  54. public function getRefreshToken(): ?string
  55. {
  56. return $this->getAttribute('refresh_token');
  57. }
  58. public function setExpiresIn(int $expiresIn): self
  59. {
  60. $this->setAttribute('expires_in', $expiresIn);
  61. return $this;
  62. }
  63. public function getExpiresIn(): ?int
  64. {
  65. return $this->getAttribute('expires_in');
  66. }
  67. public function setRaw(array $user): self
  68. {
  69. $this->setAttribute('raw', $user);
  70. return $this;
  71. }
  72. public function getRaw(): array
  73. {
  74. return $this->getAttribute('raw');
  75. }
  76. public function setTokenResponse(array $response)
  77. {
  78. $this->setAttribute('token_response', $response);
  79. return $this;
  80. }
  81. public function getTokenResponse()
  82. {
  83. return $this->getAttribute('token_response');
  84. }
  85. public function jsonSerialize(): array
  86. {
  87. return $this->attributes;
  88. }
  89. public function serialize()
  90. {
  91. return serialize($this->attributes);
  92. }
  93. public function unserialize($serialized)
  94. {
  95. $this->attributes = unserialize($serialized) ?: [];
  96. }
  97. /**
  98. * @return \Overtrue\Socialite\Contracts\ProviderInterface
  99. */
  100. public function getProvider(): \Overtrue\Socialite\Contracts\ProviderInterface
  101. {
  102. return $this->provider;
  103. }
  104. /**
  105. * @param \Overtrue\Socialite\Contracts\ProviderInterface $provider
  106. *
  107. * @return $this
  108. */
  109. public function setProvider(\Overtrue\Socialite\Contracts\ProviderInterface $provider)
  110. {
  111. $this->provider = $provider;
  112. return $this;
  113. }
  114. }