<commit_before><?php

namespace Amiss;

abstract class Mapper
{
	public $unnamedPropertyMapper;
	
	public $typeHandlers = array();
	
	private $typeHandlerMap = array();
	
	abstract function getMeta($class);
	
	function createObject($meta, $row, $args)
	{
		if ($args) {
			$rc = new \ReflectionClass($meta->class);
			$object = $rc->newInstanceArgs($args);
		}
		else {
			$cname = $meta->class;
			$object = new $cname;
		}
		
		$defaultType = $meta->getDefaultFieldType();
		
		foreach ($meta->getFields() as $prop=>$field) {
			// TODO: getter and setter support
			$value = $row[$field['name']];
			
			$type = $field['type'] ?: $defaultType;
			
			if ($type) {
				if (!isset($this->typeHandlerMap[$type])) {
					$this->typeHandlerMap[$type] = $this->determineTypeHandler($type);
				}
				if ($this->typeHandlerMap[$type]) {
					$value = $this->typeHandlerMap[$type]->handleValueFromDb($value, $object, $field['name']);
				}
			}
			
			if (!isset($field['setter']))
				$object->{$prop} = $value;
			else
				call_user_func(array($object, $field['setter']), $value);
		}
		
		return $object;
	}
	
	function exportRow($meta, $object)
	{
		$row = array();
		
		$defaultType = $meta->getDefaultFieldType();
		
		foreach ($meta->getFields() as $prop=>$field) {
			if (!isset($field['getter']))
				$value = $object->$prop;
			else
				$value = call_user_func(array($object, $field['getter']));
			
			$type = $field['type'] ?: $defaultType;
			
			if ($type) {
				if (!isset($this->typeHandlerMap[$type])) {
					$this->typeHandlerMap[$type] = $this->determineTypeHandler($type);
				}
				if ($this->typeHandlerMap[$type]) {
					$value = $this->typeHandlerMap[$type]->prepareValueForDb($value, $object, $field['name']);
				}
			}
			
			$row[$field['name']] = $value;
		}
		
		return $row;
	}
	
	function setPrimary($meta, $object, $id)
	{
		$object->{$meta->primary} = $id;
	}
	
	protected function determineTypeHandler($type)
	{
		// this splits off any extra crap that you may have defined
		// in the field's definition, i.e. "varchar(80) not null etc etc"
		// becomes "varchar"
		$x = preg_split('@[ \(]@', $type, 2);
		$id = strtolower($x[0]);
		
		return isset($this->typeHandlers[$id]) ? $this->typeHandlers[$id] : false;
	}
	
	protected function getDefaultTable($class)
	{
		$table = $class;
		
		if ($pos = strrpos($table, '\\')) $table = substr($table, $pos+1);
		
		$table = trim(preg_replace_callback('/[A-Z]/', function($match) {
			return "_".strtolower($match[0]);
		}, $table), '_');

		return $table;
	}
	
	protected function resolveUnnamedFields($fields)<commit_msg>added setter support for primary<commit_after><?php

namespace Amiss;

abstract class Mapper
{
	public $unnamedPropertyMapper;
	
	public $typeHandlers = array();
	
	private $typeHandlerMap = array();
	
	abstract function getMeta($class);
	
	function createObject($meta, $row, $args)
	{
		if ($args) {
			$rc = new \ReflectionClass($meta->class);
			$object = $rc->newInstanceArgs($args);
		}
		else {
			$cname = $meta->class;
			$object = new $cname;
		}
		
		$defaultType = $meta->getDefaultFieldType();
		
		foreach ($meta->getFields() as $prop=>$field) {
			// TODO: getter and setter support
			$value = $row[$field['name']];
			
			$type = $field['type'] ?: $defaultType;
			
			if ($type) {
				if (!isset($this->typeHandlerMap[$type])) {
					$this->typeHandlerMap[$type] = $this->determineTypeHandler($type);
				}
				if ($this->typeHandlerMap[$type]) {
					$value = $this->typeHandlerMap[$type]->handleValueFromDb($value, $object, $field['name']);
				}
			}
			
			if (!isset($field['setter']))
				$object->{$prop} = $value;
			else
				call_user_func(array($object, $field['setter']), $value);
		}
		
		return $object;
	}
	
	function exportRow($meta, $object)
	{
		$row = array();
		
		$defaultType = $meta->getDefaultFieldType();
		
		foreach ($meta->getFields() as $prop=>$field) {
			if (!isset($field['getter']))
				$value = $object->$prop;
			else
				$value = call_user_func(array($object, $field['getter']));
			
			$type = $field['type'] ?: $defaultType;
			
			if ($type) {
				if (!isset($this->typeHandlerMap[$type])) {
					$this->typeHandlerMap[$type] = $this->determineTypeHandler($type);
				}
				if ($this->typeHandlerMap[$type]) {
					$value = $this->typeHandlerMap[$type]->prepareValueForDb($value, $object, $field['name']);
				}
			}
			
			$row[$field['name']] = $value;
		}
		
		return $row;
	}
	
	function setPrimary($meta, $object, $id)
	{
		$field = $meta->getField($meta->primary);
		if (!isset($field['setter'])) {
			$object->{$meta->primary} = $id;
		}
		else {
			call_user_func(array($object, $field['setter']), $id);
		}
	}
	
	protected function determineTypeHandler($type)
	{
		// this splits off any extra crap that you may have defined
		// in the field's definition, i.e. "varchar(80) not null etc etc"
		// becomes "varchar"
		$x = preg_split('@[ \(]@', $type, 2);
		$id = strtolower($x[0]);
		
		return isset($this->typeHandlers[$id]) ? $this->typeHandlers[$id] : false;
	}
	
	protected function getDefaultTable($class)
	{
		$table = $class;
		
		if ($pos = strrpos($table, '\\')) $table = substr($table, $pos+1);
		
		$table = trim(preg_replace_callback('/[A-Z]/', function($match) {
			return "_".strtolower($match[0]);
		}, $table), '_');

		return $table;
	}
	
	protected function resolveUnnamedFields($fields)
{'fraction_non_alphanumeric': 0.19918017514440098, 'fraction_numerical': 0.0022358859698155395, 'mean_word_length': 3.7253521126760565, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, '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': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4389764', 'n_tokens_mistral': 2193, 'n_tokens_neox': 1833, 'n_words': 359}