1. sfValidatorTime.class.php
  2. /** * sfValidatorTime validates a time. It also converts the input value to a valid time. * * @package symfony * @subpackage validator * @author Fabien Potencier * @author Fabian Lange * @version SVN: $Id: sfValidatorTime.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ */
  3. class sfValidatorTime extends sfValidatorBase
  4. {
  5. /**
  6. * Configures the current validator.
  7. *
  8. * Available options:
  9. *
  10. * * time_format: A regular expression that dates must match
  11. * * time_output: The format to use when returning a date with time (default to H:i:s)
  12. * * time_format_error: The date format to use when displaying an error for a bad_format error
  13. *
  14. * Available error codes:
  15. *
  16. * * bad_format
  17. *
  18. * @param array $options An array of options
  19. * @param array $messages An array of error messages
  20. *
  21. * @see sfValidatorBase
  22. */
  23. protected function configure($options = array(), $messages = array())
  24. {
  25. $this->addMessage('bad_format', '"%value%" does not match the time format (%time_format%).');
  26. $this->addOption('time_format', null);
  27. $this->addOption('time_output', 'H:i:s');
  28. $this->addOption('time_format_error');
  29. }
  30. /**
  31. * @see sfValidatorBase
  32. */
  33. protected function doClean($value)
  34. {
  35. if (is_array($value))
  36. {
  37. $clean = $this->convertTimeArrayToTimestamp($value);
  38. }
  39. else if ($regex = $this->getOption('time_format'))
  40. {
  41. if (!preg_match($regex, $value, $match))
  42. {
  43. throw new sfValidatorError($this, 'bad_format', array('value' => $value, 'time_format' => $this->getOption('time_format_error') ? $this->getOption('time_format_error') : $this->getOption('time_format')));
  44. }
  45. $clean = $this->convertTimeArrayToTimestamp($match);
  46. }
  47. else if (!ctype_digit($value))
  48. {
  49. $clean = strtotime($value);
  50. if (false === $clean)
  51. {
  52. throw new sfValidatorError($this, 'invalid', array('value' => $value));
  53. }
  54. }
  55. else
  56. {
  57. $clean = (integer) $value;
  58. }
  59. return $clean === $this->getEmptyValue() ? $clean : date($this->getOption('time_output'), $clean);
  60. }
  61. /**
  62. * Converts an array representing a time to a timestamp.
  63. *
  64. * The array can contains the following keys: hour, minute, second
  65. *
  66. * @param array $value An array of date elements
  67. *
  68. * @return int A timestamp
  69. */
  70. protected function convertTimeArrayToTimestamp($value)
  71. {
  72. // all elements must be empty or a number
  73. foreach (array('hour', 'minute', 'second') as $key)
  74. {
  75. if (isset($value[$key]) && !preg_match('#^\d+$#', $value[$key]) && !empty($value[$key]))
  76. {
  77. throw new sfValidatorError($this, 'invalid', array('value' => $value));
  78. }
  79. }
  80. // if second is set, minute and hour must be set
  81. // if minute is set, hour must be set
  82. if (
  83. $this->isValueSet($value, 'second') && (!$this->isValueSet($value, 'minute') || !$this->isValueSet($value, 'hour'))
  84. ||
  85. $this->isValueSet($value, 'minute') && !$this->isValueSet($value, 'hour')
  86. )
  87. {
  88. throw new sfValidatorError($this, 'invalid', array('value' => $value));
  89. }
  90. $clean = mktime(
  91. isset($value['hour']) ? intval($value['hour']) : 0,
  92. isset($value['minute']) ? intval($value['minute']) : 0,
  93. isset($value['second']) ? intval($value['second']) : 0
  94. );
  95. if (false === $clean)
  96. {
  97. throw new sfValidatorError($this, 'invalid', array('value' => var_export($value, true)));
  98. }
  99. return $clean;
  100. }
  101. protected function isValueSet($values, $key)
  102. {
  103. return isset($values[$key]) && !in_array($values[$key], array(null, ''), true);
  104. }
  105. /**
  106. * @see sfValidatorBase
  107. */
  108. protected function isEmpty($value)
  109. {
  110. if (is_array($value))
  111. {
  112. // array is not empty when a value is found
  113. foreach($value as $key => $val)
  114. {
  115. // int and string '0' are 'empty' values that are explicitly accepted
  116. if ($val === 0 || $val === '0' || !empty($val)) return false;
  117. }
  118. return true;
  119. }
  120. return parent::isEmpty($value);
  121. }
  122. }

Debug toolbar