<?php
namespace App\ImportedDocument;

/**
 * Stores the user details for an imported document.
 * @author cristiana.matache
 */

class User extends EntityAbstract
{
	/**
	 * TINA user id.
	 * @var Int
	 */
	public $idTina;

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

	/**
	 * The first name of the user.
	 * @var String
	 */
	public $firstName;

	/**
	 * The last name of the user.
	 * @var String
	 */
	public $lastName;


	/**
	 *
	 * {@inheritDoc}
	 * @see \App\ImportedDocument\EntityAbstract::validateEntityFields()
	 */
	protected function validateEntityFields(\App\MessagesCollector $messagesCollector)
	{

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

	}

	/**
	 * Gets the TINA matching user
	 */
	public function getMatchedId($idExternalSystem)
	{

		$matchObj =  new \App\ImportedDocument\EntityMatch\User('intersect');
		if ($this->idTina) {
			$matchObj->setId($this->idTina);
		}
		if ($this->lastName or $this->firstName) {
			$matchObj->setName($this->lastName, $this->firstName, true, true);
		}
		if ($idExternalSystem and $this->idSystem) {
			$matchObj->setIdInExtSys($this->idSystem, $idExternalSystem);
		}
		$matchObj->search();
		if ($matchObj->getResultsCount()==1) {
			return $matchObj->getFirstMatchId();
		}

		return 0;

	}

}