RateLimit.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is a part of the DiscordPHP-Http project.
  4. *
  5. * Copyright (c) 2021-present David Cole <david.cole1340@gmail.com>
  6. *
  7. * This file is subject to the MIT license that is bundled
  8. * with this source code in the LICENSE file.
  9. */
  10. namespace Discord\Http;
  11. /**
  12. * Represents a rate-limit given by Discord.
  13. *
  14. * @author David Cole <david.cole1340@gmail.com>
  15. */
  16. class RateLimit
  17. {
  18. /**
  19. * Whether the rate-limit is global.
  20. *
  21. * @var bool
  22. */
  23. protected $global;
  24. /**
  25. * Time in seconds of when to retry after.
  26. *
  27. * @var float
  28. */
  29. protected $retry_after;
  30. /**
  31. * Rate limit constructor.
  32. *
  33. * @param bool $global
  34. * @param float $retry_after
  35. */
  36. public function __construct(bool $global, float $retry_after)
  37. {
  38. $this->global = $global;
  39. $this->retry_after = $retry_after;
  40. }
  41. /**
  42. * Gets the global parameter.
  43. *
  44. * @return bool
  45. */
  46. public function isGlobal(): bool
  47. {
  48. return $this->global;
  49. }
  50. /**
  51. * Gets the retry after parameter.
  52. *
  53. * @return float
  54. */
  55. public function getRetryAfter(): float
  56. {
  57. return $this->retry_after;
  58. }
  59. /**
  60. * Converts a rate-limit to a user-readable string.
  61. *
  62. * @return string
  63. */
  64. public function __toString()
  65. {
  66. return 'RATELIMIT '.($this->global ? 'Global' : 'Non-global').', retry after '.$this->retry_after.' s';
  67. }
  68. }