<commit_before><?php
namespace GMO\Common\Web;

use Carbon\Carbon;
use GMO\Common\Web\Exception;
use LogicException;
use Symfony\Component\HttpFoundation\ParameterBag as ParameterBagBase;

/**
 * {@inheritdoc}
 *
 * Additional methods have been added.
 */
class ParameterBag extends ParameterBagBase {

	protected $required = false;

	/**
	 * Sets the next parameter retrieved to be required.
	 *
	 * @return $this
	 */
	public function required() {
		$this->required = true;

		return $this;
	}

	/**
	 * Returns a parameter by name.
	 *
	 * @param string $key     The key
	 * @param mixed  $default The default value if the parameter key does not exist
	 * @param bool   $deep    If true, a path like foo[bar] will find deeper items
	 *
	 * @throws Exception\MissingParameterException If the parameter is required and does not exist.
	 * @throws Exception\InvalidParameterException If the parameter is required and empty.
	 *
	 * @return mixed
	 */
	public function get($key, $default = null, $deep = false) {
		if (!$this->required) {
			return parent::get($key, $default, $deep);
		}
		$this->required = false;

		if (!$this->has($key)) {
			throw new Exception\MissingParameterException($key);
		}

		$value = parent::get($key);

		if (empty($value)) {
			throw new Exception\InvalidParameterException($key, '%s should not be empty');
		}

		return $value;
	}

	/**
	 * @deprecated
	 */
	public function getRequired($key) {
		return $this->required()->get($key);
	}

	/**
	 * Wrapper to throw NoKeyException if API key does not exist.
	 *
	 * @param string $keyName
	 *
	 * @throws Exception\InvalidKeyException
	 * @throws Exception\NoKeyException
	 *
	 * @return mixed
	 */
	public function getApiKey($keyName = 'key') {
		try {
			return $this->required()->get($keyName);
		} catch (Exception\MissingParameterException $e) {
			throw new Exception\NoKeyException($keyName);
		} catch (Exception\InvalidParameterException $e) {
			throw new Exception\InvalidKeyException($keyName);
		}
	}

	/**
	 * Returns a parameter value converted to a Carbon instance.
	 *
	 * If the default value is an int or string it will be converted to a Carbon instance.
	 *
	 * @param string                 $key     The key
	 * @param Carbon|int|string|null $default The default value if the parameter key doesn't exist or is empty
	 * @param string                 $tz      The timezone to create the Carbon instances with. Default: UTC
	 *
	 * @throws Exception\InvalidParameterException If the parameter value fails to parse
	 * @throws LogicException If Carbon is not installed
	 *
	 * @return Carbon|null A Carbon timestamp or null if the key was empty and default is null
	 */
	public function getTimestamp($key, $default = null, $tz = 'UTC') {
		if (!class_exists('\Carbon\Carbon')) {
			throw new LogicException('Carbon library is not installed');
		}

		$timestamp = $this->get($key);
		if (!empty($timestamp)) {
			try {
				if (is_numeric($timestamp)) {
					$carbon = Carbon::createFromTimestamp($timestamp, $tz);
				} else {
					$carbon = new Carbon($timestamp, $tz);
				}
			} catch (\Exception $e) {
				throw new Exception\InvalidParameterException($key);
			}
		} else {
			if ($default === null) {
				return null;
			}
			if (!$default instanceof Carbon) {
				$carbon = Carbon::createFromTimestamp($default, $tz);
			} else {
				$carbon = $default;
			}
		}

		// Assert date is in valid range
		$carbon = max($carbon, Carbon::create(1000, 1, 1, 0, 0, 0));
		$carbon = min($carbon, Carbon::create(9999, 12, 31, 23, 59, 59));

		return $carbon;
	}
}
<commit_msg>[ParameterBag] getTimestamp default timezone to null (aka timezone set in php.ini)<commit_after><?php
namespace GMO\Common\Web;

use Carbon\Carbon;
use GMO\Common\Web\Exception;
use LogicException;
use Symfony\Component\HttpFoundation\ParameterBag as ParameterBagBase;

/**
 * {@inheritdoc}
 *
 * Additional methods have been added.
 */
class ParameterBag extends ParameterBagBase {

	protected $required = false;

	/**
	 * Sets the next parameter retrieved to be required.
	 *
	 * @return $this
	 */
	public function required() {
		$this->required = true;

		return $this;
	}

	/**
	 * Returns a parameter by name.
	 *
	 * @param string $key     The key
	 * @param mixed  $default The default value if the parameter key does not exist
	 * @param bool   $deep    If true, a path like foo[bar] will find deeper items
	 *
	 * @throws Exception\MissingParameterException If the parameter is required and does not exist.
	 * @throws Exception\InvalidParameterException If the parameter is required and empty.
	 *
	 * @return mixed
	 */
	public function get($key, $default = null, $deep = false) {
		if (!$this->required) {
			return parent::get($key, $default, $deep);
		}
		$this->required = false;

		if (!$this->has($key)) {
			throw new Exception\MissingParameterException($key);
		}

		$value = parent::get($key);

		if (empty($value)) {
			throw new Exception\InvalidParameterException($key, '%s should not be empty');
		}

		return $value;
	}

	/**
	 * @deprecated
	 */
	public function getRequired($key) {
		return $this->required()->get($key);
	}

	/**
	 * Wrapper to throw NoKeyException if API key does not exist.
	 *
	 * @param string $keyName
	 *
	 * @throws Exception\InvalidKeyException
	 * @throws Exception\NoKeyException
	 *
	 * @return mixed
	 */
	public function getApiKey($keyName = 'key') {
		try {
			return $this->required()->get($keyName);
		} catch (Exception\MissingParameterException $e) {
			throw new Exception\NoKeyException($keyName);
		} catch (Exception\InvalidParameterException $e) {
			throw new Exception\InvalidKeyException($keyName);
		}
	}

	/**
	 * Returns a parameter value converted to a Carbon instance.
	 *
	 * If the default value is an int or string it will be converted to a Carbon instance.
	 *
	 * @param string                 $key     The key
	 * @param Carbon|int|string|null $default The default value if the parameter key doesn't exist or is empty
	 * @param string                 $tz      The timezone to create the Carbon instances with.
	 *
	 * @throws Exception\InvalidParameterException If the parameter value fails to parse
	 * @throws LogicException If Carbon is not installed
	 *
	 * @return Carbon|null A Carbon timestamp or null if the key was empty and default is null
	 */
	public function getTimestamp($key, $default = null, $tz = null) {
		if (!class_exists('\Carbon\Carbon')) {
			throw new LogicException('Carbon library is not installed');
		}

		$timestamp = $this->get($key);
		if (!empty($timestamp)) {
			try {
				if (is_numeric($timestamp)) {
					$carbon = Carbon::createFromTimestamp($timestamp, $tz);
				} else {
					$carbon = new Carbon($timestamp, $tz);
				}
			} catch (\Exception $e) {
				throw new Exception\InvalidParameterException($key);
			}
		} else {
			if ($default === null) {
				return null;
			}
			if (!$default instanceof Carbon) {
				$carbon = Carbon::createFromTimestamp($default, $tz);
			} else {
				$carbon = $default;
			}
		}

		// Assert date is in valid range
		$carbon = max($carbon, Carbon::create(1000, 1, 1, 0, 0, 0));
		$carbon = min($carbon, Carbon::create(9999, 12, 31, 23, 59, 59));

		return $carbon;
	}
}

{'fraction_non_alphanumeric': 0.12362828170579247, 'fraction_numerical': 0.006389776357827476, 'mean_word_length': 3.3956043956043955, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '739080', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2052, 'n_words': 702}