<commit_before><?php
namespace GMO\Common;

use GMO\Common\Exception\NotSerializableException;

abstract class AbstractSerializable implements ISerializable {

	/**
	 * If overriding this, be sure to include "class" with the fully qualified class name
	 * @return array
	 */
	public function toArray() {
		$values = array(
			"class" => get_called_class()
		);

		$objVars = get_object_vars( $this );
		foreach ( $objVars as $key => $value ) {
			if ( $value instanceof ISerializable ) {
				$values[$key] = $value->toArray();
			} elseif ($value instanceof \DateTime) {
				$values[$key] = json_decode(json_encode($value), true);
			} else {
				$values[$key] = $value;
			}
		}

		return $values;
	}

	/**
	 * Recreates object by calling the constructor with parameters
	 * that match the array keys (recursively for objects)
	 *
	 * Requirements:
	 * 1) Constructor parameters that are objects need to be type hinted
	 * 2) Constructor parameters that are objects need to extend AbstractSerializable or override fromArray
	 * 3) Constructor parameter names need to match the class variable names
	 *
	 * @param array $obj
	 * @throws NotSerializableException if a constructor takes an object that doesn't implement GMO\Common\ISerializable
	 * @return mixed
	 */
	public static function fromArray($obj) {
		$cls = new \ReflectionClass(get_called_class());
		$refParams = $cls->getConstructor()->getParameters();
		$params = array();
		foreach($refParams as $refParam) {
			$paramCls = $refParam->getClass();
			if (!array_key_exists($refParam->name, $obj)) {
				$params[] = $refParam->isOptional() ? $refParam->getDefaultValue() : null;
				continue;
			}
			if (!$paramCls) {
				$params[] = $obj[$refParam->name];
			} elseif ($paramCls->name === "DateTime") {
				$timestamp = $obj[$refParam->name];
				$tz = $timestamp['timezone'] ? new \DateTimeZone($timestamp['timezone']) : null;
				$params[] = new \DateTime($timestamp['date'], $tz);
			} elseif ($paramCls->isSubclassOf('GMO\Common\ISerializable')) {
				/** @var ISerializable $clsName */
				$clsName = $paramCls->name;
				$params[] = $clsName::fromArray($obj[$refParam->name]);
			} else {
				throw new NotSerializableException($paramCls->name . ' does not implement GMO\Common\ISerializable');
			}
		}
		return $cls->newInstanceArgs($params);
	}

	public function toJson() {
		return json_encode( $this->toArray() );
	}

	public static function fromJson($json) {
		return static::fromArray(json_decode( $json, true ));
	}

	public function jsonSerialize() {
		return $this->toArray();
	}

	public function serialize() {
		return $this->toJson();
	}

	public function unserialize($serialized) {
		return $this->fromJson($serialized);
	}

}<commit_msg>Updated AbstractSerializable::unserialize to set property values<commit_after><?php
namespace GMO\Common;

use GMO\Common\Exception\NotSerializableException;

abstract class AbstractSerializable implements ISerializable {

	/**
	 * If overriding this, be sure to include "class" with the fully qualified class name
	 * @return array
	 */
	public function toArray() {
		$values = array(
			"class" => get_called_class()
		);

		$objVars = get_object_vars( $this );
		foreach ( $objVars as $key => $value ) {
			if ( $value instanceof ISerializable ) {
				$values[$key] = $value->toArray();
			} elseif ($value instanceof \DateTime) {
				$values[$key] = json_decode(json_encode($value), true);
			} else {
				$values[$key] = $value;
			}
		}

		return $values;
	}

	/**
	 * Recreates object by calling the constructor with parameters
	 * that match the array keys (recursively for objects)
	 *
	 * Requirements:
	 * 1) Constructor parameters that are objects need to be type hinted
	 * 2) Constructor parameters that are objects need to extend AbstractSerializable or override fromArray
	 * 3) Constructor parameter names need to match the class variable names
	 *
	 * @param array $obj
	 * @throws NotSerializableException if a constructor takes an object that doesn't implement GMO\Common\ISerializable
	 * @return mixed
	 */
	public static function fromArray($obj) {
		$cls = new \ReflectionClass(get_called_class());
		$refParams = $cls->getConstructor()->getParameters();
		$params = array();
		foreach($refParams as $refParam) {
			$paramCls = $refParam->getClass();
			if (!array_key_exists($refParam->name, $obj)) {
				$params[] = $refParam->isOptional() ? $refParam->getDefaultValue() : null;
				continue;
			}
			if (!$paramCls) {
				$params[] = $obj[$refParam->name];
			} elseif ($paramCls->name === "DateTime") {
				$timestamp = $obj[$refParam->name];
				$tz = $timestamp['timezone'] ? new \DateTimeZone($timestamp['timezone']) : null;
				$params[] = new \DateTime($timestamp['date'], $tz);
			} elseif ($paramCls->isSubclassOf('GMO\Common\ISerializable')) {
				/** @var ISerializable $clsName */
				$clsName = $paramCls->name;
				$params[] = $clsName::fromArray($obj[$refParam->name]);
			} else {
				throw new NotSerializableException($paramCls->name . ' does not implement GMO\Common\ISerializable');
			}
		}
		return $cls->newInstanceArgs($params);
	}

	public function toJson() {
		return json_encode( $this->toArray() );
	}

	public static function fromJson($json) {
		return static::fromArray(json_decode( $json, true ));
	}

	public function jsonSerialize() {
		return $this->toArray();
	}

	public function serialize() {
		return $this->toJson();
	}

	public function unserialize($serialized) {
		$cls = $this->fromJson($serialized);
		$properties = get_class_vars(get_called_class());
		foreach ($properties as $property => $value) {
			$this->$property = $cls->$property;
		}
	}
}
{'fraction_non_alphanumeric': 0.14473217452997517, 'fraction_numerical': 0.001064207165661582, 'mean_word_length': 4.187672493100276, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, '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': '1755423', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1703, 'n_words': 450}