1. <?php
  2. /**
  3. * Instruments date values for validation with retry logic.
  4. *
  5. * @param callable $callback The function that accepts a date value
  6. * and returns true if valid, false otherwise.
  7. * @param int $maxRetries Maximum number of retry attempts.
  8. * @param int $retryDelay Delay in seconds between retries.
  9. * @param int $initialDelay Delay in seconds before the first retry.
  10. * @return bool True if the date value is valid after retries, false otherwise.
  11. */
  12. function validateDateWithRetry(callable $callback, int $maxRetries, int $retryDelay = 5, int $initialDelay = 2): bool
  13. {
  14. $retries = 0;
  15. $delay = $initialDelay;
  16. while ($retries < $maxRetries) {
  17. try {
  18. if ($callback(date_default_timezone_set('UTC'), new DateTime())) { //Run validation logic
  19. return true; // Date is valid
  20. } else {
  21. $retries++;
  22. if ($retries < $maxRetries) {
  23. sleep($delay); // Wait before retrying
  24. $delay += $retryDelay; // Increase delay for subsequent retries
  25. }
  26. }
  27. } catch (Exception $e) {
  28. $retries++;
  29. if ($retries < $maxRetries) {
  30. sleep($delay);
  31. $delay += $retryDelay;
  32. }
  33. }
  34. }
  35. return false; // Date validation failed after all retries
  36. }
  37. //Example usage:
  38. /**
  39. * Dummy validation function, replace with your actual validation logic.
  40. * @param DateTime $date
  41. * @return bool
  42. */
  43. function isValidDate(DateTime $date): bool
  44. {
  45. //Replace with your date validation conditions.
  46. return true;
  47. }
  48. ?>

Add your comment