<commit_before><?php
namespace AMH\EntityManager\Repository;

use AMH\EntityManager\Repository\Mapper\AbstractMapper as Mapper;
use AMH\EntityManager\Entity\AbstractEntity as Entity;
use AMH\EntityManager\Repository\Mapper\SelectStatement as SelSttm;

/**
Manages loaded from mappers entities.

@author <NAME> <EMAIL>
*/
class IdentityMap extends Mapper{
	const FLUSH_ACTION_NONE=0;
	const FLUSH_ACTION_INSERT=1;
	const FLUSH_ACTION_UPDATE=2;
	const FLUSH_ACTION_REMOVE=3;
	
	/**
	@var array Of Entities, their flush action and loaded attribute.
	*/
	protected $entities=array();
	/**
	@return array of Entity.
	*/
	public function find(SelSttm $select){
		return $this->findEntities($select);
	}
	/**
	@return array of Entity.
	*/
	protected function findEntities(SelSttm $select){
		$found=array();
		
		
		
		return $found;
	}
	
	protected function loadEntityData($id){}
	
	public function load(Entity $e){}
	
	public function add(Entity $e){
		$this->addToMap($e);
	}
	
	public function update(Entity $e){}
	
	public function remove(Entity $e){
		if(($ind=$this->has($e))>=0){
			unset($this->entities[$ind]);
		}
	}
	/**
	Checks if entity is stored.
	
	@param int|Entity Entity object or ID.
	
	@return int Idnex or -1 if not found.
	*/
	public function has($e){
		if(!($e instanceof Entity)){
			$e=(int)$e;
		}
		foreach($this->entities as $key=>$data){
			if((($e instanceof Entity) && $data['entity']->id()==$e->id()) || (gettype($e)=='int' && $e==$data['entity']->id())){
				return $key;
			}
		}
		
		return -1;
	}
	/**
	Adds entity to object's storage.
	
	@param Entity $e Entity.
	@param int Flush action.
	
	@throws \InvalidArgumentException if wrong Flush Action given.
	
	@return bool True if saved, FALSE if entity is already saved.
	*/
	public function addToMap(Entity $e, $f_action=self::FLUSH_ACTION_NONE){
		if($this->has($e) == -1){
			switch($f_action){
			case self::FLUSH_ACTION_NONE:
			case self::FLUSH_ACTION_INSERT:
			case self::FLUSH_ACTION_UPDATE:
			case self::FLUSH_ACTION_REMOVE:
				$this->entities[]=array(
					'entity'=>$e,
					'action'=>(int)$f_action,
					'loaded'=>TRUE,
				);
				break;
			default:
				throw new \InvalidArgumentException('Invalid flush action given');
				break;
			}
			
			return TRUE;
		}
		
		return FALSE;
	}
	/**
	Adds multiple entities to object's storage.
	
	@param array $es Of Entity.
	@param int $f_action Flush action for all entities.
	*/
	public function addAllToMap(array $es, $f_action=self::FLUSH_ACTION_NONE){
		foreach($es as $e){
			$this->addToMap($e,$f_action);
		}
	}
	/**
	Checks if entity was loaded.
	
	@param Entity
	
	@return bool
	*/
	public function isEntityLoaded(Entity $e){
		$ind=$this->has($e);
		if($ind>=0 && $this->entities[$ind]['loaded'])
			return TRUE;
					
		return FALSE;
	}
	/**
	@return int count of entities in identity map.
	*/
	public function count(){
		return count($this->entities);
	}
	/**
	Finds relative Entity, of not founds returns Entity obj only with ID.
	
	@param int ID.
	
	@return Entity
	
	@throws \RuntimeException
	*/
	public function findRelative($id){
		$id=(int)$id;
		if(($ind=$this->has($id))>=0){
			return $this->entities[$ind]['entity'];
		}
		else{
			if(!$this->getHydrator()){
				throw \RuntimeException('No hydrator provided');
			}
			$e=$this->getHydrator()->create();
			$e->setId($id);
			return $e;
		}
	}
	/**
	Marks entity to be updated.
	
	@param int|Entity ID or Entity obj.
	*/
	public function dirty($e){
		if(($int=$this->has($e))>=0){
			$this->entities[$ind]['action']=self::FLUSH_ACTION_UPDATE;
		}
	}
	/**
	@return int flush action
	*/
	public function flushAction(Entity $e){
		if(($ind=$this->has($e))>=0){
			return $this->entities[$ind]['action'];
		}
		return FALSE;
	}
	/**
	@return array List of entities to add, update or remove.
	*/
	public function unitOfWork(){<commit_msg>entity is being mark as dirty only if it was alredy inserted<commit_after><?php
namespace AMH\EntityManager\Repository;

use AMH\EntityManager\Repository\Mapper\AbstractMapper as Mapper;
use AMH\EntityManager\Entity\AbstractEntity as Entity;
use AMH\EntityManager\Repository\Mapper\SelectStatement as SelSttm;

/**
Manages loaded from mappers entities.

@author <NAME> <EMAIL>
*/
class IdentityMap extends Mapper{
	const FLUSH_ACTION_NONE=0;
	const FLUSH_ACTION_INSERT=1;
	const FLUSH_ACTION_UPDATE=2;
	const FLUSH_ACTION_REMOVE=3;
	
	/**
	@var array Of Entities, their flush action and loaded attribute.
	*/
	protected $entities=array();
	/**
	@return array of Entity.
	*/
	public function find(SelSttm $select){
		return $this->findEntities($select);
	}
	/**
	@return array of Entity.
	*/
	protected function findEntities(SelSttm $select){
		$found=array();
		
		
		
		return $found;
	}
	
	protected function loadEntityData($id){}
	
	public function load(Entity $e){}
	
	public function add(Entity $e){
		$this->addToMap($e);
	}
	
	public function update(Entity $e){}
	
	public function remove(Entity $e){
		if(($ind=$this->has($e))>=0){
			unset($this->entities[$ind]);
		}
	}
	/**
	Checks if entity is stored.
	
	@param int|Entity Entity object or ID.
	
	@return int Idnex or -1 if not found.
	*/
	public function has($e){
		if(!($e instanceof Entity)){
			$e=(int)$e;
		}
		foreach($this->entities as $key=>$data){
			if((($e instanceof Entity) && $data['entity']->id()==$e->id()) || (gettype($e)=='int' && $e==$data['entity']->id())){
				return $key;
			}
		}
		
		return -1;
	}
	/**
	Adds entity to object's storage.
	
	@param Entity $e Entity.
	@param int Flush action.
	
	@throws \InvalidArgumentException if wrong Flush Action given.
	
	@return bool True if saved, FALSE if entity is already saved.
	*/
	public function addToMap(Entity $e, $f_action=self::FLUSH_ACTION_NONE){
		if($this->has($e) == -1){
			switch($f_action){
			case self::FLUSH_ACTION_NONE:
			case self::FLUSH_ACTION_INSERT:
			case self::FLUSH_ACTION_UPDATE:
			case self::FLUSH_ACTION_REMOVE:
				$this->entities[]=array(
					'entity'=>$e,
					'action'=>(int)$f_action,
					'loaded'=>TRUE,
				);
				break;
			default:
				throw new \InvalidArgumentException('Invalid flush action given');
				break;
			}
			
			return TRUE;
		}
		
		return FALSE;
	}
	/**
	Adds multiple entities to object's storage.
	
	@param array $es Of Entity.
	@param int $f_action Flush action for all entities.
	*/
	public function addAllToMap(array $es, $f_action=self::FLUSH_ACTION_NONE){
		foreach($es as $e){
			$this->addToMap($e,$f_action);
		}
	}
	/**
	Checks if entity was loaded.
	
	@param Entity
	
	@return bool
	*/
	public function isEntityLoaded(Entity $e){
		$ind=$this->has($e);
		if($ind>=0 && $this->entities[$ind]['loaded'])
			return TRUE;
					
		return FALSE;
	}
	/**
	@return int count of entities in identity map.
	*/
	public function count(){
		return count($this->entities);
	}
	/**
	Finds relative Entity, of not founds returns Entity obj only with ID.
	
	@param int ID.
	
	@return Entity
	
	@throws \RuntimeException
	*/
	public function findRelative($id){
		$id=(int)$id;
		if(($ind=$this->has($id))>=0){
			return $this->entities[$ind]['entity'];
		}
		else{
			if(!$this->getHydrator()){
				throw \RuntimeException('No hydrator provided');
			}
			$e=$this->getHydrator()->create();
			$e->setId($id);
			return $e;
		}
	}
	/**
	Marks entity to be updated.
	
	@param int|Entity ID or Entity obj.
	*/
	public function dirty($e){
		if(($ind=$this->has($e))>=0 && $this->entities[$ind]['entity']->id()){
			$this->entities[$ind]['action']=self::FLUSH_ACTION_UPDATE;
		}
	}
	/**
	@return int flush action
	*/
	public function flushAction(Entity $e){
		if(($ind=$this->has($e))>=0){
			return $this->entities[$ind]['action'];
		}
		return FALSE;
	}
	/**
	@return array List of entities to add, update or remove.
	*/
	public function unitOfWork(){
{'fraction_non_alphanumeric': 0.16255144032921812, 'fraction_numerical': 0.0030864197530864196, 'mean_word_length': 3.8424657534246576, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 75, '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': '2129848', 'n_tokens_mistral': 2997, 'n_tokens_neox': 2677, 'n_words': 675}