<commit_before><?php
namespace Objection\Mapper;


use Objection\LiteObject;
use Objection\Enum\VarType;
use Objection\Enum\SetupFields;
use Objection\Mapper\Base\Values\IValuesProcessorContainer;
use Objection\Setup\Container;
use Objection\Mapper\Base\IMapperCollection;
use Objection\Mapper\Base\IObjectToTargetBuilder;
use Objection\Mapper\Base\Extensions\IMappedObject;
use Objection\Mapper\Loaders\MapperLoadHelpers;
use Objection\Mapper\Loaders\LoadHelpersProcessor;
use Objection\Exceptions\LiteObjectException;


class ObjectMapper
{
	use \Objection\TStaticClass;
	
	
	/**
	 * @param LiteObject|IMappedObject $object
	 * @param IValuesProcessorContainer $container
	 * @return array
	 */
	private static function getFields(LiteObject $object, IValuesProcessorContainer $container)
	{
		if ($object instanceof IMappedObject)
		{
			$data = $object->getObjectData();
		}
		else
		{
			$data = $object->toArray();
		}
		
		$className = get_class($object);
		
		if ($container->has($className))
		{
			$data = $container->get($className)->toRawValues($data);
		}
		
		return $data;
	}
	
	/**
	 * @param LiteObject $object
	 * @param array $data
	 * @param IValuesProcessorContainer $container
	 * @return LiteObject
	 */
	private static function setFields(LiteObject $object, $data, IValuesProcessorContainer $container)
	{
		$className = get_class($object);
		
		if ($container->has($className))
		{
			$data = $container->get($className)->toObjectValues($data);
		}
		
		if ($object instanceof IMappedObject)
		{
			$object->setObjectData($data);
		}
		else
		{
			$data = array_intersect_key($data, array_flip($object->getPropertyNames()));
			$object->fromArray($data);
		}
		
		return $object;
	}
	
	
	/**
	 * @param LiteObject $object
	 * @param IMapperCollection $collection
	 * @param \stdClass|array|null $value
	 * @param MapperLoadHelpers $loaders
	 * @param IValuesProcessorContainer $container
	 * @return mixed
	 */
	private static function getObjectFromData(LiteObject $object, 
		IMapperCollection $collection, $value, MapperLoadHelpers $loaders, 
		IValuesProcessorContainer $container)
	{
		if (is_string($value))
			$value = json_decode($value);
		
		$data = [];
		$className = get_class($object);
		$fieldMapper = $collection->getOrDefault($object);
		$setup = Container::instance()->get(get_class($object));
	
		$processor = new LoadHelpersProcessor();
		$processor->setLoadersContainer($loaders);
		
		$value = $processor->preMapProcess($className, $value);
		
		foreach ($value as $dataName => $dataValue)
		{
			$fieldName = $fieldMapper->mapToObjectField($dataName, get_class($object));
			
			if (!isset($setup[$fieldName]))
			{
				throw new LiteObjectException("Property $fieldName is not defined in " . get_class($object));
			}
			
			$fieldSetup = $setup[$fieldName];
			
			if (isset($fieldSetup[SetupFields::INSTANCE_TYPE]))
			{
				$instanceType = $fieldSetup[SetupFields::INSTANCE_TYPE];
				
				if ($fieldSetup[SetupFields::TYPE] == VarType::INSTANCE)
				{
					$fieldValue = new $instanceType;
					self::getObjectFromData($fieldValue, $collection, $dataValue, $loaders, $container);
				}<commit_msg>Instead of throwing exception when mapping unexisting field, just ignore the field<commit_after><?php
namespace Objection\Mapper;


use Objection\LiteObject;
use Objection\Enum\VarType;
use Objection\Enum\SetupFields;
use Objection\Mapper\Base\Values\IValuesProcessorContainer;
use Objection\Setup\Container;
use Objection\Mapper\Base\IMapperCollection;
use Objection\Mapper\Base\IObjectToTargetBuilder;
use Objection\Mapper\Base\Extensions\IMappedObject;
use Objection\Mapper\Loaders\MapperLoadHelpers;
use Objection\Mapper\Loaders\LoadHelpersProcessor;
use Objection\Exceptions\LiteObjectException;


class ObjectMapper
{
	use \Objection\TStaticClass;
	
	
	/**
	 * @param LiteObject|IMappedObject $object
	 * @param IValuesProcessorContainer $container
	 * @return array
	 */
	private static function getFields(LiteObject $object, IValuesProcessorContainer $container)
	{
		if ($object instanceof IMappedObject)
		{
			$data = $object->getObjectData();
		}
		else
		{
			$data = $object->toArray();
		}
		
		$className = get_class($object);
		
		if ($container->has($className))
		{
			$data = $container->get($className)->toRawValues($data);
		}
		
		return $data;
	}
	
	/**
	 * @param LiteObject $object
	 * @param array $data
	 * @param IValuesProcessorContainer $container
	 * @return LiteObject
	 */
	private static function setFields(LiteObject $object, $data, IValuesProcessorContainer $container)
	{
		$className = get_class($object);
		
		if ($container->has($className))
		{
			$data = $container->get($className)->toObjectValues($data);
		}
		
		if ($object instanceof IMappedObject)
		{
			$object->setObjectData($data);
		}
		else
		{
			$data = array_intersect_key($data, array_flip($object->getPropertyNames()));
			$object->fromArray($data);
		}
		
		return $object;
	}
	
	
	/**
	 * @param LiteObject $object
	 * @param IMapperCollection $collection
	 * @param \stdClass|array|null $value
	 * @param MapperLoadHelpers $loaders
	 * @param IValuesProcessorContainer $container
	 * @return mixed
	 */
	private static function getObjectFromData(LiteObject $object, 
		IMapperCollection $collection, $value, MapperLoadHelpers $loaders, 
		IValuesProcessorContainer $container)
	{
		if (is_string($value))
			$value = json_decode($value);
		
		$data = [];
		$className = get_class($object);
		$fieldMapper = $collection->getOrDefault($object);
		$setup = Container::instance()->get(get_class($object));
	
		$processor = new LoadHelpersProcessor();
		$processor->setLoadersContainer($loaders);
		
		$value = $processor->preMapProcess($className, $value);
		
		foreach ($value as $dataName => $dataValue)
		{
			$fieldName = $fieldMapper->mapToObjectField($dataName, get_class($object));
			
			if (!$fieldName || !isset($setup[$fieldName]))
			{
				continue;
			}
			
			$fieldSetup = $setup[$fieldName];
			
			if (isset($fieldSetup[SetupFields::INSTANCE_TYPE]))
			{
				$instanceType = $fieldSetup[SetupFields::INSTANCE_TYPE];
				
				if ($fieldSetup[SetupFields::TYPE] == VarType::INSTANCE)
				{
					$fieldValue = new $instanceType;
					self::getObjectFromData($fieldValue, $collection, $dataValue, $loaders, $container);
				}
{'fraction_non_alphanumeric': 0.1334604385128694, 'fraction_numerical': 0.0, 'mean_word_length': 4.6968325791855206, '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': '2476644', 'n_tokens_mistral': 2179, 'n_tokens_neox': 1892, 'n_words': 366}