Timer.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace React\EventLoop\Timer;
  3. use React\EventLoop\TimerInterface;
  4. /**
  5. * The actual connection implementation for TimerInterface
  6. *
  7. * This class should only be used internally, see TimerInterface instead.
  8. *
  9. * @see TimerInterface
  10. * @internal
  11. */
  12. final class Timer implements TimerInterface
  13. {
  14. const MIN_INTERVAL = 0.000001;
  15. private $interval;
  16. private $callback;
  17. private $periodic;
  18. /**
  19. * Constructor initializes the fields of the Timer
  20. *
  21. * @param float $interval The interval after which this timer will execute, in seconds
  22. * @param callable $callback The callback that will be executed when this timer elapses
  23. * @param bool $periodic Whether the time is periodic
  24. */
  25. public function __construct($interval, $callback, $periodic = false)
  26. {
  27. if ($interval < self::MIN_INTERVAL) {
  28. $interval = self::MIN_INTERVAL;
  29. }
  30. $this->interval = (float) $interval;
  31. $this->callback = $callback;
  32. $this->periodic = (bool) $periodic;
  33. }
  34. public function getInterval()
  35. {
  36. return $this->interval;
  37. }
  38. public function getCallback()
  39. {
  40. return $this->callback;
  41. }
  42. public function isPeriodic()
  43. {
  44. return $this->periodic;
  45. }
  46. }