/**
  * Parses the provided string and returns the result.
  *
  * @param string $value
  *
  * @throws ParseException
  * @return TimeValue
  */
 protected function stringParse($value)
 {
     list($sign, $year) = $this->eraParser->parse($value);
     // Negative dates usually don't have a month, assume non-digits are thousands separators
     if ($sign === '-') {
         $separator = $this->getOption(self::OPT_DIGIT_GROUP_SEPARATOR);
         // Always accept ISO (e.g. "1 000 BC") as well as programming style (e.g. "-1_000")
         $year = preg_replace('/(?<=\\d)[' . preg_quote($separator, '/') . '\\s_](?=\\d)/', '', $year);
     }
     if (!preg_match('/^\\d+$/', $year)) {
         throw new ParseException('Failed to parse year', $value, self::FORMAT_NAME);
     }
     return $this->isoTimestampParser->parse($sign . $year . '-00-00T00:00:00Z');
 }
Exemplo n.º 2
0
 /**
  * @param string $value
  *
  * @throws ParseException
  * @return TimeValue
  */
 protected function stringParse($value)
 {
     try {
         list($sign, $preparsedValue) = $this->eraParser->parse($value);
         list($signedYear, $month, $day) = $this->parseYearMonthDay($preparsedValue);
         if (substr($signedYear, 0, 1) !== '-') {
             $signedYear = $sign . $signedYear;
         } elseif ($sign === '-') {
             throw new ParseException('Two eras found');
         }
         return $this->newTimeValue($signedYear, $month, $day);
     } catch (ParseException $ex) {
         throw new ParseException($ex->getMessage(), $value, self::FORMAT_NAME);
     }
 }
Exemplo n.º 3
0
 /**
  * @param string $value in a format as specified by the PHP DateTime object
  *       there are exceptions as we can handel 5+ digit dates
  *
  * @throws ParseException
  * @return TimeValue
  */
 protected function stringParse($value)
 {
     $rawValue = $value;
     try {
         list($sign, $value) = $this->eraParser->parse($value);
         $value = trim($value);
         $value = $this->monthNameUnlocalizer->unlocalize($value);
         $year = $this->fetchAndNormalizeYear($value);
         $value = $this->getValueWithFixedSeparators($value, $year);
         $this->validateDateTimeInput($value);
         // Parse using the DateTime object (this will allow us to format the date in a nicer way)
         $dateTime = new DateTime($value);
         // Fail if the DateTime object does calculations like changing 2015-00-00 to 2014-12-30.
         if ($year !== null && $dateTime->format('Y') !== substr($year, -4)) {
             throw new ParseException($value . ' is not a valid date.');
         }
         // Input was three numbers? Where the heck does a time come from?
         if ($dateTime->format('H:i:s') !== '00:00:00' && preg_match('/^\\D*\\d+\\D+\\d+\\D+\\d+\\D*$/', $value)) {
             throw new ParseException($value . ' is not a valid date.');
         }
         if ($year !== null && strlen($year) > 4) {
             $timestamp = $sign . $year . $dateTime->format('-m-d\\TH:i:s\\Z');
         } else {
             $timestamp = $sign . $dateTime->format('Y-m-d\\TH:i:s\\Z');
         }
         // Pass the reformatted string into a base parser that parses this +/-Y-m-d\TH:i:s\Z format with a precision
         return $this->isoTimestampParser->parse($timestamp);
     } catch (Exception $exception) {
         throw new ParseException($exception->getMessage(), $rawValue, self::FORMAT_NAME);
     }
 }
Exemplo n.º 4
0
 /**
  * Parses the provided string and returns the result.
  *
  * @param string $value
  *
  * @throws ParseException
  * @return TimeValue
  */
 protected function stringParse($value)
 {
     list($sign, $year) = $this->eraParser->parse($value);
     // Negative dates usually don't have a month, assume non-digits are thousands separators
     if ($sign === '-') {
         $separatorMap = $this->lang->separatorTransformTable();
         if (is_array($separatorMap) && array_key_exists(',', $separatorMap)) {
             $separator = $separatorMap[','];
         } else {
             $separator = ',';
         }
         // Always accept ISO (e.g. "1 000 BC") as well as programming style (e.g. "-1_000")
         $year = preg_replace('/(?<=\\d)[' . preg_quote($separator, '/') . '\\s_](?=\\d)/', '', $year);
     }
     if (!preg_match('/^\\d+$/', $year)) {
         throw new ParseException('Failed to parse year', $value, self::FORMAT_NAME);
     }
     return $this->isoTimestampParser->parse($sign . $year . '-00-00T00:00:00Z');
 }
Exemplo n.º 5
0
 /**
  * @param int $precision
  */
 private function setPrecision($precision)
 {
     $this->isoTimestampParser->getOptions()->setOption(IsoTimestampParser::OPT_PRECISION, $precision);
 }
 /**
  * @param ValueParser $parser
  * @param string $value
  * @param ValueValidator|null $validator
  *
  * @return array
  */
 private function parseStringValue(ValueParser $parser, $value, ValueValidator $validator = null)
 {
     $result = array('raw' => $value);
     try {
         $parseResult = $parser->parse($value);
     } catch (ParseException $parseError) {
         $this->addParseErrorToResult($result, $parseError);
         return $result;
     }
     if ($parseResult instanceof DataValue) {
         $result['value'] = $parseResult->getArrayValue();
         $result['type'] = $parseResult->getType();
     } else {
         $result['value'] = $parseResult;
     }
     if ($validator) {
         $validatorResult = $validator->validate($parseResult);
         $validationStatus = $this->validatorErrorLocalizer->getResultStatus($validatorResult);
         $result['valid'] = $validationStatus->isOK();
         if (!$validationStatus->isOK()) {
             $result['error'] = 'ValidationError';
             $this->errorReporter->addStatusToResult($validationStatus, $result);
             $result['validation-errors'] = $this->getValidatorErrorCodes($validatorResult->getErrors());
         }
     }
     return $result;
 }
 /**
  * @param string $year
  * @param string $month
  *
  * @return TimeValue
  */
 private function getTimeFromYearMonth($year, $month)
 {
     return $this->isoTimestampParser->parse(sprintf('+%d-%02d-00T00:00:00Z', $year, $month));
 }
Exemplo n.º 8
0
 /**
  * Parses and validates the provided with with specified parser.
  * The result is returned in an array on success. On fail, false is returned.
  * The result is wrapped in an array since we need to be able to distinguish
  * between the method returning false and the value being false.
  *
  * Parsing and validation errors get added to $this->errors.
  *
  * @since 1.0
  *
  * @param ValueParser $parser
  * @param mixed $value
  *
  * @return array|bool
  */
 protected function parseAndValidateValue(ValueParser $parser, $value)
 {
     try {
         $value = $parser->parse($value);
     } catch (ParseException $parseException) {
         $this->registerProcessingError($parseException->getMessage());
         return false;
     }
     if ($value instanceof \DataValues\DataValue) {
         $value = $value->getValue();
     }
     $this->validateValue($value);
     return array($value);
 }