Ejemplo n.º 1
0
 /**
  * Checks if the given value is a valid number.
  * Note: a value of NULL or empty string ('') is considered valid
  *
  * @param mixed $value The value that should be validated
  * @return void
  * @api
  * @todo Currency support should be added when it will be supported by NumberParser
  */
 protected function isValid($value)
 {
     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.', 1281286579);
         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\NumbersReader::validateFormatLength($formatLength);
     } else {
         $formatLength = \TYPO3\FLOW3\I18n\Cldr\Reader\NumbersReader::FORMAT_LENGTH_DEFAULT;
     }
     if (isset($this->options['formatType'])) {
         $formatType = $this->options['formatType'];
         \TYPO3\FLOW3\I18n\Cldr\Reader\NumbersReader::validateFormatType($formatType);
     } else {
         $formatType = \TYPO3\FLOW3\I18n\Cldr\Reader\NumbersReader::FORMAT_TYPE_DECIMAL;
     }
     if ($formatType === \TYPO3\FLOW3\I18n\Cldr\Reader\NumbersReader::FORMAT_TYPE_PERCENT) {
         if ($this->numberParser->parsePercentNumber($value, $locale, $formatLength, $strictMode) === FALSE) {
             $this->addError('A valid percent number is expected.', 1281452093);
         }
         return;
     } else {
         if ($this->numberParser->parseDecimalNumber($value, $locale, $formatLength, $strictMode) === FALSE) {
             $this->addError('A valid decimal number is expected.', 1281452094);
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Tries to parse the input using the NumberParser.
  *
  * @param string $source
  * @param \TYPO3\FLOW3\Property\PropertyMappingConfigurationInterface $configuration
  * @return float|\TYPO3\FLOW3\Validation\Error Parsed float number or error
  */
 protected function parseUsingLocaleIfConfigured($source, \TYPO3\FLOW3\Property\PropertyMappingConfigurationInterface $configuration)
 {
     $configuration = $this->getConfigurationKeysAndValues($configuration, array('locale', 'strictMode', 'formatLength', 'formatType'));
     if ($configuration['locale'] === NULL) {
         return $source;
     } elseif ($configuration['locale'] === TRUE) {
         $locale = $this->localizationService->getConfiguration()->getCurrentLocale();
     } elseif (is_string($configuration['locale'])) {
         $locale = new \TYPO3\FLOW3\I18n\Locale($configuration['locale']);
     } elseif ($configuration['locale'] instanceof \TYPO3\FLOW3\I18n\Locale) {
         $locale = $configuration['locale'];
     }
     if (!$locale instanceof \TYPO3\FLOW3\I18n\Locale) {
         $exceptionMessage = 'Determined locale is not of type "\\TYPO3\\FLOW3\\I18n\\Locale", but of type "' . (is_object($locale) ? get_class($locale) : gettype($locale)) . '".';
         throw new InvalidPropertyMappingConfigurationException($exceptionMessage, 1334837413);
     }
     if ($configuration['strictMode'] === NULL || $configuration['strictMode'] === TRUE) {
         $strictMode = TRUE;
     } else {
         $strictMode = FALSE;
     }
     if ($configuration['formatLength'] !== NULL) {
         $formatLength = $configuration['formatLength'];
         NumbersReader::validateFormatLength($formatLength);
     } else {
         $formatLength = NumbersReader::FORMAT_LENGTH_DEFAULT;
     }
     if ($configuration['formatType'] !== NULL) {
         $formatType = $configuration['formatType'];
         \TYPO3\FLOW3\I18n\Cldr\Reader\NumbersReader::validateFormatType($formatType);
     } else {
         $formatType = NumbersReader::FORMAT_TYPE_DECIMAL;
     }
     if ($formatType === NumbersReader::FORMAT_TYPE_PERCENT) {
         $return = $this->numberParser->parsePercentNumber($source, $locale, $formatLength, $strictMode);
         if ($return === FALSE) {
             $return = new Error('A valid percent number is expected.', 1334839253);
         }
     } else {
         $return = $this->numberParser->parseDecimalNumber($source, $locale, $formatLength, $strictMode);
         if ($return === FALSE) {
             $return = new Error('A valid decimal number is expected.', 1334839260);
         }
     }
     return $return;
 }