123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- /*
- * This file is part of the overtrue/socialite.
- *
- * (c) overtrue <i@overtrue.me>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace Overtrue\Socialite;
- use ArrayAccess;
- use JsonSerializable;
- /**
- * Class User.
- */
- class User implements ArrayAccess, UserInterface, JsonSerializable, \Serializable
- {
- use HasAttributes;
- /**
- * User constructor.
- *
- * @param array $attributes
- */
- public function __construct(array $attributes)
- {
- $this->attributes = $attributes;
- }
- /**
- * Get the unique identifier for the user.
- *
- * @return string
- */
- public function getId()
- {
- return $this->getAttribute('id');
- }
- /**
- * Get the username for the user.
- *
- * @return string
- */
- public function getUsername()
- {
- return $this->getAttribute('username', $this->getId());
- }
- /**
- * Get the nickname / username for the user.
- *
- * @return string
- */
- public function getNickname()
- {
- return $this->getAttribute('nickname');
- }
- /**
- * Get the full name of the user.
- *
- * @return string
- */
- public function getName()
- {
- return $this->getAttribute('name');
- }
- /**
- * Get the e-mail address of the user.
- *
- * @return string
- */
- public function getEmail()
- {
- return $this->getAttribute('email');
- }
- /**
- * Get the avatar / image URL for the user.
- *
- * @return string
- */
- public function getAvatar()
- {
- return $this->getAttribute('avatar');
- }
- /**
- * Set the token on the user.
- *
- * @param \Overtrue\Socialite\AccessTokenInterface $token
- *
- * @return $this
- */
- public function setToken(AccessTokenInterface $token)
- {
- $this->setAttribute('token', $token->getToken());
- $this->setAttribute('access_token', $token->getToken());
- if (\is_callable([$token, 'getRefreshToken'])) {
- $this->setAttribute('refresh_token', $token->getRefreshToken());
- }
- return $this;
- }
- /**
- * @param string $provider
- *
- * @return $this
- */
- public function setProviderName($provider)
- {
- $this->setAttribute('provider', $provider);
- return $this;
- }
- /**
- * @return string
- */
- public function getProviderName()
- {
- return $this->getAttribute('provider');
- }
- /**
- * Get the authorized token.
- *
- * @return \Overtrue\Socialite\AccessToken
- */
- public function getToken()
- {
- return new AccessToken([
- 'access_token' => $this->getAccessToken(),
- 'refresh_token' => $this->getAttribute('refresh_token')
- ]);
- }
- /**
- * Get user access token.
- *
- * @return string
- */
- public function getAccessToken()
- {
- return $this->getAttribute('token') ?: $this->getAttribute('access_token');
- }
- /**
- * Get user refresh token.
- *
- * @return string
- */
- public function getRefreshToken()
- {
- return $this->getAttribute('refresh_token');
- }
- /**
- * Get the original attributes.
- *
- * @return array
- */
- public function getOriginal()
- {
- return $this->getAttribute('original');
- }
- /**
- * {@inheritdoc}
- */
- public function jsonSerialize()
- {
- return $this->attributes;
- }
- public function serialize()
- {
- return serialize($this->attributes);
- }
- /**
- * Constructs the object.
- *
- * @see https://php.net/manual/en/serializable.unserialize.php
- *
- * @param string $serialized <p>
- * The string representation of the object.
- * </p>
- *
- * @since 5.1.0
- */
- public function unserialize($serialized)
- {
- $this->attributes = unserialize($serialized) ?: [];
- }
- }
|