When third parameter ($formatLength) equals 'default', default format for a
locale will be used.
/** * Formats dateTime with format string for date and time defined in CLDR for * particular locale. * * First date and time are formatted separately, and then dateTime format * from CLDR is used to place date and time in correct order. * * @param \DateTimeInterface $dateTime PHP object representing particular point in time * @param Locale $locale * @param string $formatLength One of DatesReader FORMAT_LENGTH constants * @return string Formatted date and time * @api */ public function formatDateTime(\DateTimeInterface $dateTime, Locale $locale, $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT) { DatesReader::validateFormatLength($formatLength); return $this->doFormattingWithParsedFormat($dateTime, $this->datesReader->parseFormatFromCldr($locale, DatesReader::FORMAT_TYPE_DATETIME, $formatLength), $this->datesReader->getLocalizedLiteralsForLocale($locale)); }
/** * @test */ public function dateTimeFormatIsParsedCorrectly() { $mockModel = $this->getAccessibleMock(I18n\Cldr\CldrModel::class, ['getElement'], [[]]); $mockModel->expects($this->at(0))->method('getElement')->with('dates/calendars/calendar[@type="gregorian"]/dateTimeFormats/dateTimeFormatLength[@type="full"]/dateTimeFormat/pattern')->will($this->returnValue('foo {0} {1} bar')); $mockModel->expects($this->at(1))->method('getElement')->with('dates/calendars/calendar[@type="gregorian"]/dateFormats/dateFormatLength[@type="full"]/dateFormat/pattern')->will($this->returnValue('dMy')); $mockModel->expects($this->at(2))->method('getElement')->with('dates/calendars/calendar[@type="gregorian"]/timeFormats/timeFormatLength[@type="full"]/timeFormat/pattern')->will($this->returnValue('hms')); $mockRepository = $this->createMock(I18n\Cldr\CldrRepository::class); $mockRepository->expects($this->exactly(3))->method('getModelForLocale')->with($this->sampleLocale)->will($this->returnValue($mockModel)); $mockCache = $this->getMockBuilder(VariableFrontend::class)->disableOriginalConstructor()->getMock(); $this->createCacheExpectations($mockCache); $reader = new I18n\Cldr\Reader\DatesReader(); $reader->injectCldrRepository($mockRepository); $reader->injectCache($mockCache); $reader->initializeObject(); $result = $reader->parseFormatFromCldr($this->sampleLocale, I18n\Cldr\Reader\DatesReader::FORMAT_TYPE_DATETIME, I18n\Cldr\Reader\DatesReader::FORMAT_LENGTH_FULL); $this->assertEquals([['foo '], 'h', 'm', 's', [' '], 'd', 'M', 'y', [' bar']], $result); $reader->shutdownObject(); }
/** * Parses dateTime with format string for date and time defined in CLDR for * particular locale. * * @param string $dateAndTimeToParse Date and time to be parsed * @param I18n\Locale $locale * @param string $formatLength One of: full, long, medium, short, or 'default' in order to use default length from CLDR * @param boolean $strictMode Work mode (strict when TRUE, lenient when FALSE) * @return mixed Array of parsed date and time elements, FALSE on failure */ public function parseDateAndTime($dateAndTimeToParse, I18n\Locale $locale, $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT, $strictMode = true) { DatesReader::validateFormatLength($formatLength); return $this->doParsingWithParsedFormat($dateAndTimeToParse, $this->datesReader->parseFormatFromCldr($locale, DatesReader::FORMAT_TYPE_DATETIME, $formatLength), $this->datesReader->getLocalizedLiteralsForLocale($locale), $strictMode); }