<?php

namespace Modules\RemoteData\EntityManagementWs\Model\Entity\Corporate;

class Matching extends \Modules\RemoteData\EntityManagementWs\Model\Entity\MatchingAbstract
{

	protected $forcedRequestAttributes = ['externalSystem', 'company', 'alias', 'fiscalCode'];

	/**
	 *
	 * {@inheritDoc}
	 * @see \Modules\RemoteData\EntityManagementWs\Model\Entity\GeneralAbstract::checkRequestData()
	 */
	protected function checkRequestData() :void
	{

		// validate input
		if (!$this->request->externalSystem and !$this->request->alias and !$this->request->company and !$this->request->fiscalCode) {
			throw new \Modules\RemoteData\EntityManagementWs\Model\RuntimeException('At least one criteria for matching must be set.', 'CorporateMatchCriteriaMissing');
		}

		if ($this->request->alias and mb_strlen($this->request->alias) < 2) {
			throw new \Modules\RemoteData\EntityManagementWs\Model\RuntimeException('The alias value must have at least 2 characters.', 'CorporateMatchAliasMinLength');
		}

		if ($this->request->company and mb_strlen($this->request->company) < 2) {
			throw new \Modules\RemoteData\EntityManagementWs\Model\RuntimeException('The company name must have at least 2 characters.', 'CorporateMatchCompanyMinLength');
		}

	}


	/**
	 * Populated the matching entity object.
	 * @return \App\EntityMatch\BaseAbstract
	 */
	protected function getEntityMatchObj() :\App\EntityMatch\BaseAbstract
	{

		// corporate matching object
		$corporateMatchObj = new \App\ImportedOrder\EntityMatch\Corporate('intersect');

		if ($this->request->externalSystem) {
			$corporateMatchObj->setIdInExtSys($this->request->externalSystem->idEntityExtSys, $this->request->externalSystem->idSystem);
		}
		if ($this->request->alias) {
			$corporateMatchObj->setAlias($this->request->alias);
		}
		if ($this->request->company) {
			$corporateMatchObj->setCompany($this->request->company);
		}
		if ($this->request->fiscalCode) {
			$corporateMatchObj->setFiscalCode($this->request->fiscalCode);
		}

		return $corporateMatchObj;

	}

}
