<?php

namespace App\EntityClone;


/**
 *
 * @author dcs
 *
 */
abstract class BaseAbstract
{

	/**
	 * id of entity that will be clone
	 * @var int
	 */
	protected $id;

	/**
	 * The clone entity
	 * @var \App\Entity\GeneralAbstract
	 */
	protected \App\Entity\GeneralAbstract $entityNew;

	/**
	 *
	 * @param int $id
	 * @var \App\Entity\GeneralAbstract $entityNew
	 */
	public function __construct($id, \App\Entity\GeneralAbstract $entityNew)
	{
		$this->id = $id;
		$this->entityNew = $entityNew;

	}

	/**
	 *  returns the entity with attached fields
	 *  this methos is available only for `insert` operation
	 * @throws \Exception
	 */
	public function populate()
	{

		if ($this->entityNew->getOperation()!='insert') {
			throw new \Exception("You can clone an entity only for `insert` operation");
		}

		return $this->attachFields();

	}

	/**
	 * populate the new entity with information from the original entity
	 */
	abstract protected function attachFields();

}

