<commit_before><?php

defined('SYSPATH') or die('No direct script access.');

/**
 * Utility class to convert Models and Controllers
 * from and to objects of type stdClass
 *
 * @package Aurora
 * @author <NAME>
 * @copyright (c) 2013, <NAME>
 * @license http://license.enov.ws/mit MIT
 *
 */
class Aurora_Aurora_StdClass
{
	/**
	 * Converts a model to stdClass
	 * takes care of getters
	 *
	 * @param Model $model
	 * @return stdClass
	 */
	public static function from_model($model) {
		if (!Aurora_Type::is_model($model))
			throw new Kohana_Exception('Invalid model');
		// Get the list of properties
		$props = Aurora_Property::getters($model);
		// Create the stdClass object
		$std = new stdClass();
		// loop through the list of props
		foreach ($props as $prop => $arrProp) {
			// get the value
			$value = Aurora_Property::get($model, $prop);
			// if $value is a Model
			if (Aurora_Type::is_model($value)) {
				$std->$prop = static::from_model($value);
			}
			// if $value is a Collection
			else if (Aurora_Type::is_collection($value)) {
				$std->$prop = static::from_collection($value);
			}
			// Special care for DateTime. Is this generally acceptable?
			else if ($value instanceof DateTime) {
				$std->$prop = Date::format_iso8601($value);
			}
			// if scalar?
			else {
				$std->$prop = $value;
			}
		}
		return $std;
	}
	/**
	 * Converts an stdClass to the specified Model
	 *
	 * @param stdClass $stdObj
	 * @param Model/string $model
	 * @return Model
	 */
	public static function to_model($stdObj, $model) {
		$model = (is_string($model)) ? Aurora_Core::factory($model) : $model;
		$au = Aurora_Type::aurora($model);
		// Get the list of properties
		$props = Aurora_Property::setters($model);
		// loop through the list of props
		foreach ($props as $prop => $arrProp) {
			// test if property exists
			if (!property_exists($stdObj, $prop))
				continue;
			// get the value
			$value = $stdObj->$prop;
			// if property is primary key
			if ($prop == Aurora_Database::pkey($au)) {
				Aurora_Property::set_pkey($model, $value);
				continue;
			}
			// if property is setter
			if ($arrProp['type'] == 'method') {
				$setter = 'set_' . $prop;
				$typehint = Aurora_Reflection::typehint($model, $setter);
				$classname_only = TRUE;
				if (Aurora_Type::is_collection($typehint, $classname_only)) {
					$value = static::to_collection($value, $typehint);
				} else if (Aurora_Type::is_model($typehint, $classname_only)) {
					$value = static::to_model($value, $typehint);
				}
			}
			Aurora_Property::set($model, $prop, $value);
		}
		return $model;
	}
	/**
	 * Converts a collection (an object of type Collection)
	 * to an array of stdClass
	 *
	 * @param Collection $collection
	 * @return array
	 */
	public static function from_collection(Collection $collection) {<commit_msg>debuging<commit_after><?php

defined('SYSPATH') or die('No direct script access.');

/**
 * Utility class to convert Models and Controllers
 * from and to objects of type stdClass
 *
 * @package Aurora
 * @author <NAME>
 * @copyright (c) 2013, <NAME>
 * @license http://license.enov.ws/mit MIT
 *
 */
class Aurora_Aurora_StdClass
{
	/**
	 * Converts a model to stdClass
	 * takes care of getters
	 *
	 * @param Model $model
	 * @return stdClass
	 */
	public static function from_model($model) {
		if (!Aurora_Type::is_model($model))
			throw new Kohana_Exception('Invalid model');
		// Get the list of properties
		$props = Aurora_Property::getters($model);
		// Create the stdClass object
		$std = new stdClass();
		// loop through the list of props
		foreach ($props as $prop => $arrProp) {
			// get the value
			$value = Aurora_Property::get($model, $prop);
			// if $value is a Model
			if (Aurora_Type::is_model($value)) {
				$std->$prop = static::from_model($value);
			}
			// if $value is a Collection
			else if (Aurora_Type::is_collection($value)) {
				$std->$prop = static::from_collection($value);
			}
			// Special care for DateTime. Is this generally acceptable?
			else if ($value instanceof DateTime) {
				$std->$prop = Date::format_iso8601($value);
			}
			// if scalar?
			else {
				$std->$prop = $value;
			}
		}
		return $std;
	}
	/**
	 * Converts an stdClass to the specified Model
	 *
	 * @param stdClass $stdObj
	 * @param Model/string $model
	 * @return Model
	 */
	public static function to_model($stdObj, $model) {
		$model = (is_string($model)) ? Aurora_Core::factory($model) : $model;
		$au = Aurora_Type::aurora($model);
		// Get the list of properties
		$props = Aurora_Property::setters($model);
		// loop through the list of props
		foreach ($props as $prop => $arrProp) {
			// test if property exists
			if (!property_exists($stdObj, $prop))
				continue;
			// get the value
			$value = $stdObj->$prop;
			// if property is primary key
			if ($prop == Aurora_Database::pkey($au)) {
				Aurora_Property::set_pkey($model, $value);
				continue;
			}
			// if property is setter
			if ($arrProp['type'] == 'method' AND $value != NULL) {
			fire::dump('prop', $prop);
			fire::dump('arrProp', $arrProp);
			fire::dump('value', $value);
				$setter = 'set_' . $prop;
				$typehint = Aurora_Reflection::typehint($model, $setter);
				$classname_only = TRUE;
				fire::dump('typehint', $typehint);
				if (Aurora_Type::is_collection($typehint, $classname_only)) {
					$value = static::to_collection($value, $typehint);
					fire::dump('typehint_is_collection', $typehint);
				} else if (Aurora_Type::is_model($typehint, $classname_only)) {
					fire::dump('typehint_model', 'success');
					$value = static::to_model($value, $typehint);
					fire::dump('typehint_is_model', $typehint);
				}
			}
			Aurora_Property::set($model, $prop, $value);
		}
		return $model;
	}
	/**
	 * Converts a collection (an object of type Collection)
	 * to an array of stdClass
	 *
	 * @param Collection $collection
	 * @return array
	 */
	public static function from_collection(Collection $collection) {
{'fraction_non_alphanumeric': 0.15129585997980477, 'fraction_numerical': 0.0026926960619320095, 'mean_word_length': 3.5750577367205545, 'pattern_counts': {'":': 0, '<': 9, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3084466', 'n_tokens_mistral': 2245, 'n_tokens_neox': 1925, 'n_words': 525}