<?php

namespace App\OfferProfileRules;

class Criterion_OfferStartDate extends CriterionAbstract
{

	/**
	 * Sets the operation and the values for the current criterion - the values are stored as a unique list of elements.
	 * @param String $operation
	 * @param Array $values
	 */
	public function __construct($operation, $startValue, $endValue)
	{
		parent::__construct($operation, ['start'=>$startValue, 'end'=>$endValue]);
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\OfferProfileRules\CriterionAbstract::checkFields()
	 */
	public function checkFields(\App\OfferProfileRules\Fields $fields)
	{
		return $this->checkField($fields->offerStartDate);
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\OfferProfileRules\CriterionAbstract::checkAssignedValues()
	 */
	public function checkAssignedValues(\App\MessagesCollector $msgs)
	{

		if ($this->values['start'] and !validDate($this->values['start'])) {
			$msgs->addWarning('The start date is not valid.');
		}

		if ($this->values['end'] and !validDate($this->values['end'])) {
			$msgs->addWarning('The end date is not valid.');
		}

		if (!$this->values['start'] and !$this->values['end']) {
			$msgs->addWarning('At least the beginning or the end of the offer start date must be set.');
		}

	}

	/**
	 * Returns the name of the current entity id
	 * @param String $val
	 */
	protected function createDescription_getEntityName($code)
	{
		return \Engine\DateTime::createFromIsoDate($code)->formatDateStd();
	}

}
