<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

namespace E4J\VikRestaurants\Recurrence\Types;

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

use E4J\VikRestaurants\Recurrence\RecurrenceAware;

/**
 * Monthly recurrence pattern.
 * Checks whether the provided date is between the specified period, which might
 * repeat with monthly frequency.
 * 
 * @since 1.10
 */
class MonthlyRecurrence extends RecurrenceAware
{
    /**
     * @inheritDoc
     */
    protected function isMatchingDate(\DateTime $date, \DateTime $start, \DateTime $end)
    {
        // calculate difference from start date and target date
        $diff = $date->diff($start);

        // adjust both the start and end dates to the target date
        $start->modify('+' . ($diff->y * 12 + $diff->m) . ' months');
        $end->modify('+' . ($diff->y * 12 + $diff->m) . ' months');
        
        return $start <= $date && $date <= $end;
    }
}
