Ejemplo n.º 1
0
 /**
  * Checks if the given value is a valid DateTime object.
  * Note: a value of NULL or empty string ('') is considered valid
  *
  * @param mixed $value The value that should be validated
  * @return void
  * @api
  */
 protected function isValid($value)
 {
     if ($value instanceof \DateTime) {
         return;
     }
     if (!isset($this->options['locale'])) {
         $locale = $this->localizationService->getConfiguration()->getDefaultLocale();
     } elseif (is_string($this->options['locale'])) {
         $locale = new \TYPO3\FLOW3\I18n\Locale($this->options['locale']);
     } elseif ($this->options['locale'] instanceof \TYPO3\FLOW3\I18n\Locale) {
         $locale = $this->options['locale'];
     } else {
         $this->addError('The "locale" option can be only set to string identifier, or Locale object.', 1281454676);
         return;
     }
     if (!isset($this->options['strictMode']) || $this->options['strictMode'] === TRUE) {
         $strictMode = TRUE;
     } else {
         $strictMode = FALSE;
     }
     if (isset($this->options['formatLength'])) {
         $formatLength = $this->options['formatLength'];
         \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::validateFormatLength($formatLength);
     } else {
         $formatLength = \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_LENGTH_DEFAULT;
     }
     if (isset($this->options['formatType'])) {
         $formatType = $this->options['formatType'];
         \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::validateFormatType($formatType);
     } else {
         $formatType = \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_TYPE_DATE;
     }
     if ($formatType === \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_TYPE_TIME) {
         if ($this->datetimeParser->parseTime($value, $locale, $formatLength, $strictMode) === FALSE) {
             $this->addError('A valid time is expected.', 1281454830);
         }
     } elseif ($formatType === \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_TYPE_DATETIME) {
         if ($this->datetimeParser->parseDateAndTime($value, $locale, $formatLength, $strictMode) === FALSE) {
             $this->addError('A valid date and time is expected.', 1281454831);
         }
     } else {
         if ($this->datetimeParser->parseDate($value, $locale, $formatLength, $strictMode) === FALSE) {
             $this->addError('A valid date is expected.', 1281454832);
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Formats provided value using optional style properties
  *
  * @param mixed $value Formatter-specific variable to format (can be integer, \DateTime, etc)
  * @param \TYPO3\FLOW3\I18n\Locale $locale Locale to use
  * @param array $styleProperties Integer-indexed array of formatter-specific style properties (can be empty)
  * @return string String representation of $value provided, or (string)$value
  * @api
  */
 public function format($value, \TYPO3\FLOW3\I18n\Locale $locale, array $styleProperties = array())
 {
     if (isset($styleProperties[0])) {
         $formatType = $styleProperties[0];
         \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::validateFormatType($formatType);
     } else {
         $formatType = \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_TYPE_DATETIME;
     }
     if (isset($styleProperties[1])) {
         $formatLength = $styleProperties[1];
         \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::validateFormatLength($formatLength);
     } else {
         $formatLength = \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_LENGTH_DEFAULT;
     }
     switch ($formatType) {
         case \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_TYPE_DATE:
             return $this->formatDate($value, $locale, $formatLength);
         case \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_TYPE_TIME:
             return $this->formatTime($value, $locale, $formatLength);
         default:
             return $this->formatDateTime($value, $locale, $formatLength);
     }
 }