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