<commit_before><?php
/**
 * Base class for JSON models.
 *
 * @since 1.0
 * @package Components
 * @author <NAME> <<EMAIL>>
 */
class CBJsonModel extends CFormModel
{
	/**
	 * Throw exception if validation fails.
	 *
	 * @param array $attributes
	 * @param boolean $clearErrors
	 * @return boolean
	 * @throws CException
	 */
	public function validate($attributes = null, $clearErrors = true)
	{
		if (parent::validate($attributes, $clearErrors)) {
			return true;
		}

		$allErrors = array();
		foreach ($this->getErrors() as $attrName=>$attrErrors) {
			$allErrors = array_merge($allErrors, $attrErrors);
		}

		throw new CException(implode("\n", $allErrors));
	}

	/**
	 * Types of non-scalar members.
	 *
	 * @return string[]
	 */
	public function getAttributeTypes()
	{
		return array();
	}

	/**
	 * Initialize from source object/array.
	 *
	 * @param array|object $source
	 * @return CBJsonModel
	 */
	public function copyFrom($source)
	{
		if (is_array($source)) {
			$isSourceArray = true;
		} else if (is_object($source)) {
			$isSourceArray = false;
		} else {
			throw new CException(get_class($this).' cannot be initialized from a '
					.gettype($source).'. An array or object is required');
		}

		// Our attribute types
		$attrTypes = $this->getAttributeTypes();

		foreach($this->attributeNames() as $attrName) {
			// Loop through our attributes

			if ($isSourceArray) {
				// Array key
				$attrValue = isset($source[$attrName]) ? $source[$attrName] : null;
			} else {
				// Object property
				$attrValue = isset($source->$attrName) ? $source->$attrName : null;
			}

			if ($attrValue === null) {
				// Attribute not in source
				continue;
			}

			if (!isset($attrTypes[$attrName])) {
				// Scalar type
				$this->$attrName = $attrValue;
			} else {
				// Object or array of objects
				$attrType = $attrTypes[$attrName];

				if (substr($attrType, -2) === '[]') {
					// Array type
					$isAttrTypeArray = true;
					// Remove "[]"
					$attrType = substr($attrType, 0, -2);
				} else {
					// Non-array type
					$isAttrTypeArray = false;
				}

				// Object or array of objects
				if ($isAttrTypeArray) {
					// Array of objects

					if (!is_array($attrValue)) {
						// Make sure value is an array of objects
						throw new CException('Attribute '.$attrName.' of '.get_class($this)
								.' must be an array of '.$attrType.' instances');
					}

					$arrayJsonModels = array();
					foreach ($attrValue as $fromJsonAttributeElement) {
						// Loop through array of objects and create corresponding json models
						$arrayJsonModels[] = $this->castFrom($attrType, $fromJsonAttributeElement);
					}
					$this->$attrName = $arrayJsonModels;

				} else {
					// Simple object
					$this->$attrName = $this->castFrom($attrType, $attrValue);
				}
			}
		}

		return $this;
	}

	/**
	 * Cast $sourceData into $targetType.
	 *
	 * @param string $targetType
	 * @param mixed $sourceData
	 * @return mixed
	 */
	private function castFrom($targetType, &$sourceData)
	{
		switch ($targetType) {
		case 'integer':
			return intval($sourceData);

		case 'float':
			return floatval($sourceData);

		case 'boolean':
			return boolval($sourceData);<commit_msg>Added double, utc8601datetime and utctimestamp data types<commit_after><?php
/**
 * Base class for JSON models.
 *
 * @since 1.1
 * @package Components
 * @author <NAME> <konfilios<EMAIL>>
 */
class CBJsonModel extends CFormModel
{
	/**
	 * Throw exception if validation fails.
	 *
	 * @param array $attributes
	 * @param boolean $clearErrors
	 * @return boolean
	 * @throws CException
	 */
	public function validate($attributes = null, $clearErrors = true)
	{
		if (parent::validate($attributes, $clearErrors)) {
			return true;
		}

		$allErrors = array();
		foreach ($this->getErrors() as $attrName=>$attrErrors) {
			$allErrors = array_merge($allErrors, $attrErrors);
		}

		throw new CException(implode("\n", $allErrors));
	}

	/**
	 * Types of non-scalar members.
	 *
	 * @return string[]
	 */
	public function getAttributeTypes()
	{
		return array();
	}

	/**
	 * Initialize from source object/array.
	 *
	 * @param array|object $source
	 * @return CBJsonModel
	 */
	public function copyFrom($source)
	{
		if (is_array($source)) {
			$isSourceArray = true;
		} else if (is_object($source)) {
			$isSourceArray = false;
		} else {
			throw new CException(get_class($this).' cannot be initialized from a '
					.gettype($source).'. An array or object is required');
		}

		// Our attribute types
		$attrTypes = $this->getAttributeTypes();

		foreach($this->attributeNames() as $attrName) {
			// Loop through our attributes

			if ($isSourceArray) {
				// Array key
				$attrValue = isset($source[$attrName]) ? $source[$attrName] : null;
			} else {
				// Object property
				$attrValue = isset($source->$attrName) ? $source->$attrName : null;
			}

			if ($attrValue === null) {
				// Attribute not in source
				continue;
			}

			if (!isset($attrTypes[$attrName])) {
				// Scalar type
				$this->$attrName = $attrValue;
			} else {
				// Object or array of objects
				$attrType = $attrTypes[$attrName];

				if (substr($attrType, -2) === '[]') {
					// Array type
					$isAttrTypeArray = true;
					// Remove "[]"
					$attrType = substr($attrType, 0, -2);
				} else {
					// Non-array type
					$isAttrTypeArray = false;
				}

				// Object or array of objects
				if ($isAttrTypeArray) {
					// Array of objects

					if (!is_array($attrValue)) {
						// Make sure value is an array of objects
						throw new CException('Attribute '.$attrName.' of '.get_class($this)
								.' must be an array of '.$attrType.' instances');
					}

					$arrayJsonModels = array();
					foreach ($attrValue as $fromJsonAttributeElement) {
						// Loop through array of objects and create corresponding json models
						$arrayJsonModels[] = $this->castFrom($attrType, $fromJsonAttributeElement);
					}
					$this->$attrName = $arrayJsonModels;

				} else {
					// Simple object
					$this->$attrName = $this->castFrom($attrType, $attrValue);
				}
			}
		}

		return $this;
	}

	/**
	 * UTC datetime object.
	 *
	 * @var Datetime
	 */
	private $utcDatetime;

	/**
	 * @return Datetime
	 */
	private function getUtcDatetime()
	{
		if ($this->utcDatetime === null) {
			$this->utcDatetime = new DateTime();
			$this->utcDatetime->setTimezone(new DateTimeZone('UTC'));
		}

		return $this->utcDatetime;
	}

	/**
	 * Cast $sourceData into $targetType.
	 *
	 * @param string $targetType
	 * @param mixed $sourceData
	 * @return mixed
	 */
	private function castFrom($targetType, &$sourceData)
	{
		switch ($targetType) {
		case 'integer':
			return intval($sourceData);

		case 'double':
		case 'float':
			return floatval($sourceData);

		case 'boolean':
			return boolval($sourceData);

		case 'utc8601datetime':
			return $this->getUtcDatetime()->modify($sourceData)->format("Y-m-d\TH:i:s\Z");

		case 'utctimestamp':
			return $this->getUtcDatetime()->modify($sourceData)->format("U");
{'fraction_non_alphanumeric': 0.1507437070938215, 'fraction_numerical': 0.002574370709382151, 'mean_word_length': 3.037528868360277, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3919197', 'n_tokens_mistral': 2677, 'n_tokens_neox': 2174, 'n_words': 563}