<?php

namespace App\ImportedOrder;

/**
 * Contains info about the MICE project.
 * @author Gabriel
 */
class MiceProject extends EntityAbstract
{

	/**
	 * The MICE project id in TINA.
	 * @var Int
	 */
	public $idTina;

	/**
	 * The MICE project id in the external system.
	 * @var String
	 */
	public $idSystem;

	/**
	 * The MICE project name.
	 * @var String
	 */
	public $name;

	/**
	 * The MICE project code.
	 * @var String
	 */
	public $code;


	/**
	 * (non-PHPdoc)
	 * @see \App\ImportedOrder\EntityAbstract::validateEntityFields()
	 */
	protected function validateEntityFields(\App\MessagesCollector $messagesCollector): void
	{

		// checks idTina
		if ( $this->idTina and (!is_numeric($this->idTina) or $this->idTina<0) ) {
			$messagesCollector->addInfo('The TINA id is invalid - it should be a positive integer.');
		}

	}


	/**
	 * Matches the entity by its fields - can also check for matches in the external
	 * systems associations (if $idExternalSystem is set).
	 * @param Int $idExternalSystem - if set, it is attempted to match the user by the manual associations;
	 * @return Int - the entity id, if matched; 0/blank if no entity was matched
	 */
	public function getMatchedId($idExternalSystem)
	{

		$matchObj = new \App\ImportedOrder\EntityMatch\MiceProject('intersect');
		if ($this->idTina) {
			$matchObj->setId($this->idTina);
		}
		if ($idExternalSystem and $this->idSystem) {
			$matchObj->setIdInExtSys($this->idSystem, $idExternalSystem);
		}
		if ($this->name) {
			$matchObj->setName($this->name);
		}
		if ($this->code) {
			$matchObj->setCode($this->code);
		}
		$matchObj->search();
		if ($matchObj->getResultsCount()==1) {
			return $matchObj->getFirstMatchId();
		}

		return 0;

	}


}
