sfValidatorTime.class.php 4.5 KB

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