<commit_before><?php

/**
 * This file is part of the Nextras\ORM library.
 *
 * @license    MIT
 * @link       https://github.com/nextras/orm
 * @author     <NAME>
 */

namespace Nextras\Orm\Relationships;

use Nette\Object;
use Nextras\Orm\Entity\Collection\ArrayCollection;
use Nextras\Orm\Entity\Collection\ICollection;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Entity\IPropertyInjection;
use Nextras\Orm\Entity\Reflection\PropertyMetadata;
use Nextras\Orm\NotSupportedException;
use Nextras\Orm\Repository\IRepository;
use Nextras\Orm\InvalidStateException;


abstract class HasMany extends Object implements IPropertyInjection, IRelationshipCollection
{
	/** @var IEntity */
	protected $parent;

	/** @var PropertyMetadata */
	protected $metadata;

	/** @var mixed */
	protected $injectedValue;

	/** @var ICollection */
	protected $collection;

	/** @var array */
	protected $toAdd;

	/** @var array */
	protected $toRemove;

	/** @var IRepository */
	protected $targetRepository;

	/** @var bool */
	protected $updatingReverseRelationship = FALSE;


	public function __construct(IEntity $parent, PropertyMetadata $metadata, $value)
	{
		$this->parent = $parent;
		$this->metadata = $metadata;
		$this->injectedValue = $value ? unserialize($value) : NULL;
	}


	public function setParent(IEntity $parent)
	{
		$this->parent = $parent;
	}


	public function add($entity)
	{
		if ($this->updatingReverseRelationship) {
			return NULL;
		}

		$entity = $this->createEntity($entity);
		$entityHash = spl_object_hash($entity);

		if (isset($this->toRemove[$entityHash])) {
			unset($this->toRemove[$entityHash]);
		} else {
			$this->toAdd[$entityHash] = $entity;
		}

		$this->updateRelationshipAdd($entity);
		$this->collection = NULL;
		return $entity;
	}


	public function remove($entity)
	{
		if ($this->updatingReverseRelationship) {
			return NULL;
		}

		$entity = $this->createEntity($entity);
		$entityHash = spl_object_hash($entity);

		if (isset($this->toAdd[$entityHash])) {
			unset($this->toAdd[$entityHash]);
		} else {
			$this->toRemove[$entityHash] = $entity;
		}

		$this->updateRelationshipRemove($entity);
		$this->collection = NULL;
		return $entity;
	}


	public function has($entity)
	{
		$entity = $this->createEntity($entity, FALSE);
		if (!$entity) {
			return FALSE;
		}

		$entityHash = spl_object_hash($entity);
		if (isset($this->toAdd[$entityHash])) {
			return TRUE;

		} elseif (isset($this->toRemove[$entityHash])) {
			return FALSE;

		} else {
			return (bool) $this->getCollection()->getById($entity->id);
		}
	}


	public function set(array $data)
	{
		foreach ($this->getCollection() as $entity) {
			$this->remove($entity);
		}

		foreach ($data as $entity) {
			$this->add($entity);
		}

		return $this;
	}


	public function get()
	{
		return $this->getCollection()->toCollection();
	}


	public function count($collectionName = NULL)
	{
		$collection = $this->collection === NULL && !$this->toAdd && !$this->toRemove ? $this->getCachedCollection($collectionName) : $this->getCollection();
		if ($collection->getRelationshipMapper()) {
			return $collection->getRelationshipMapper()->getIteratorCount($this->parent, $collection);
		} else {
			return count($collection);
		}
	}


	public function getIterator($collectionName = NULL)
	{
		$collection = $this->collection === NULL && !$this->toAdd && !$this->toRemove ? $this->getCachedCollection($collectionName) : $this->getCollection();
		if ($collection->getRelationshipMapper()) {
			return $collection->getRelationshipMapper()->getIterator($this->parent, $collection);
		} else {
			return $collection->getIterator();
		}
	}


	public function setInjectedValue($values)
	{
		throw new NotSupportedException;
	}<commit_msg>[relationship] implemented setInjectedValue for HasMany relationship<commit_after><?php

/**
 * This file is part of the Nextras\ORM library.
 *
 * @license    MIT
 * @link       https://github.com/nextras/orm
 * @author     <NAME>
 */

namespace Nextras\Orm\Relationships;

use Nette\Object;
use Nextras\Orm\Entity\Collection\ArrayCollection;
use Nextras\Orm\Entity\Collection\ICollection;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Entity\IPropertyInjection;
use Nextras\Orm\Entity\Reflection\PropertyMetadata;
use Nextras\Orm\Repository\IRepository;
use Nextras\Orm\InvalidStateException;


abstract class HasMany extends Object implements IPropertyInjection, IRelationshipCollection
{
	/** @var IEntity */
	protected $parent;

	/** @var PropertyMetadata */
	protected $metadata;

	/** @var mixed */
	protected $injectedValue;

	/** @var ICollection */
	protected $collection;

	/** @var array */
	protected $toAdd;

	/** @var array */
	protected $toRemove;

	/** @var IRepository */
	protected $targetRepository;

	/** @var bool */
	protected $updatingReverseRelationship = FALSE;


	public function __construct(IEntity $parent, PropertyMetadata $metadata, $value)
	{
		$this->parent = $parent;
		$this->metadata = $metadata;
		$this->injectedValue = $value ? unserialize($value) : NULL;
	}


	public function setParent(IEntity $parent)
	{
		$this->parent = $parent;
	}


	public function add($entity)
	{
		if ($this->updatingReverseRelationship) {
			return NULL;
		}

		$entity = $this->createEntity($entity);
		$entityHash = spl_object_hash($entity);

		if (isset($this->toRemove[$entityHash])) {
			unset($this->toRemove[$entityHash]);
		} else {
			$this->toAdd[$entityHash] = $entity;
		}

		$this->updateRelationshipAdd($entity);
		$this->collection = NULL;
		return $entity;
	}


	public function remove($entity)
	{
		if ($this->updatingReverseRelationship) {
			return NULL;
		}

		$entity = $this->createEntity($entity);
		$entityHash = spl_object_hash($entity);

		if (isset($this->toAdd[$entityHash])) {
			unset($this->toAdd[$entityHash]);
		} else {
			$this->toRemove[$entityHash] = $entity;
		}

		$this->updateRelationshipRemove($entity);
		$this->collection = NULL;
		return $entity;
	}


	public function has($entity)
	{
		$entity = $this->createEntity($entity, FALSE);
		if (!$entity) {
			return FALSE;
		}

		$entityHash = spl_object_hash($entity);
		if (isset($this->toAdd[$entityHash])) {
			return TRUE;

		} elseif (isset($this->toRemove[$entityHash])) {
			return FALSE;

		} else {
			return (bool) $this->getCollection()->getById($entity->id);
		}
	}


	public function set(array $data)
	{
		foreach ($this->getCollection() as $entity) {
			$this->remove($entity);
		}

		foreach ($data as $entity) {
			$this->add($entity);
		}

		return $this;
	}


	public function get()
	{
		return $this->getCollection()->toCollection();
	}


	public function count($collectionName = NULL)
	{
		$collection = $this->collection === NULL && !$this->toAdd && !$this->toRemove ? $this->getCachedCollection($collectionName) : $this->getCollection();
		if ($collection->getRelationshipMapper()) {
			return $collection->getRelationshipMapper()->getIteratorCount($this->parent, $collection);
		} else {
			return count($collection);
		}
	}


	public function getIterator($collectionName = NULL)
	{
		$collection = $this->collection === NULL && !$this->toAdd && !$this->toRemove ? $this->getCachedCollection($collectionName) : $this->getCollection();
		if ($collection->getRelationshipMapper()) {
			return $collection->getRelationshipMapper()->getIterator($this->parent, $collection);
		} else {
			return $collection->getIterator();
		}
	}


	public function setInjectedValue($values)
	{
		$this->set($values);
	}
{'fraction_non_alphanumeric': 0.15987670865719647, 'fraction_numerical': 0.0, 'mean_word_length': 4.427636363636363, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 102, 'https://': 2, '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': '791447', 'n_tokens_mistral': 2607, 'n_tokens_neox': 2352, 'n_words': 461}