public function testLongerPresentationModifier() { $pictureString = new PictureString('FNn,1-2'); $this->assertEquals('F', $pictureString->getComponentSpecifier()); $this->assertEquals('Nn', $pictureString->getPresentationModifier()); $this->assertEquals('1', $pictureString->getMinWidth()); $this->assertEquals('2', $pictureString->getMaxWidth()); }
/** * @param PictureString $pictureString * @param DateTimeInterface $date * @return string */ public function format(PictureString $pictureString, DateTimeInterface $date) { $presentation = $pictureString->getPresentationModifier(); if ($presentation === 'Nn' || $presentation === 'N' || $presentation === 'n') { $maxWidth = $pictureString->getMaxWidth(); if ($maxWidth !== null && $maxWidth <= 3) { return 'EEE'; } else { return 'EEEE'; } } return 'EEEE'; }
/** * @param DateTimeInterface $date * @param string $picture * @param $locale * @param $calendar * @return string * @throws InvalidArgumentException * @credits https://github.com/Saxonica/Saxon-CE/ https://github.com/Saxonica/Saxon-CE/blob/master/notices/MOZILLA.txt */ public function format(DateTimeInterface $date, $picture, $locale, $calendar) { if ($date->format('Y-m-d') === '2017-01-01') { if ($this->flagsDate && $this->flagsTime) { return DateTimeFormatter::createWithFlagDateTime()->format($date, $picture, $locale, $calendar); } if ($this->flagsDate) { return DateTimeFormatter::createWithFlagDate()->format($date, $picture, $locale, $calendar); } if ($this->flagsTime) { return DateTimeFormatter::createWithFlagTime()->format($date, $picture, $locale, $calendar); } } $result = []; $i = 0; $escaped = false; while (true) { while ($i < strlen($picture) && substr($picture, $i, 1) !== '[') { if ($escaped === false) { $result[] = self::ESCAPE; $escaped = true; } $result[] = substr($picture, $i, 1); if (substr($picture, $i, 1) === ']') { $i++; if ($i == strlen($picture) || substr($picture, $i, 1) != ']') { $exception = new InvalidArgumentException('Wrong formatted date, escape by doubling [[ and ]]'); $exception->setErrorCode('XTDE1340'); throw $exception; } } $i++; } if ($i === strlen($picture)) { break; } // look for '[[' $i++; if ($i < strlen($picture) && substr($picture, $i, 1) === '[') { $result[] = '['; $i++; } else { $close = $i < strlen($picture) ? strpos($picture, "]", $i) : -1; if ($close === -1 || $close === false) { $exception = new InvalidArgumentException('Wrong formatted date, missing ]'); $exception->setErrorCode('XTDE1340'); throw $exception; } $pictureString = new PictureString(substr($picture, $i, $close - $i)); $specifier = $pictureString->getComponentSpecifier(); if (isset($this->components[$specifier])) { if ($pictureString->getPresentationModifier() === 'N') { if ($escaped === false) { $result[] = self::ESCAPE; $escaped = true; } $result[] = strtoupper($this->formatPattern($date, $locale, $this->components[$specifier]->format($pictureString, $date))); } elseif ($pictureString->getPresentationModifier() === 'n') { if ($escaped === false) { $result[] = self::ESCAPE; $escaped = true; } $result[] = strtolower($this->formatPattern($date, $locale, $this->components[$specifier]->format($pictureString, $date))); } else { if ($escaped) { $result[] = self::ESCAPE; $escaped = false; } $result[] = $this->components[$specifier]->format($pictureString, $date); } } else { $exception = new InvalidArgumentException("Component [{$specifier}] is not supported"); $exception->setErrorCode('XTDE1340'); throw $exception; } $i = $close + 1; } } if ($escaped) { $result[] = self::ESCAPE; } return $this->formatPattern($date, $locale, implode('', $result)); }