<commit_before><?php
/**
 * Generated PHP model
 * @author <NAME> | grrr.nl
 * @modifiedby $LastChangedBy: $
 * @version $Revision: $
 * @package Garp
 * @subpackage Model
 * @lastmodified $Date: $
 */
class Garp_Model_Spawn_Php_Renderer {
	protected $_model;

	const _BASE_MODEL_PATH = '/modules/default/models/Base/';
	const _EXTENDED_MODEL_PATH = '/modules/default/models/';


	/**
	 * Some behaviors defined in the Spawner's model configuration do not need a PHP behavior, and should therefore not be injected into the model as such.
	 */
	protected $_behaviorsExcludedFromRendering = array('Locatable');
	
	protected $_behaviorsThatRequireParams = array('Weighable');
	


	public function __construct(Garp_Model_Spawn_Model $model) {
		$this->_model = $model;
	}


	public function save() {
		$indent = '        ';

		//	generate base model
		$baseModelPath = $this->_getBaseModelPath($this->_model->id);
		$baseModelContent = $this->_renderBaseModel();
		$this->_saveFile($baseModelPath, $baseModelContent, 'PHP base model', true);

		//	generate extended model
		$extendedModelPath = $this->_getExtendedModelPath($this->_model->id);
		$extendedModelContent = $this->_renderExtendedModel($this->_model->id);
		$this->_saveFile($extendedModelPath, $extendedModelContent, 'PHP extended model', false);

		//	generate hasAndBelongsToMany binding models that relate to this model
		$habtmRelations = $this->_model->relations->getRelations('type', 'hasAndBelongsToMany');
		if ($habtmRelations) {
			foreach ($habtmRelations as $habtmRelation) {
				$bindingModelName = Garp_Model_Spawn_Relations::getBindingModelName($habtmRelation->model, $this->_model->id);
				$bindingBaseModelPath = $this->_getBaseModelPath($bindingModelName);
				$bindingBaseModelContent = $this->_renderBindingBaseModel($bindingModelName, $this->_model->id, $habtmRelation);
				$this->_saveFile($bindingBaseModelPath, $bindingBaseModelContent, 'PHP base binding model to '.$habtmRelation->model, true);
				
				$bindingExtendedModelPath = $this->_getExtendedModelPath($bindingModelName);
				$bindingExtendedModelContent = $this->_renderExtendedModel($bindingModelName, false);
				$this->_saveFile($bindingExtendedModelPath, $bindingExtendedModelContent, 'PHP extended binding model to '.$habtmRelation->model, false);
			}
		}

		new Garp_Model_Spawn_Php_ModelsIncluder($this->_model->id);
	}
	
	
	protected function _saveFile($path, $content, $label, $overwrite = false) {
		if (
			$overwrite ||
			!$overwrite && !file_exists($path)
		) {
			if (!file_put_contents($path, $content)) {
				throw new Exception("Could not generate {$label}.");
			}
		}
	}


	protected static function _getBaseModelPath($modelId) {
		return APPLICATION_PATH.self::_BASE_MODEL_PATH.$modelId.'.php';
	}
	
	
	protected static function _getExtendedModelPath($modelId) {
		return APPLICATION_PATH.self::_EXTENDED_MODEL_PATH.$modelId.'.php';
	}


	/**
	 * Extracts the public properties from an iteratable object
	 */
	protected function _objectToArray($obj) {
		$arr = array();
		$arrObj = is_object($obj) ? get_object_vars($obj) : $obj;

		foreach ($arrObj as $key => $val) {
			if (is_object($val)) {
				if (get_class($val) === 'Garp_Model_Spawn_Relations') {
					$val = $val->getRelations();
				} elseif (get_class($val) === 'Garp_Model_Spawn_Behaviors') {
					$val = $val->getBehaviors();
				} elseif (get_class($val) === 'Garp_Model_Spawn_Fields') {
					$val = $val->getFields();
				}

				$val = $this->_objectToArray($val);
			} elseif (is_array($val)) {
				$val = $this->_objectToArray($val);
			}
			$arr[$key] = $val;
		}

		return $arr;
	}


	protected function _renderBaseModel() {
		$out = $this->_rl("<?php");
		$out.= $this->_rl('/* This file was generated by '.get_class().' */');

		$out.= $this->_rl("class Model_Base_{$this->_model->id} extends Garp_Model_Db {");

		/* Table */
		$out.= $this->_rl("protected \$_name = '{$this->_model->id}';", 1, 2);

		/* This model's scheme, deducted from the combined Spawn model configurations. */
		$out.= $this->_rl("protected \$_configuration = " . Garp_Model_Spawn_Util::array2phpStatement($this->_objectToArray($this->_model)) .";", 1, 2);

		/* Joint view to include labels of singular relations */
		$out.= $this->_rl("protected \$_jointView = '{$this->_model->id}_joint';", 1, 2);

		/* Default order */
		$orderStatement = (
			strpos($this->_model->order, ",") !== false &&
			strpos($this->_model->order, "(") === false) ?
			Garp_Model_Spawn_Util::array2phpStatement(explode(", ", $this->_model->order)) :
			"'".$this->_model->order."'";
		$out.= $this->_rl("protected \$_defaultOrder = {$orderStatement};", 1, 2);

		/* Relations */
		$relations = $this->_model->relations->getRelations();

		if (count($relations)) {
			/* Bindable */
			$i = 0;
			$out.= $this->_rl('protected $_bindable = array(', 1);

			$registeredBindableModels = array();
			$bindablesOutput = array();

			foreach ($relations as $relationName => $relation) {
				if (!in_array($relation->model, $registeredBindableModels)) {
					$registeredBindableModels[] = $relation->model;
					$bindablesOutput[] = $this->_rl("'Model_{$relation->model}'", 2, 0);
				}
			}
			$bindablesOutput = array_unique($bindablesOutput);
			sort($bindablesOutput);

			$out.= $this->_rl(implode(",\n", $bindablesOutput), 0);
			$out.= $this->_rl(');', 1, 2);
		

			/* ReferenceMap */
			$i = 0;
			$out.= $this->_rl('protected $_referenceMap = array(', 1);

			$referenceMapOutput = array();

			foreach ($relations as $relationName => $relation) {
				if (
					$relation->type === 'hasOne' ||
					$relation->type === 'belongsTo'
				) {
					$referenceMapOutput[] =
						 $this->_rl("'{$relationName}' => array(", 2)
						.$this->_rl("'refTableClass' => 'Model_{$relation->model}',", 3)
						.$this->_rl("'columns' => '{$relation->column}',", 3)
						.$this->_rl("'refColumns' => 'id'", 3)
						.$this->_rl(")", 2, 0)
					;
				}
			}
			
			$out.= $this->_rl(implode(",\n", $referenceMapOutput), 0);

			$out.= $this->_rl(');', 1, 3);
		}

		/* Default behaviors */
		$out.= $this->_rl("public function init() {", 1);
		$out.= $this->_rl('parent::init();', 2);
		$behaviors = $this->_model->behaviors->getBehaviors();
		foreach ($behaviors as $behaviorName => $behavior) {
			if (!in_array($behaviorName, $this->_behaviorsExcludedFromRendering)) {
				$behavior = $this->_filterBehaviorParams($behaviorName, $behavior);
				$behaviorOutput = $this->_renderBehavior($behavior);
				if ($behaviorOutput) {
					$out.= $this->_rl($behaviorOutput, 2);
				}
			}
		}
		$out.= $this->_rl('}', 1, 2);

		/*	Compose the method to fetch composite columns as a string in a MySql query<commit_msg>lowercase<commit_after><?php
/**
 * Generated PHP model
 * @author <NAME> | grrr.nl
 * @modifiedby $LastChangedBy: $
 * @version $Revision: $
 * @package Garp
 * @subpackage Model
 * @lastmodified $Date: $
 */
class Garp_Model_Spawn_Php_Renderer {
	protected $_model;

	const _BASE_MODEL_PATH = '/modules/default/models/Base/';
	const _EXTENDED_MODEL_PATH = '/modules/default/models/';


	/**
	 * Some behaviors defined in the Spawner's model configuration do not need a PHP behavior, and should therefore not be injected into the model as such.
	 */
	protected $_behaviorsExcludedFromRendering = array('Locatable');
	
	protected $_behaviorsThatRequireParams = array('Weighable');
	


	public function __construct(Garp_Model_Spawn_Model $model) {
		$this->_model = $model;
	}


	public function save() {
		$indent = '        ';

		//	generate base model
		$baseModelPath = $this->_getBaseModelPath($this->_model->id);
		$baseModelContent = $this->_renderBaseModel();
		$this->_saveFile($baseModelPath, $baseModelContent, 'PHP base model', true);

		//	generate extended model
		$extendedModelPath = $this->_getExtendedModelPath($this->_model->id);
		$extendedModelContent = $this->_renderExtendedModel($this->_model->id);
		$this->_saveFile($extendedModelPath, $extendedModelContent, 'PHP extended model', false);

		//	generate hasAndBelongsToMany binding models that relate to this model
		$habtmRelations = $this->_model->relations->getRelations('type', 'hasAndBelongsToMany');
		if ($habtmRelations) {
			foreach ($habtmRelations as $habtmRelation) {
				$bindingModelName = Garp_Model_Spawn_Relations::getBindingModelName($habtmRelation->model, $this->_model->id);
				$bindingBaseModelPath = $this->_getBaseModelPath($bindingModelName);
				$bindingBaseModelContent = $this->_renderBindingBaseModel($bindingModelName, $this->_model->id, $habtmRelation);
				$this->_saveFile($bindingBaseModelPath, $bindingBaseModelContent, 'PHP base binding model to '.$habtmRelation->model, true);
				
				$bindingExtendedModelPath = $this->_getExtendedModelPath($bindingModelName);
				$bindingExtendedModelContent = $this->_renderExtendedModel($bindingModelName, false);
				$this->_saveFile($bindingExtendedModelPath, $bindingExtendedModelContent, 'PHP extended binding model to '.$habtmRelation->model, false);
			}
		}

		new Garp_Model_Spawn_Php_ModelsIncluder($this->_model->id);
	}
	
	
	protected function _saveFile($path, $content, $label, $overwrite = false) {
		if (
			$overwrite ||
			!$overwrite && !file_exists($path)
		) {
			if (!file_put_contents($path, $content)) {
				throw new Exception("Could not generate {$label}.");
			}
		}
	}


	protected static function _getBaseModelPath($modelId) {
		return APPLICATION_PATH.self::_BASE_MODEL_PATH.$modelId.'.php';
	}
	
	
	protected static function _getExtendedModelPath($modelId) {
		return APPLICATION_PATH.self::_EXTENDED_MODEL_PATH.$modelId.'.php';
	}


	/**
	 * Extracts the public properties from an iteratable object
	 */
	protected function _objectToArray($obj) {
		$arr = array();
		$arrObj = is_object($obj) ? get_object_vars($obj) : $obj;

		foreach ($arrObj as $key => $val) {
			if (is_object($val)) {
				if (get_class($val) === 'Garp_Model_Spawn_Relations') {
					$val = $val->getRelations();
				} elseif (get_class($val) === 'Garp_Model_Spawn_Behaviors') {
					$val = $val->getBehaviors();
				} elseif (get_class($val) === 'Garp_Model_Spawn_Fields') {
					$val = $val->getFields();
				}

				$val = $this->_objectToArray($val);
			} elseif (is_array($val)) {
				$val = $this->_objectToArray($val);
			}
			$arr[$key] = $val;
		}

		return $arr;
	}


	protected function _renderBaseModel() {
		$lcModelId = strtolower($this->_model->id);
		
		$out = $this->_rl("<?php");
		$out.= $this->_rl('/* This file was generated by '.get_class().' */');

		$out.= $this->_rl("class Model_Base_{$this->_model->id} extends Garp_Model_Db {");

		/* Table */
		$out.= $this->_rl("protected \$_name = '{$lcModelId}';", 1, 2);

		/* This model's scheme, deducted from the combined Spawn model configurations. */
		$out.= $this->_rl("protected \$_configuration = " . Garp_Model_Spawn_Util::array2phpStatement($this->_objectToArray($this->_model)) .";", 1, 2);

		/* Joint view to include labels of singular relations */
		$out.= $this->_rl("protected \$_jointView = '{$lcModelId}_joint';", 1, 2);

		/* Default order */
		$orderStatement = (
			strpos($this->_model->order, ",") !== false &&
			strpos($this->_model->order, "(") === false) ?
			Garp_Model_Spawn_Util::array2phpStatement(explode(", ", $this->_model->order)) :
			"'".$this->_model->order."'";
		$out.= $this->_rl("protected \$_defaultOrder = {$orderStatement};", 1, 2);

		/* Relations */
		$relations = $this->_model->relations->getRelations();

		if (count($relations)) {
			/* Bindable */
			$i = 0;
			$out.= $this->_rl('protected $_bindable = array(', 1);

			$registeredBindableModels = array();
			$bindablesOutput = array();

			foreach ($relations as $relationName => $relation) {
				if (!in_array($relation->model, $registeredBindableModels)) {
					$registeredBindableModels[] = $relation->model;
					$bindablesOutput[] = $this->_rl("'Model_{$relation->model}'", 2, 0);
				}
			}
			$bindablesOutput = array_unique($bindablesOutput);
			sort($bindablesOutput);

			$out.= $this->_rl(implode(",\n", $bindablesOutput), 0);
			$out.= $this->_rl(');', 1, 2);
		

			/* ReferenceMap */
			$i = 0;
			$out.= $this->_rl('protected $_referenceMap = array(', 1);

			$referenceMapOutput = array();

			foreach ($relations as $relationName => $relation) {
				if (
					$relation->type === 'hasOne' ||
					$relation->type === 'belongsTo'
				) {
					$lcRelationModel = strtolower($relation->model);
					$referenceMapOutput[] =
						 $this->_rl("'{$relationName}' => array(", 2)
						.$this->_rl("'refTableClass' => 'Model_{$lcRelationModel}',", 3)
						.$this->_rl("'columns' => '{$relation->column}',", 3)
						.$this->_rl("'refColumns' => 'id'", 3)
						.$this->_rl(")", 2, 0)
					;
				}
			}
			
			$out.= $this->_rl(implode(",\n", $referenceMapOutput), 0);

			$out.= $this->_rl(');', 1, 3);
		}

		/* Default behaviors */
		$out.= $this->_rl("public function init() {", 1);
		$out.= $this->_rl('parent::init();', 2);
		$behaviors = $this->_model->behaviors->getBehaviors();
		foreach ($behaviors as $behaviorName => $behavior) {
			if (!in_array($behaviorName, $this->_behaviorsExcludedFromRendering)) {
				$behavior = $this->_filterBehaviorParams($behaviorName, $behavior);
				$behaviorOutput = $this->_renderBehavior($behavior);
				if ($behaviorOutput) {
					$out.= $this->_rl($behaviorOutput, 2);
				}
			}
		}
		$out.= $this->_rl('}', 1, 2);

		/*	Compose the method to fetch composite columns as a string in a MySql query
{'fraction_non_alphanumeric': 0.18115673026950777, 'fraction_numerical': 0.004900141064667013, 'mean_word_length': 4.305238282788499, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 201, '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': '1548152', 'n_tokens_mistral': 5132, 'n_tokens_neox': 4347, 'n_words': 867}