<?php

namespace App\ImportedOrder\EntityMatch;

/**
 * The class is used for matching MICE projects and also adds functionality for matching projects
 * using the imported orders associations.
 * @author Gabriel
 */
class MiceProject extends \App\EntityMatch\MiceProject
{

	/**
	 * Forces the overwrite of cleanMatches() which cleans up all matching except 'ordersExtSys'
	 * criterion (if any).
	 */
	use \App\ImportedOrder\EntityMatch\ExtSysCleanMatchesTrait;

	/**
	 * Sets the id of the MICE project in external system.
	 * @param String $idEntityExtSys
	 * @param Int $idSystem - the external system id
	 */
	public function setIdInExtSys($idEntityExtSys, $idSystem)
	{
		$this->setSearchCriterion('ordersExtSys', $idEntityExtSys, false, ['idSystem'=>$idSystem]);
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\EntityMatch\MiceProject::dispatchSearchRecords()
	 */
	protected function dispatchSearchRecords($field, $value, $useWildcard, Array $options=[])
	{

		if ($field=='ordersExtSys') {
			$matchedId = \App\Config\ImportedOrders_Matches::getInstance()->getMiceProject($options['idSystem'], $value);
			if ($matchedId) {
				return $this->searchRecord_primaryKey($matchedId);
			} else {
				return null;
			}
		} else {
			return parent::dispatchSearchRecords($field, $value, $useWildcard, $options);
		}

	}


}
