<commit_before><?php 

namespace Qik\Database;

use Qik\Utility\{Utility};

class DBResult
{
	private $objects = [];

	public function __construct($objects = null)
	{
		$this->objects = $objects;
	}

	public function GetObjects()
	{
		return $this->objects;
	}

	public static function CreateObjects($results, array $objects)
	{
		if (empty($results) || empty($objects))
			return array();

		$baseObjects = [];
		$columns = [];
		$primaryClass = Utility::GetBaseClassNameFromNamespace($objects[0]);
		foreach ($objects as $object)
		{
			$baseObjects[Utility::GetBaseClassNameFromNamespace($object)] = $object;
			$cols = $object->GetColumns();
			foreach ($cols as $key=>$val)
				$columns[$key] = $columns[$key] ?? Utility::GetBaseClassNameFromNamespace($object);
		}

		$return = [];
		foreach ($results as $result)
		{
			$objectified = [];
			foreach ($baseObjects as $key=>$object)
				$objectified[$key] = clone $object;

			foreach ($result as $key=>$val)
			{
				if (strpos($key, '_') > -1)
				{
					$reversed = strrev($key);
					$parts = explode('_', $reversed);
					$field = strrev(array_shift($parts));
					$class = strrev(implode('_', $parts));
					$subclass = '';

					//we're using __ to denote aliases of the same object so we need to handle them specially
					if (strpos($class, '__') > -1)
					{
						$parts = explode('__', $class);
						$class = $parts[0];
						$subclass = $parts[1];
					}

					if (!empty($subclass))
					{
						if (!isset($objectified[ucfirst($class)]->{$subclass}))
						{
							$name = get_class($objectified[ucfirst($class)]);
							$objectified[ucfirst($class)]->{$subclass} = new $name;
						}

						$objectified[ucfirst($class)]->{$subclass}->{$field} = $val;
					}
					elseif (isset($objectified[ucfirst($class)]))
						$objectified[ucfirst($class)]->{$field} = $val;
				}
				elseif (isset($columns[$key]) && !isset($objectified[$columns[$key]]->{$key}))
					$objectified[ucfirst($columns[$key])]->{$key} = $val;
			}

			$object = $objectified;
			if (isset($objectified[$primaryClass]))
			{
				$object = $objectified[$primaryClass];
				unset($objectified[$primaryClass]);
				foreach ($objectified as $key=>$o)
					$object->{strtolower($key)} = $o;
			}

			array_push($return, $object);
		}

		return new DBResult($return);
	}

	public static function Objectify()
	{

	}
<commit_msg>changing the data structure of dbresult objects within arrays<commit_after><?php 

namespace Qik\Database;

use Qik\Utility\{Utility};

class DBResult
{
	private $objects = [];

	public function __construct($objects = null)
	{
		$this->objects = $objects;
	}

	public function GetObjects()
	{
		return $this->objects;
	}

	public static function CreateObjects($results, array $objects)
	{
		if (empty($results) || empty($objects))
			return array();

		$baseObjects = [];
		$columns = [];
		$primaryClass = Utility::GetBaseClassNameFromNamespace($objects[0]);
		foreach ($objects as $object)
		{
			$baseObjects[Utility::GetBaseClassNameFromNamespace($object)] = $object;
			$cols = $object->GetColumns();
			foreach ($cols as $key=>$val)
				$columns[$key] = $columns[$key] ?? Utility::GetBaseClassNameFromNamespace($object);
		}

		$return = [];
		foreach ($results as $result)
		{
			$objectified = [];
			foreach ($baseObjects as $key=>$object)
				$objectified[$key] = clone $object;

			foreach ($result as $key=>$val)
			{
				if (strpos($key, '_') > -1)
				{
					$reversed = strrev($key);
					$parts = explode('_', $reversed);
					$field = strrev(array_shift($parts));
					$class = strrev(implode('_', $parts));
					$subclass = '';

					//we're using __ to denote aliases of the same object so we need to handle them specially
					if (strpos($class, '__') > -1)
					{
						$parts = explode('__', $class);
						$class = $parts[0];
						$subclass = $parts[1];
					}

					if (!empty($subclass))
					{
						if (!isset($objectified[ucfirst($class)]->{$subclass}))
						{
							$name = get_class($objectified[ucfirst($class)]);
							$objectified[ucfirst($class)]->{$subclass} = new $name;
						}

						$objectified[ucfirst($class)]->{$subclass}->{$field} = $val;
					}
					elseif (isset($objectified[ucfirst($class)]))
						$objectified[ucfirst($class)]->{$field} = $val;
				}
				elseif (isset($columns[$key]) && !isset($objectified[$columns[$key]]->{$key}))
					$objectified[ucfirst($columns[$key])]->{$key} = $val;
			}

			$object = $objectified;
			if (isset($objectified[$primaryClass]))
			{
				$object = $objectified[$primaryClass];
				unset($objectified[$primaryClass]);
				foreach ($objectified as $key=>$o)
					$object->{is_object($key) ? strtolower($key) : $key} = $o;
			}

			array_push($return, $object);
		}

		return new DBResult($return);
	}

	public static function Objectify()
	{

	}

{'fraction_non_alphanumeric': 0.18829410539470945, 'fraction_numerical': 0.002082899395959175, 'mean_word_length': 3.6666666666666665, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '631669', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1570, 'n_words': 308}