Exemplo n.º 1
0
 protected function dateToString($time)
 {
     if (is_numeric($time)) {
         $cal = $this->tm->createCalendar();
         $cal->setTime($time);
         $time = $cal;
     }
     $time->getTime();
     $format = new AgaviDateFormatter('EEE MMM dd HH:mm:ss zzz yyyy');
     return $format->format($time, 'gregorian', $this->tm->getCurrentLocale());
 }
 /**
  * Validates the input.
  * 
  * @return     bool True if the input was a valid date.
  * 
  * @author     Dominik del Bondio <*****@*****.**>
  * @since      0.11.0
  */
 protected function validate()
 {
     if (!AgaviConfig::get('core.use_translation')) {
         throw new AgaviConfigurationException('The datetime validator can only be used with use_translation on');
     }
     $tm = $this->getContext()->getTranslationManager();
     $cal = null;
     $check = $this->getParameter('check', true);
     $locale = $this->hasParameter('locale') ? $tm->getLocale($this->getParameter('locale')) : $tm->getCurrentLocale();
     if ($this->hasMultipleArguments() && !$this->getParameter('arguments_format')) {
         $cal = $tm->createCalendar();
         $cal->clear();
         $cal->setLenient(!$check);
         foreach ($this->getArguments() as $calField => $field) {
             $param = $this->getData($field);
             if (defined($calField)) {
                 $calField = constant($calField);
             } elseif (!is_numeric($calField)) {
                 throw new AgaviValidatorException('Unknown argument name "' . $calField . '" for argument "' . $field . '" supplied. This needs to be one of the constants defined in AgaviDateDefinitions.');
             }
             if (!is_scalar($param)) {
                 // everything which is non scalar is ignored, since it couldn't be handled anyways
                 continue;
             }
             if ($calField == AgaviDateDefinitions::MONTH) {
                 $param -= 1;
             }
             $cal->set($calField, (double) $param);
         }
         try {
             $cal->getTime();
         } catch (Exception $e) {
             $this->throwError('check');
             return false;
         }
     } else {
         if ($argFormat = $this->getParameter('arguments_format')) {
             $values = array();
             foreach ($this->getArguments() as $field) {
                 $values[] = $this->getData($field);
             }
             $param = vsprintf($argFormat, $values);
         } else {
             $param = $this->getData($this->getArgument());
             if (!is_scalar($param)) {
                 $this->throwError();
                 return false;
             }
         }
         $matchedFormat = false;
         foreach ((array) $this->getParameter('formats', array()) as $key => $item) {
             if (!is_array($item)) {
                 $item = array(is_int($key) ? 'format' : $key => $item);
             }
             $itemLocale = empty($item['locale']) ? $locale : $tm->getLocale($item['locale']);
             $type = empty($item['type']) ? 'format' : $item['type'];
             if ($type == 'format') {
                 $formatString = $item['format'];
             } elseif ($type == 'time' || $type == 'date' || $type == 'datetime') {
                 $format = isset($item['format']) ? $item['format'] : null;
                 $formatString = AgaviDateFormatter::resolveFormat($format, $itemLocale, $type);
             } elseif ($type == 'translation_domain') {
                 $td = $item['translation_domain'];
                 $formatString = $tm->_($item['format'], $td, $itemLocale);
             } elseif ($type == 'unix') {
                 $matchedFormat = $param === (string) (int) $param;
                 $cal = $tm->createCalendar($itemLocale);
                 $cal->setUnixTimestamp($param);
                 if ($matchedFormat) {
                     try {
                         if ($cal->getUnixTimestamp() !== (int) $param) {
                             $this->throwError('check');
                             return false;
                         }
                     } catch (Exception $e) {
                         $matchedFormat = false;
                     }
                 }
             } elseif ($type == 'unix_milliseconds') {
                 $matchedFormat = is_numeric($param);
                 $cal = $tm->createCalendar($itemLocale);
                 $cal->setTime($param);
                 if ($matchedFormat) {
                     try {
                         if ($cal->getTime() !== (double) $param) {
                             $this->throwError('check');
                             return false;
                         }
                     } catch (Exception $e) {
                         $matchedFormat = false;
                     }
                 }
             }
             if (!$cal) {
                 try {
                     $format = new AgaviDateFormat($formatString);
                     $cal = $format->parse($param, $itemLocale, $check);
                     // no exception got thrown so the parsing was successful
                     $matchedFormat = true;
                     break;
                 } catch (Exception $e) {
                     // nop
                 }
             }
         }
         if (!$matchedFormat) {
             $this->throwError('format');
             return false;
         }
     }
     $cal->setLenient(true);
     $value = $cal;
     if ($cast = $this->getParameter('cast_to')) {
         // an array means the user wants it custom formatted
         if (is_array($cast)) {
             $type = empty($cast['type']) ? 'format' : $cast['type'];
             if ($type == 'format') {
                 $formatString = $cast['format'];
             } elseif ($type == 'time' || $type == 'date' || $type == 'datetime') {
                 $format = isset($cast['format']) ? $cast['format'] : null;
                 $formatString = AgaviDateFormatter::resolveFormat($format, $locale, $type);
             }
             $format = new AgaviDateFormat($formatString);
             $value = $format->format($cal, $cal->getType(), $locale);
         } else {
             $cast = strtolower($cast);
             if ($cast == 'unix') {
                 $value = $cal->getUnixTimestamp();
             } elseif ($cast == 'string') {
                 $value = $tm->_d($cal);
             } elseif ($cast == 'datetime') {
                 $value = $cal->getNativeDateTime();
             } else {
                 $value = $cal;
             }
         }
     }
     $defaultParseFormat = new AgaviDateFormat('yyyy-MM-dd HH:mm:ss.S');
     if ($this->hasParameter('min')) {
         $min = $this->getMinOrMaxValue('min', $defaultParseFormat, $locale);
         $isAfterEqual = $cal->after($min) || $cal->equals($min);
         if (!$isAfterEqual) {
             $this->throwError('min');
             return false;
         }
     }
     if ($this->hasParameter('max')) {
         $max = $this->getMinOrMaxValue('max', $defaultParseFormat, $locale);
         $isBefore = $cal->before($max);
         if (!$isBefore) {
             $this->throwError('max');
             return false;
         }
     }
     if ($this->hasParameter('export')) {
         $export = $this->getParameter('export');
         if (is_string($export)) {
             $this->export($value);
         } elseif (is_array($export)) {
             foreach ($export as $calField => $field) {
                 if (defined($calField)) {
                     $this->export($cal->get(constant($calField)), $field);
                 }
             }
         }
     }
     return true;
 }