mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-29 19:13:49 +00:00
Merge remote-tracking branch 'origin/111_exports_suite' into calendar/finalization
This commit is contained in:
101
src/Bundle/ChillMainBundle/Service/RollingDate/RollingDate.php
Normal file
101
src/Bundle/ChillMainBundle/Service/RollingDate/RollingDate.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Service\RollingDate;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
class RollingDate
|
||||
{
|
||||
public const ALL_T = [
|
||||
self::T_YEAR_PREVIOUS_START,
|
||||
self::T_QUARTER_PREVIOUS_START,
|
||||
self::T_MONTH_PREVIOUS_START,
|
||||
self::T_WEEK_PREVIOUS_START,
|
||||
self::T_YEAR_CURRENT_START,
|
||||
self::T_QUARTER_CURRENT_START,
|
||||
self::T_MONTH_CURRENT_START,
|
||||
self::T_WEEK_CURRENT_START,
|
||||
self::T_TODAY,
|
||||
self::T_WEEK_NEXT_START,
|
||||
self::T_MONTH_NEXT_START,
|
||||
self::T_QUARTER_NEXT_START,
|
||||
self::T_YEAR_NEXT_START,
|
||||
self::T_FIXED_DATE,
|
||||
];
|
||||
|
||||
/**
|
||||
* A given fixed date.
|
||||
*/
|
||||
public const T_FIXED_DATE = 'fixed_date';
|
||||
|
||||
public const T_MONTH_CURRENT_START = 'month_current_start';
|
||||
|
||||
public const T_MONTH_NEXT_START = 'month_next_start';
|
||||
|
||||
public const T_MONTH_PREVIOUS_START = 'month_previous_start';
|
||||
|
||||
public const T_QUARTER_CURRENT_START = 'quarter_current_start';
|
||||
|
||||
public const T_QUARTER_NEXT_START = 'quarter_next_start';
|
||||
|
||||
public const T_QUARTER_PREVIOUS_START = 'quarter_previous_start';
|
||||
|
||||
public const T_TODAY = 'today';
|
||||
|
||||
public const T_WEEK_CURRENT_START = 'week_current_start';
|
||||
|
||||
public const T_WEEK_NEXT_START = 'week_next_start';
|
||||
|
||||
public const T_WEEK_PREVIOUS_START = 'week_previous_start';
|
||||
|
||||
public const T_YEAR_CURRENT_START = 'year_current_start';
|
||||
|
||||
public const T_YEAR_NEXT_START = 'year_next_start';
|
||||
|
||||
public const T_YEAR_PREVIOUS_START = 'year_previous_start';
|
||||
|
||||
private ?DateTimeImmutable $fixedDate;
|
||||
|
||||
/**
|
||||
* The pivot date is the date from the rolling is computed. By default, it is "now".
|
||||
*/
|
||||
private DateTimeImmutable $pivotDate;
|
||||
|
||||
private string $roll;
|
||||
|
||||
/**
|
||||
* @param string|self::T_* $roll
|
||||
* @param DateTimeImmutable|null $pivotDate Will be "now" if null is given
|
||||
* @param DateTimeImmutable|null $fixedDate Only to insert if $roll equals @see{self::T_FIXED_DATE}
|
||||
*/
|
||||
public function __construct(string $roll, ?DateTimeImmutable $fixedDate = null, ?DateTimeImmutable $pivotDate = null)
|
||||
{
|
||||
$this->roll = $roll;
|
||||
$this->pivotDate = $pivotDate ?? new DateTimeImmutable('now');
|
||||
$this->fixedDate = $fixedDate;
|
||||
}
|
||||
|
||||
public function getFixedDate(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->fixedDate;
|
||||
}
|
||||
|
||||
public function getPivotDate(): DateTimeImmutable
|
||||
{
|
||||
return $this->pivotDate;
|
||||
}
|
||||
|
||||
public function getRoll(): string
|
||||
{
|
||||
return $this->roll;
|
||||
}
|
||||
}
|
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Service\RollingDate;
|
||||
|
||||
use DateInterval;
|
||||
use DateTimeImmutable;
|
||||
use LogicException;
|
||||
use UnexpectedValueException;
|
||||
|
||||
class RollingDateConverter implements RollingDateConverterInterface
|
||||
{
|
||||
public function convert(RollingDate $rollingDate): DateTimeImmutable
|
||||
{
|
||||
switch ($rollingDate->getRoll()) {
|
||||
case RollingDate::T_MONTH_CURRENT_START:
|
||||
return $this->toBeginOfMonth($rollingDate->getPivotDate());
|
||||
|
||||
case RollingDate::T_MONTH_NEXT_START:
|
||||
return $this->toBeginOfMonth($rollingDate->getPivotDate()->add(new DateInterval('P1M')));
|
||||
|
||||
case RollingDate::T_MONTH_PREVIOUS_START:
|
||||
return $this->toBeginOfMonth($rollingDate->getPivotDate()->sub(new DateInterval('P1M')));
|
||||
|
||||
case RollingDate::T_QUARTER_CURRENT_START:
|
||||
return $this->toBeginOfQuarter($rollingDate->getPivotDate());
|
||||
|
||||
case RollingDate::T_QUARTER_NEXT_START:
|
||||
return $this->toBeginOfQuarter($rollingDate->getPivotDate()->add(new DateInterval('P3M')));
|
||||
|
||||
case RollingDate::T_QUARTER_PREVIOUS_START:
|
||||
return $this->toBeginOfQuarter($rollingDate->getPivotDate()->sub(new DateInterval('P3M')));
|
||||
|
||||
case RollingDate::T_WEEK_CURRENT_START:
|
||||
return $this->toBeginOfWeek($rollingDate->getPivotDate());
|
||||
|
||||
case RollingDate::T_WEEK_NEXT_START:
|
||||
return $this->toBeginOfWeek($rollingDate->getPivotDate()->add(new DateInterval('P1W')));
|
||||
|
||||
case RollingDate::T_WEEK_PREVIOUS_START:
|
||||
return $this->toBeginOfWeek($rollingDate->getPivotDate()->sub(new DateInterval('P1W')));
|
||||
|
||||
case RollingDate::T_YEAR_CURRENT_START:
|
||||
return $this->toBeginOfYear($rollingDate->getPivotDate());
|
||||
|
||||
case RollingDate::T_YEAR_PREVIOUS_START:
|
||||
return $this->toBeginOfYear($rollingDate->getPivotDate()->sub(new DateInterval('P1Y')));
|
||||
|
||||
case RollingDate::T_YEAR_NEXT_START:
|
||||
return $this->toBeginOfYear($rollingDate->getPivotDate()->add(new DateInterval('P1Y')));
|
||||
|
||||
case RollingDate::T_TODAY:
|
||||
return $rollingDate->getPivotDate();
|
||||
|
||||
case RollingDate::T_FIXED_DATE:
|
||||
if (null === $rollingDate->getFixedDate()) {
|
||||
throw new LogicException('You must provide a fixed date when selecting a fixed date');
|
||||
}
|
||||
|
||||
return $rollingDate->getFixedDate();
|
||||
|
||||
default:
|
||||
throw new UnexpectedValueException(sprintf('%s rolling operation not supported', $rollingDate->getRoll()));
|
||||
}
|
||||
}
|
||||
|
||||
private function toBeginOfMonth(DateTimeImmutable $date): DateTimeImmutable
|
||||
{
|
||||
return DateTimeImmutable::createFromFormat(
|
||||
'Y-m-d His',
|
||||
sprintf('%s-%s-01 000000', $date->format('Y'), $date->format('m'))
|
||||
);
|
||||
}
|
||||
|
||||
private function toBeginOfQuarter(DateTimeImmutable $date): DateTimeImmutable
|
||||
{
|
||||
switch ((int) $date->format('n')) {
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
$month = '01';
|
||||
|
||||
break;
|
||||
|
||||
case 4:
|
||||
case 5:
|
||||
case 6:
|
||||
$month = '04';
|
||||
|
||||
break;
|
||||
|
||||
case 7:
|
||||
case 8:
|
||||
case 9:
|
||||
$month = '07';
|
||||
|
||||
break;
|
||||
|
||||
case 10:
|
||||
case 11:
|
||||
case 12:
|
||||
$month = '10';
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new LogicException('this month is not valid: ' . $date->format('n'));
|
||||
}
|
||||
|
||||
return DateTimeImmutable::createFromFormat(
|
||||
'Y-m-d His',
|
||||
sprintf('%s-%s-01 000000', $date->format('Y'), $month)
|
||||
);
|
||||
}
|
||||
|
||||
private function toBeginOfWeek(DateTimeImmutable $date): DateTimeImmutable
|
||||
{
|
||||
if (1 === $dayOfWeek = (int) $date->format('N')) {
|
||||
return $date->setTime(0, 0, 0);
|
||||
}
|
||||
|
||||
return $date
|
||||
->sub(new DateInterval('P' . ($dayOfWeek - 1) . 'D'))
|
||||
->setTime(0, 0, 0);
|
||||
}
|
||||
|
||||
private function toBeginOfYear(DateTimeImmutable $date): DateTimeImmutable
|
||||
{
|
||||
return DateTimeImmutable::createFromFormat(
|
||||
'Y-m-d His',
|
||||
sprintf('%s-01-01 000000', $date->format('Y'))
|
||||
);
|
||||
}
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Service\RollingDate;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
interface RollingDateConverterInterface
|
||||
{
|
||||
public function convert(RollingDate $rollingDate): DateTimeImmutable;
|
||||
}
|
Reference in New Issue
Block a user