<commit_before><?php
/**
 * Litus is a project by a group of students from the K.U.Leuven. The goal is to create
 * various applications to support the IT needs of student unions.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace LogisticsBundle\Component\Validator;

use DateTime,
    Doctrine\ORM\EntityManager;

/**
 * Checks whether no reservation exists yet for the given resource.
 *
 * @author <NAME> <<EMAIL>>
 */
class ReservationConflictValidator extends \Zend\Validator\AbstractValidator
{
    /**
     * Error codes
     * @const string
     */
    const CONFLICT_EXISTS      = 'conflictExists';

    /**
     * Error messages
     * @var array
     */
    protected $messageTemplates = array(
        self::CONFLICT_EXISTS      => "A conflicting reservation already exists for this resource.",
    );

    /**
     * The start date of the interval
     * 
     * @var string
     */
    private $_startDate;

    /**
     * @var string
     */
    private $_format;
    
    /**
     * @var LogisticsBundle\Entity\Reservation\ReservableResource
     */
    private $_resource;
    
    /**
     * The id of the reservation to ignore when searching for conflicts. -1 indicates none.
     * 
     * @var int
     */
    private $_reservationId;
    
    /**
     * @var \Doctrine\ORM\EntityManager The EntityManager instance
     */
    private $_entityManager = null;

    /**
     * Sets validator options
     *
     * @param  mixed $token
     * @param string $format
     * @return void
     */
    public function __construct($startDate, $format, $resource, $entityManager, $reservationId = -1)
    {
        $this->_startDate = $startDate;
        $this->_format = $format;
        $this->_resource = $resource;
        $this->_entityManager = $entityManager;
        $this->_reservationId = $reservationId;

        parent::__construct(null);
    }

    /**
     * Returns true if and only if no other reservation exists for the resource that conflicts with the new one.
     *
     * @param  mixed $value
     * @param  array $context
     * @return boolean
     */
    public function isValid($value, $context = null)
    {
        $this->setValue($value);

        if (($context !== null) && isset($context) && array_key_exists($this->_startDate, $context)) {
            $startDate = $context[$this->_startDate];
        } else {
            $this->error(self::NOT_VALID);
            return false;
        }

        if ($startDate === null) {
            $this->error(self::NOT_VALID);
            return false;
        }
        
        $repository = $this->_entityManager
            ->getRepository('LogisticsBundle\Entity\Reservation\ReservableResource');
        $resource = $repository->findOneByName($this->_resource);
        
        $startDate = DateTime::createFromFormat($this->_format, $startDate);
        $endDate = DateTime::createFromFormat($this->_format, $value);

        $repository = $this->_entityManager
            ->getRepository('LogisticsBundle\Entity\Reservation\Reservation');
        
        $conflicting = $repository->findAllConflictingIgnoringId($startDate, $endDate, $resource, $this->_reservationId);<commit_msg>Fix crash on bad date format input<commit_after><?php
/**
 * Litus is a project by a group of students from the K.U.Leuven. The goal is to create
 * various applications to support the IT needs of student unions.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace LogisticsBundle\Component\Validator;

use DateTime,
    Doctrine\ORM\EntityManager;

/**
 * Checks whether no reservation exists yet for the given resource.
 *
 * @author <NAME> <<EMAIL>>
 */
class ReservationConflictValidator extends \Zend\Validator\AbstractValidator
{
    /**
     * Error codes
     * @const string
     */
    const CONFLICT_EXISTS      = 'conflictExists';
    const INVALID_FORMAT = 'invalidFormat';

    /**
     * Error messages
     * @var array
     */
    protected $messageTemplates = array(
        self::CONFLICT_EXISTS      => "A conflicting reservation already exists for this resource.",
        self::INVALID_FORMAT       => "One of the dates is not in the correct format.",
    );

    /**
     * The start date of the interval
     * 
     * @var string
     */
    private $_startDate;

    /**
     * @var string
     */
    private $_format;
    
    /**
     * @var LogisticsBundle\Entity\Reservation\ReservableResource
     */
    private $_resource;
    
    /**
     * The id of the reservation to ignore when searching for conflicts. -1 indicates none.
     * 
     * @var int
     */
    private $_reservationId;
    
    /**
     * @var \Doctrine\ORM\EntityManager The EntityManager instance
     */
    private $_entityManager = null;

    /**
     * Sets validator options
     *
     * @param  mixed $token
     * @param string $format
     * @return void
     */
    public function __construct($startDate, $format, $resource, $entityManager, $reservationId = -1)
    {
        $this->_startDate = $startDate;
        $this->_format = $format;
        $this->_resource = $resource;
        $this->_entityManager = $entityManager;
        $this->_reservationId = $reservationId;

        parent::__construct(null);
    }

    /**
     * Returns true if and only if no other reservation exists for the resource that conflicts with the new one.
     *
     * @param  mixed $value
     * @param  array $context
     * @return boolean
     */
    public function isValid($value, $context = null)
    {
        $this->setValue($value);

        if (($context !== null) && isset($context) && array_key_exists($this->_startDate, $context)) {
            $startDate = $context[$this->_startDate];
        } else {
            $this->error(self::NOT_VALID);
            return false;
        }

        if ($startDate === null) {
            $this->error(self::NOT_VALID);
            return false;
        }
        
        $repository = $this->_entityManager
            ->getRepository('LogisticsBundle\Entity\Reservation\ReservableResource');
        $resource = $repository->findOneByName($this->_resource);
        
        $startDate = DateTime::createFromFormat($this->_format, $startDate);
        $endDate = DateTime::createFromFormat($this->_format, $value);

        if (!$startDate || !$endDate) {
            return false;
        }
        
        $repository = $this->_entityManager
            ->getRepository('LogisticsBundle\Entity\Reservation\Reservation');
        
        $conflicting = $repository->findAllConflictingIgnoringId($startDate, $endDate, $resource, $this->_reservationId);
{'fraction_non_alphanumeric': 0.1285387474978553, 'fraction_numerical': 0.0005719187875321705, 'mean_word_length': 2.41053144807411, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 94, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3406093', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1959, 'n_words': 504}