コード例 #1
0
ファイル: DayInMonthComponent.php プロジェクト: genkgo/xsl
 /**
  * @param PictureString $pictureString
  * @return string
  */
 public function format(PictureString $pictureString, DateTimeInterface $date)
 {
     $maxWidth = $pictureString->getMaxWidth();
     if ($maxWidth === null || $maxWidth > 1) {
         return 'dd';
     } else {
         return 'd';
     }
 }
コード例 #2
0
ファイル: DayOfWeekComponent.php プロジェクト: genkgo/xsl
 /**
  * @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';
 }
コード例 #3
0
ファイル: DateTimeFormatter.php プロジェクト: genkgo/xsl
 /**
  * @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)
 {
     $result = [];
     $i = 0;
     while (true) {
         while ($i < strlen($picture) && substr($picture, $i, 1) != '[') {
             $result[] = $this->escape(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])) {
                 $result[] = $this->components[$specifier]->format($pictureString, $date);
             } else {
                 $exception = new InvalidArgumentException("Component [{$specifier}] is not supported");
                 $exception->setErrorCode('XTDE1340');
                 throw $exception;
             }
             $i = $close + 1;
         }
     }
     return $date->format(implode('', $result));
 }
コード例 #4
0
ファイル: IntlDateTimeFormatter.php プロジェクト: genkgo/xsl
 /**
  * @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));
 }
コード例 #5
0
ファイル: PictureStringTest.php プロジェクト: genkgo/xsl
 public function testUnlimitedMaxWidth()
 {
     $pictureString = new PictureString('FNn,1-*');
     $this->assertNull($pictureString->getMaxWidth());
 }