Esempio n. 1
0
 /**
  * @see sfValidatorBase
  */
 protected function doClean($value)
 {
     // check date format
     if (is_string($value) && ($regex = $this->getOption('date_format'))) {
         if (!preg_match($regex, $value, $match)) {
             throw new sfValidatorError($this, 'bad_format', array('value' => $value, 'date_format' => $this->getOption('date_format_error') ?: $this->getOption('date_format')));
         }
         $value = $match;
     }
     // convert array to date string
     if (is_array($value)) {
         $value = $this->convertDateArrayToString($value);
     }
     // convert timestamp to date number format
     if (is_numeric($value)) {
         if ($date = T::completeDate((string) $value)) {
             $clean = $date->format('YmdHis');
         } else {
             $cleanTime = (int) $value;
             $clean = date('YmdHis', $cleanTime);
         }
     } else {
         if (preg_match('/^(\\d{4})-(\\d{2})-(\\d{2})/', $value, $match)) {
             //per Kalender eingegebenes Datum (Y-m-d) oder Bearbeitungsdatum (Datum mit Zeit Y-m-d H:i:s)
             try {
                 $date = new DateTime($value);
             } catch (Exception $e) {
                 throw new sfValidatorError($this, 'invalid', array('value' => $value));
             }
             $date->setTimezone(new DateTimeZone(date_default_timezone_get()));
             $clean = $date->format('YmdHis');
         } elseif ($date = T::completeDate((string) $value)) {
             $clean = $date->format('YmdHis');
         } else {
             throw new sfValidatorError($this, 'invalid', array('value' => $value));
         }
     }
     // check max
     if ($max = $this->getOption('max')) {
         // convert timestamp to date number format
         if (is_numeric($max)) {
             $maxError = date($this->getOption('date_format_range_error'), $max);
             $max = date('YmdHis', $max);
         } else {
             $dateMax = new DateTime($max);
             $max = $dateMax->format('YmdHis');
             $maxError = $dateMax->format($this->getOption('date_format_range_error'));
         }
         if ($clean > $max) {
             throw new sfValidatorError($this, 'max', array('value' => $value, 'max' => $maxError));
         }
     }
     // check min
     if ($min = $this->getOption('min')) {
         // convert timestamp to date number
         if (is_numeric($min)) {
             $minError = date($this->getOption('date_format_range_error'), $min);
             $min = date('YmdHis', $min);
         } else {
             $dateMin = new DateTime($min);
             $min = $dateMin->format('YmdHis');
             $minError = $dateMin->format($this->getOption('date_format_range_error'));
         }
         if ($clean < $min) {
             throw new sfValidatorError($this, 'min', array('value' => $value, 'min' => $minError));
         }
     }
     if ($clean === $this->getEmptyValue()) {
         return $cleanTime;
     }
     $format = $this->getOption('with_time') ? $this->getOption('datetime_output') : $this->getOption('date_output');
     return isset($date) ? $date->format($format) : date($format, $cleanTime);
 }