1. <?php
  2. /**
  3. * Validates time configuration from a configuration file.
  4. *
  5. * @param array $config An array containing time configuration values.
  6. * @return bool True if the configuration is valid, false otherwise.
  7. */
  8. function validateTimeConfig(array $config): bool
  9. {
  10. // Check if the configuration array is empty.
  11. if (empty($config)) {
  12. error_log("Error: Configuration array is empty.");
  13. return false;
  14. }
  15. // Validate 'debug_time_format'
  16. if (!is_string($config['debug_time_format'])) {
  17. error_log("Error: debug_time_format must be a string.");
  18. return false;
  19. }
  20. // Validate 'debug_time_precision'
  21. if (!is_int($config['debug_time_precision']) || $config['debug_time_precision'] < 0 || $config['debug_time_precision'] > 6) {
  22. error_log("Error: debug_time_precision must be an integer between 0 and 6.");
  23. return false;
  24. }
  25. // Validate 'debug_time_zone'
  26. if (!is_string($config['debug_time_zone'])) {
  27. error_log("Error: debug_time_zone must be a string.");
  28. return false;
  29. }
  30. // Validate 'debug_time_enable'
  31. if (!is_bool($config['debug_time_enable'])) {
  32. error_log("Error: debug_time_enable must be a boolean.");
  33. return false;
  34. }
  35. return true; // Configuration is valid.
  36. }
  37. // Example usage (for testing)
  38. /*
  39. $config = [
  40. 'debug_time_format' => 'Y-m-d H:i:s.u',
  41. 'debug_time_precision' => 6,
  42. 'debug_time_zone' => 'UTC',
  43. 'debug_time_enable' => true,
  44. ];
  45. if (validateTimeConfig($config)) {
  46. echo "Time configuration is valid.\n";
  47. } else {
  48. echo "Time configuration is invalid.\n";
  49. }
  50. */
  51. ?>

Add your comment