<commit_before><?php
	/**
	 * Class ModelsUtils
	 *
	 * @package EchoIt\JsonApi\Utils
	 * @author  <NAME> <<EMAIL>>
	 */
	
	namespace EchoIt\JsonApi\Utils;
	
	use EchoIt\JsonApi\Database\Eloquent\Model;
	use EchoIt\JsonApi\Error;
	use EchoIt\JsonApi\Exception;
	use Illuminate\Http\Response;
	use Illuminate\Support\Collection;
	
	class ModelsUtils {
		
		/**
		 * Returns the models from a relationship. Will always return as array.
		 *
		 * @param  Model $model
		 * @param  string $relationKey
		 *
		 * @return array|\Illuminate\Database\Eloquent\Collection
		 * @throws Exception
		 */
		public static function getModelsForRelation(Model $model, $relationKey) {
			if (method_exists($model, $relationKey) === false) {
				throw new Exception([
						new Error ('Relation "' . $relationKey . '" does not exist in model',
							Error::RELATION_DOESNT_EXISTS_IN_MODEL, Response::HTTP_UNPROCESSABLE_ENTITY)
					]);
			}
			$relationModels = $model->{$relationKey};
			if (is_null($relationModels)) {
				return null;
			}
			
			if ( ! $relationModels instanceof Collection) {
				return [$relationModels];
			}
			
			return $relationModels;
		}
		
		/**
		 * Iterate through result set to fetch the requested resources to include.
		 *
		 * @param Model $models
		 *
		 * @return array
		 */
		public static function getIncludedModels($models) {
			$modelsCollection = new Collection();
			$models           = $models instanceof Collection ? $models : [$models];
			
			/** @var Model $model */
			foreach ($models as $model) {
				$exposedRelations = $model->exposedRelations();
				
				foreach ($exposedRelations as $relationName) {
					$value = static::getModelsForRelation($model, $relationName);
					
					if (is_null($value)) {
						continue;
					}
					
					//Each one of the models relations
					/* @var \EchoIt\JsonApi\Database\Eloquent\Model $obj */
					foreach ($value as $obj) {
						// Check whether the object is already included in the response on it's ID
						$duplicate = false;
						$items     = $modelsCollection->where($obj->getPrimaryKey(), $obj->getKey());
						
						if (count($items) > 0) {
							foreach ($items as $item) {
								/** @var $item Model */
								if ($item->getResourceType() === $obj->getResourceType()) {
									$duplicate = true;
									break;
								}
							}
							if ($duplicate) {
								continue;
							}
						}
						
						//add type property
						$attributes = $obj->getAttributes();
						
						$obj->setRawAttributes($attributes);
						
						$modelsCollection->push($obj);
					}
				}
			}
			
			return $modelsCollection->toArray();
		}
	}<commit_msg>Finished implementation of getIncludedModels<commit_after><?php
	/**
	 * Class ModelsUtils
	 *
	 * @package EchoIt\JsonApi\Utils
	 * @author  <NAME> <<EMAIL>>
	 */
	
	namespace EchoIt\JsonApi\Utils;
	
	use EchoIt\JsonApi\Database\Eloquent\Model;
	use EchoIt\JsonApi\Error;
	use EchoIt\JsonApi\Exception;
	use Illuminate\Http\Response;
	use Illuminate\Support\Collection;
	
	class ModelsUtils {
		
		/**
		 * Returns the models from a relationship. Will always return as array.
		 *
		 * @param  Model $model
		 * @param  string $relationKey
		 *
		 * @return array|\Illuminate\Database\Eloquent\Collection
		 * @throws Exception
		 */
		public static function getModelsForRelation(Model $model, $relationKey) {
			if (method_exists($model, $relationKey) === false) {
				throw new Exception([
						new Error ('Relation "' . $relationKey . '" does not exist in model',
							Error::RELATION_DOESNT_EXISTS_IN_MODEL, Response::HTTP_UNPROCESSABLE_ENTITY)
					]);
			}
			$relationModels = $model->{$relationKey};
			if (is_null($relationModels)) {
				return null;
			}
			
			if ( ! $relationModels instanceof Collection) {
				return [$relationModels];
			}
			
			return $relationModels;
		}
		
		/**
		 * Iterate through result set to fetch the requested resources to include.
		 *
		 * @param $models
		 *
		 * @return array
		 */
		public static function getIncludedModels($models) {
			$includedModels = new Collection();
			if ($models instanceof Collection === false) {
				if (is_array($models)) {
					$models = new Collection($models);
				}
				else {
					$models = new Collection([$models]);
				}
			}
			
			/** @var Model $model */
			foreach ($models as $model) {
				$exposedRelations = $model->exposedRelations();
				
				foreach ($exposedRelations as $relationName) {
					$modelsForRelation = static::getModelsForRelation($model, $relationName);
					
					if (is_null($modelsForRelation)) {
						continue;
					}
					
					//Each one of the models relations
					foreach ($modelsForRelation as $modelForRelation) {
						if ($modelForRelation instanceof Model === true) {
							// Check whether the object is already included in the response on it's ID
							$duplicate  = false;
							$key = $modelForRelation->getKey();
							$primaryKey = $modelForRelation->getPrimaryKey();
							$items      = $includedModels->where($primaryKey, $key);
							
							if (count($items) > 0) {
								foreach ($items as $item) {
									/** @var $item Model */
									if ($item->getResourceType() === $modelForRelation->getResourceType()) {
										$duplicate = true;
										break;
									}
								}
								if ($duplicate) {
									continue;
								}
							}
							
							//add type property
							$attributes = $modelForRelation->getAttributes();
							
							$modelForRelation->setRawAttributes($attributes);
							
							$includedModels->push($modelForRelation);
						}
						else {
							throw new \InvalidArgumentException("Model " . get_class($modelForRelation) . " is not a JSON API model");
						}
					}
				}
			}
			
			return $includedModels->toArray();
		}
	}
{'fraction_non_alphanumeric': 0.12588991144295886, 'fraction_numerical': 0.0003472825143254037, 'mean_word_length': 2.5754189944134076, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, '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': '1775131', 'n_tokens_mistral': 2365, 'n_tokens_neox': 1644, 'n_words': 403}