<commit_before><?php
namespace TYPO3\FLOW3\I18n\Parser;

/*                                                                        *
 * This script belongs to the FLOW3 framework.                            *
 *                                                                        *
 * It is free software; you can redistribute it and/or modify it under    *
 * the terms of the GNU Lesser General Public License, either version 3   *
 * of the License, or (at your option) any later version.                 *
 *                                                                        *
 * The TYPO3 project - inspiring people to share!                         *
 *                                                                        */

use TYPO3\FLOW3\Annotations as FLOW3;

/**
 * Parser for date and time.
 *
 * @FLOW3\Scope("singleton")
 * @api
 */
class DatetimeParser {

	/**
	 * Regex pattern for matching abbreviated timezones, like GMT, CEST, etc.
	 * Two versions for strict and lenient matching modes.
	 */
	const PATTERN_MATCH_STRICT_TIMEZONE_ABBREVIATION = '/^[A-Z]{3,5}/';
	const PATTERN_MATCH_LENIENT_TIMEZONE_ABBREVIATION = '/[A-Z]{3,5}/';

	/**
	 * Regex pattern for matching TZ database timezones, like Europe/London.
	 * Two versions for strict and lenient matching modes.
	 */
	const PATTERN_MATCH_STRICT_TIMEZONE_TZ = '/^[A-z]+\/[A-z_]+(:?\/[A-z_]+)?/';
	const PATTERN_MATCH_LENIENT_TIMEZONE_TZ = '/[A-z]+\/[A-z_]+(:?\/[A-z_]+)?/';

	/**
	 * @var \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader
	 */
	protected $datesReader;

	/**
	 * @param \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader $datesReader
	 * @return void
	 */
	public function injectDatesReader(\TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader $datesReader) {
		$this->datesReader = $datesReader;
	}

	/**
	 * Returns dateTime parsed by custom format (string provided in parameter).
	 *
	 * Format must obey syntax defined in CLDR specification, excluding
	 * unimplemented features (see documentation for DatesReader class).
	 *
	 * Format is remembered in cache and won't be parsed again for some time.
	 *
	 * @param \DateTime $datetimeToParse PHP object representing particular point in time
	 * @param string $format Format string
	 * @param \TYPO3\FLOW3\I18n\Locale $locale A locale used for finding literals array
	 * @param boolean $strictMode Work mode (strict when TRUE, lenient when FALSE)
	 * @return mixed Array of parsed date / time elements, FALSE on failure
	 * @api
	 * @see \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader
	 */
	public function parseDatetimeWithCustomPattern($datetimeToParse, $format, \TYPO3\FLOW3\I18n\Locale $locale, $strictMode = TRUE) {
		return $this->doParsingWithParsedFormat($datetimeToParse, $this->datesReader->parseCustomFormat($format), $this->datesReader->getLocalizedLiteralsForLocale($locale), $strictMode);
	}

	/**
	 * Parses date with format string for date defined in CLDR for particular
	 * locale.
	 *
	 * @param \DateTime $dateToParse PHP object representing particular point in time
	 * @param \TYPO3\FLOW3\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 elements, FALSE on failure
	 * @api
	 */
	public function parseDate($dateToParse, \TYPO3\FLOW3\I18n\Locale $locale, $formatLength = \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_LENGTH_DEFAULT, $strictMode = TRUE) {
		\TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::validateFormatLength($formatLength);
		return $this->doParsingWithParsedFormat($dateToParse, $this->datesReader->parseFormatFromCldr($locale, \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_TYPE_DATE, $formatLength), $this->datesReader->getLocalizedLiteralsForLocale($locale), $strictMode);
	}

	/**
	 * Parses time with format string for time defined in CLDR for particular
	 * locale.
	 *
	 * @param \DateTime $timeToParse PHP object representing particular point in time
	 * @param \TYPO3\FLOW3\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 time elements, FALSE on failure
	 * @api
	 */
	public function parseTime($timeToParse, \TYPO3\FLOW3\I18n\Locale $locale, $formatLength = \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_LENGTH_DEFAULT, $strictMode = TRUE) {
		\TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::validateFormatLength($formatLength);
		return $this->doParsingWithParsedFormat($timeToParse, $this->datesReader->parseFormatFromCldr($locale, \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_TYPE_TIME, $formatLength), $this->datesReader->getLocalizedLiteralsForLocale($locale), $strictMode);
	}

	/**
	 * Parses dateTime with format string for date and time defined in CLDR for
	 * particular locale.
	 *
	 * @param \DateTime $dateAndTimeToParse PHP object representing particular point in time
	 * @param \TYPO3\FLOW3\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, \TYPO3\FLOW3\I18n\Locale $locale, $formatLength = \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_LENGTH_DEFAULT, $strictMode = TRUE) {
		\TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::validateFormatLength($formatLength);
		return $this->doParsingWithParsedFormat($dateAndTimeToParse, $this->datesReader->parseFormatFromCldr($locale, \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_TYPE_DATETIME, $formatLength), $this->datesReader->getLocalizedLiteralsForLocale($locale), $strictMode);
	}

	/**
	 * Parses date and / or time using parsed format, in strict or lenient mode.
	 *
	 * @param string $datetimeToParse Number to be parsed
	 * @param array $parsedFormat Parsed format (from NumbersReader)
	 * @param array $localizedLiterals An array with symbols to use
	 * @param boolean $strictMode Work mode (strict when TRUE, lenient when FALSE)
	 * @return mixed Array of parsed date and / or time elements, FALSE on failure
	 */
	protected function doParsingWithParsedFormat($datetimeToParse, array $parsedFormat, array $localizedLiterals, $strictMode) {
		return ($strictMode) ? $this->doParsingInStrictMode($datetimeToParse, $parsedFormat, $localizedLiterals) : $this->doParsingInLenientMode($datetimeToParse, $parsedFormat, $localizedLiterals);
	}

	/**
	 * Parses date and / or time in strict mode.
	 *
	 * @param string $datetimeToParse Date/time to be parsed
	 * @param array $parsedFormat Format parsed by DatesReader
	 * @param array $localizedLiterals Array of date / time literals from CLDR
	 * @return mixed Array of parsed date and / or time elements, FALSE on failure
	 * @throws \TYPO3\FLOW3\I18n\Exception\InvalidArgumentException When unexpected symbol found in format
	 * @see \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader
	 */
	protected function doParsingInStrictMode($datetimeToParse, array $parsedFormat, array $localizedLiterals) {
		$datetimeElements = array(
			'year' => NULL,
			'month' => NULL,
			'day' => NULL,
			'hour' => NULL,
			'minute' => NULL,
			'second' => NULL,<commit_msg>[BUGFIX] Fix code documentation of DatetimeParser<commit_after><?php
namespace TYPO3\FLOW3\I18n\Parser;

/*                                                                        *
 * This script belongs to the FLOW3 framework.                            *
 *                                                                        *
 * It is free software; you can redistribute it and/or modify it under    *
 * the terms of the GNU Lesser General Public License, either version 3   *
 * of the License, or (at your option) any later version.                 *
 *                                                                        *
 * The TYPO3 project - inspiring people to share!                         *
 *                                                                        */

use TYPO3\FLOW3\Annotations as FLOW3;

/**
 * Parser for date and time.
 *
 * @FLOW3\Scope("singleton")
 * @api
 */
class DatetimeParser {

	/**
	 * Regex pattern for matching abbreviated timezones, like GMT, CEST, etc.
	 * Two versions for strict and lenient matching modes.
	 */
	const PATTERN_MATCH_STRICT_TIMEZONE_ABBREVIATION = '/^[A-Z]{3,5}/';
	const PATTERN_MATCH_LENIENT_TIMEZONE_ABBREVIATION = '/[A-Z]{3,5}/';

	/**
	 * Regex pattern for matching TZ database timezones, like Europe/London.
	 * Two versions for strict and lenient matching modes.
	 */
	const PATTERN_MATCH_STRICT_TIMEZONE_TZ = '/^[A-z]+\/[A-z_]+(:?\/[A-z_]+)?/';
	const PATTERN_MATCH_LENIENT_TIMEZONE_TZ = '/[A-z]+\/[A-z_]+(:?\/[A-z_]+)?/';

	/**
	 * @var \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader
	 */
	protected $datesReader;

	/**
	 * @param \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader $datesReader
	 * @return void
	 */
	public function injectDatesReader(\TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader $datesReader) {
		$this->datesReader = $datesReader;
	}

	/**
	 * Returns dateTime parsed by custom format (string provided in parameter).
	 *
	 * Format must obey syntax defined in CLDR specification, excluding
	 * unimplemented features (see documentation for DatesReader class).
	 *
	 * Format is remembered in cache and won't be parsed again for some time.
	 *
	 * @param string $datetimeToParse Date/time to be parsed
	 * @param string $format Format string
	 * @param \TYPO3\FLOW3\I18n\Locale $locale A locale used for finding literals array
	 * @param boolean $strictMode Work mode (strict when TRUE, lenient when FALSE)
	 * @return mixed Array of parsed date / time elements, FALSE on failure
	 * @api
	 * @see \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader
	 */
	public function parseDatetimeWithCustomPattern($datetimeToParse, $format, \TYPO3\FLOW3\I18n\Locale $locale, $strictMode = TRUE) {
		return $this->doParsingWithParsedFormat($datetimeToParse, $this->datesReader->parseCustomFormat($format), $this->datesReader->getLocalizedLiteralsForLocale($locale), $strictMode);
	}

	/**
	 * Parses date with format string for date defined in CLDR for particular
	 * locale.
	 *
	 * @param string $dateToParse date to be parsed
	 * @param \TYPO3\FLOW3\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 elements, FALSE on failure
	 * @api
	 */
	public function parseDate($dateToParse, \TYPO3\FLOW3\I18n\Locale $locale, $formatLength = \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_LENGTH_DEFAULT, $strictMode = TRUE) {
		\TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::validateFormatLength($formatLength);
		return $this->doParsingWithParsedFormat($dateToParse, $this->datesReader->parseFormatFromCldr($locale, \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_TYPE_DATE, $formatLength), $this->datesReader->getLocalizedLiteralsForLocale($locale), $strictMode);
	}

	/**
	 * Parses time with format string for time defined in CLDR for particular
	 * locale.
	 *
	 * @param string $timeToParse Time to be parsed
	 * @param \TYPO3\FLOW3\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 time elements, FALSE on failure
	 * @api
	 */
	public function parseTime($timeToParse, \TYPO3\FLOW3\I18n\Locale $locale, $formatLength = \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_LENGTH_DEFAULT, $strictMode = TRUE) {
		\TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::validateFormatLength($formatLength);
		return $this->doParsingWithParsedFormat($timeToParse, $this->datesReader->parseFormatFromCldr($locale, \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_TYPE_TIME, $formatLength), $this->datesReader->getLocalizedLiteralsForLocale($locale), $strictMode);
	}

	/**
	 * 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 \TYPO3\FLOW3\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, \TYPO3\FLOW3\I18n\Locale $locale, $formatLength = \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_LENGTH_DEFAULT, $strictMode = TRUE) {
		\TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::validateFormatLength($formatLength);
		return $this->doParsingWithParsedFormat($dateAndTimeToParse, $this->datesReader->parseFormatFromCldr($locale, \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader::FORMAT_TYPE_DATETIME, $formatLength), $this->datesReader->getLocalizedLiteralsForLocale($locale), $strictMode);
	}

	/**
	 * Parses date and / or time using parsed format, in strict or lenient mode.
	 *
	 * @param string $datetimeToParse Date/time to be parsed
	 * @param array $parsedFormat Parsed format (from DatesReader)
	 * @param array $localizedLiterals Array of date / time literals from CLDR
	 * @param boolean $strictMode Work mode (strict when TRUE, lenient when FALSE)
	 * @return mixed Array of parsed date and / or time elements, FALSE on failure
	 */
	protected function doParsingWithParsedFormat($datetimeToParse, array $parsedFormat, array $localizedLiterals, $strictMode) {
		return ($strictMode) ? $this->doParsingInStrictMode($datetimeToParse, $parsedFormat, $localizedLiterals) : $this->doParsingInLenientMode($datetimeToParse, $parsedFormat, $localizedLiterals);
	}

	/**
	 * Parses date and / or time in strict mode.
	 *
	 * @param string $datetimeToParse Date/time to be parsed
	 * @param array $parsedFormat Format parsed by DatesReader
	 * @param array $localizedLiterals Array of date / time literals from CLDR
	 * @return array Array of parsed date and / or time elements, FALSE on failure
	 * @throws \TYPO3\FLOW3\I18n\Exception\InvalidArgumentException When unexpected symbol found in format
	 * @see \TYPO3\FLOW3\I18n\Cldr\Reader\DatesReader
	 */
	protected function doParsingInStrictMode($datetimeToParse, array $parsedFormat, array $localizedLiterals) {
		$datetimeElements = array(
			'year' => NULL,
			'month' => NULL,
			'day' => NULL,
			'hour' => NULL,
			'minute' => NULL,
			'second' => NULL,
{'fraction_non_alphanumeric': 0.10815722169874815, 'fraction_numerical': 0.014403015210660923, 'mean_word_length': 3.6448890278211943, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3219434', 'n_tokens_mistral': 4783, 'n_tokens_neox': 4473, 'n_words': 1265}