AccessToken.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 InvalidArgumentException;
  13. use JsonSerializable;
  14. /**
  15. * Class AccessToken.
  16. */
  17. class AccessToken implements AccessTokenInterface, ArrayAccess, JsonSerializable
  18. {
  19. use HasAttributes;
  20. /**
  21. * AccessToken constructor.
  22. *
  23. * @param array $attributes
  24. */
  25. public function __construct(array $attributes)
  26. {
  27. if (empty($attributes['access_token'])) {
  28. throw new InvalidArgumentException('The key "access_token" could not be empty.');
  29. }
  30. $this->attributes = $attributes;
  31. }
  32. /**
  33. * Return the access token string.
  34. *
  35. * @return string
  36. */
  37. public function getToken()
  38. {
  39. return $this->getAttribute('access_token');
  40. }
  41. /**
  42. * Return the refresh token string.
  43. *
  44. * @return string
  45. */
  46. public function getRefreshToken()
  47. {
  48. return $this->getAttribute('refresh_token');
  49. }
  50. /**
  51. * Set refresh token into this object.
  52. *
  53. * @param string $token
  54. */
  55. public function setRefreshToken($token)
  56. {
  57. $this->setAttribute('refresh_token', $token);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function __toString()
  63. {
  64. return strval($this->getAttribute('access_token', ''));
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function jsonSerialize()
  70. {
  71. return $this->getToken();
  72. }
  73. }