<?php

namespace App\Modules\Shared\Domain;

class DateReportUtils
{

    public static function eachMonth(array $dates, callable $callback)
    {
        foreach ($dates as $month) {
            $m = $month['dateStart']->format('m');
            $y = $month['dateEnd']->format('Y');
            $callback($m, $y);
        }
    }

    public static function cutByMonth($months)
    {
        $dates = [];
        foreach ($months as $date) {
            $next = clone $date;
            $next->add(new \DateInterval('P1M'));
            $next->sub(new \DateInterval('P1D'));
            $dates[] = [
                'dateStart' => $date,
                'dateEnd' => $next
            ];
        }
        return $dates;
    }

    public static function cutByYears($years)
    {
        $dates = [];
        foreach ($years as $date) {
            $next = clone $date;
            $next->add(new \DateInterval('P1Y'));
            $next->sub(new \DateInterval('P1D'));
            $dates[] = [
                'dateStart' => $date,
                'dateEnd' => $next
            ];
        }
        return $dates;
    }
}
