public function test_parse_decoratedEmpty_lenient() { $this->builder->parseLenient()->padNext2(4, '-')->optionalStart()->appendValue(ChronoField::DAY_OF_MONTH())->optionalEnd(); $parsed = $this->builder->toFormatter()->parseUnresolved("----", $this->pos); $this->assertEquals($this->pos->getIndex(), 4); $this->assertEquals($this->pos->getErrorIndex(), -1); $this->assertNotNull($parsed); }
/** * @dataProvider data_time */ public function test_time_parse(LocalTime $time, FormatStyle $timeStyle, $timeStyleOld, Locale $locale) { $old = \IntlDateFormatter::create($locale->getLocale(), \IntlDateFormatter::NONE, $timeStyleOld, new \DateTimeZone('UTC')); $oldDate = new \DateTime('1970-0-0T' . $time->getHour() . ':' . $time->getMinute() . ':' . $time->getSecond(), new \DateTimeZone('UTC')); $text = $old->format($oldDate); $f = $this->builder->appendLocalized(null, $timeStyle)->toFormatter2($locale); $parsed = $f->parsePos($text, $this->pos); $this->assertEquals($this->pos->getIndex(), strlen($text)); $this->assertEquals($this->pos->getErrorIndex(), -1); $this->assertEquals(LocalTime::from($parsed), $time); }
private function parseUnresolved0($text, ParsePosition $position) { $context = new DateTimeParseContext($this); $pos = $position->getIndex(); $pos = $this->printerParser->parse($context, $text, $pos); if ($pos < 0) { $position->setErrorIndex(~$pos); // index not updated from input return null; } $position->setIndex($pos); // errorIndex not updated from input return $context; }
/** * TODO performance * * @param string $text * @param ParsePosition $ppos * @param bool $isCaseSensitive * @return null|string */ private function match($text, ParsePosition $ppos, $isCaseSensitive) { $ids = ZoneId::getAvailableZoneIds(); if (!$isCaseSensitive) { $ids = \array_map('\\strtolower', $ids); $text = \strtolower($text); } $ids = \array_flip($ids); $pos = $ppos->getIndex(); $max = \strlen($text) - 1; for ($i = $max; $i >= $pos; $i--) { $str = \substr($text, $pos, $i - $pos + 1); if (isset($ids[$str])) { $ppos->setIndex($i + 1); return ZoneId::getAvailableZoneIds()[$ids[$str]]; } } return null; }
/** * @dataProvider data_parseSuccess */ public function test_parseSuccess_caseInsensitive($text, $expectedIndex, $expectedErrorIndex, $expected) { $this->builder->parseCaseInsensitive()->appendZoneId(); $lcText = strtolower($text); $parsed = $this->builder->toFormatter()->parseUnresolved($lcText, $this->pos); $this->assertEquals($this->pos->getErrorIndex(), $expectedErrorIndex, "Incorrect error index parsing: " . $lcText); $this->assertEquals($this->pos->getIndex(), $expectedIndex, "Incorrect index parsing: " . $lcText); if ($expected !== null) { $zid = $parsed->query(TemporalQueries::zoneId()); $this->assertEquals($parsed->query(TemporalQueries::zoneId()), $expected, "Incorrect zoneId parsing: " . $lcText); $this->assertEquals($parsed->query(TemporalQueries::offset()), null, "Incorrect offset parsing: " . $lcText); $this->assertEquals($parsed->query(TemporalQueries::zone()), $expected, "Incorrect zone parsing: " . $lcText); } else { $this->assertEquals($parsed, null); } }