mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-01-15 05:41:25 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,9 +1,29 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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\Util;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use DateTimeZone;
|
||||
use LogicException;
|
||||
use function array_diff;
|
||||
use function array_flip;
|
||||
use function array_intersect_key;
|
||||
use function array_key_exists;
|
||||
use function array_merge;
|
||||
use function array_unique;
|
||||
use function array_values;
|
||||
use function ksort;
|
||||
|
||||
/**
|
||||
* Utilities to compare date periods
|
||||
* Utilities to compare date periods.
|
||||
*
|
||||
* This class allow to compare periods when there are period covering. The
|
||||
* argument `minCovers` allow to find also when there are more than 2 period
|
||||
@@ -37,33 +57,33 @@ class DateRangeCovering
|
||||
|
||||
private array $intervals = [];
|
||||
|
||||
private int $minCover;
|
||||
|
||||
private int $uniqueKeyCounter = 0;
|
||||
|
||||
private array $metadatas = [];
|
||||
|
||||
private int $minCover;
|
||||
|
||||
private array $sequence = [];
|
||||
|
||||
private \DateTimeZone $tz;
|
||||
private DateTimeZone $tz;
|
||||
|
||||
private int $uniqueKeyCounter = 0;
|
||||
|
||||
/**
|
||||
* @param int $minCover the minimum of covering required
|
||||
*/
|
||||
public function __construct(int $minCover, \DateTimeZone $tz)
|
||||
public function __construct(int $minCover, DateTimeZone $tz)
|
||||
{
|
||||
if ($minCover < 0) {
|
||||
throw new \LogicException("argument minCover cannot be lower than 0");
|
||||
if (0 > $minCover) {
|
||||
throw new LogicException('argument minCover cannot be lower than 0');
|
||||
}
|
||||
|
||||
$this->minCover = $minCover;
|
||||
$this->tz = $tz;
|
||||
}
|
||||
|
||||
public function add(\DateTimeInterface $start, \DateTimeInterface $end = null, $metadata = null): self
|
||||
public function add(DateTimeInterface $start, ?DateTimeInterface $end = null, $metadata = null): self
|
||||
{
|
||||
if ($this->computed) {
|
||||
throw new \LogicException("You cannot add intervals to a computed instance");
|
||||
throw new LogicException('You cannot add intervals to a computed instance');
|
||||
}
|
||||
|
||||
$k = $this->uniqueKeyCounter++;
|
||||
@@ -72,29 +92,17 @@ class DateRangeCovering
|
||||
|
||||
$this->addToSequence($start->getTimestamp(), $k, null);
|
||||
$this->addToSequence(
|
||||
NULL === $end ? PHP_INT_MAX : $end->getTimestamp(), null, $k
|
||||
null === $end ? PHP_INT_MAX : $end->getTimestamp(),
|
||||
null,
|
||||
$k
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function addToSequence($timestamp, int $start = null, int $end = null)
|
||||
{
|
||||
if (!\array_key_exists($timestamp, $this->sequence)) {
|
||||
$this->sequence[$timestamp] = [ 's' => [], 'e' => [] ];
|
||||
}
|
||||
|
||||
if (NULL !== $start) {
|
||||
$this->sequence[$timestamp]['s'][] = $start;
|
||||
}
|
||||
if (NULL !== $end) {
|
||||
$this->sequence[$timestamp]['e'][] = $end;
|
||||
}
|
||||
}
|
||||
|
||||
public function compute(): self
|
||||
{
|
||||
\ksort($this->sequence);
|
||||
ksort($this->sequence);
|
||||
|
||||
$currentPeriod = [];
|
||||
$currents = [];
|
||||
@@ -102,8 +110,8 @@ class DateRangeCovering
|
||||
$overs = [];
|
||||
|
||||
foreach ($this->sequence as $ts => $moves) {
|
||||
$currents = \array_merge($currents, $moves['s']);
|
||||
$currents = \array_diff($currents, $moves['e']);
|
||||
$currents = array_merge($currents, $moves['s']);
|
||||
$currents = array_diff($currents, $moves['e']);
|
||||
|
||||
if (count($currents) > $this->minCover && !$isOpen) {
|
||||
$currentPeriod[0] = $ts;
|
||||
@@ -115,23 +123,23 @@ class DateRangeCovering
|
||||
$currentPeriod = [];
|
||||
$isOpen = false;
|
||||
} elseif ($isOpen) {
|
||||
$currentPeriod[2] = \array_merge($currentPeriod[2], $currents);
|
||||
$currentPeriod[2] = array_merge($currentPeriod[2], $currents);
|
||||
}
|
||||
}
|
||||
|
||||
// process metadata
|
||||
foreach ($overs as list($start, $end, $metadata)) {
|
||||
foreach ($overs as [$start, $end, $metadata]) {
|
||||
$this->intersections[] = [
|
||||
(new \DateTimeImmutable('@'.$start))
|
||||
(new DateTimeImmutable('@' . $start))
|
||||
->setTimezone($this->tz),
|
||||
$end === PHP_INT_MAX ? null : (new \DateTimeImmutable('@'.$end))
|
||||
PHP_INT_MAX === $end ? null : (new DateTimeImmutable('@' . $end))
|
||||
->setTimezone($this->tz),
|
||||
\array_values(
|
||||
\array_intersect_key(
|
||||
array_values(
|
||||
array_intersect_key(
|
||||
$this->metadatas,
|
||||
\array_flip(\array_unique($metadata))
|
||||
array_flip(array_unique($metadata))
|
||||
)
|
||||
)
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -140,24 +148,38 @@ class DateRangeCovering
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasIntersections(): bool
|
||||
{
|
||||
if (!$this->computed) {
|
||||
throw new \LogicException(sprintf("You cannot call the method %s before ".
|
||||
"'process'", __METHOD__));
|
||||
}
|
||||
|
||||
return count($this->intersections) > 0;
|
||||
}
|
||||
|
||||
public function getIntersections(): array
|
||||
{
|
||||
if (!$this->computed) {
|
||||
throw new \LogicException(sprintf("You cannot call the method %s before ".
|
||||
throw new LogicException(sprintf('You cannot call the method %s before ' .
|
||||
"'process'", __METHOD__));
|
||||
}
|
||||
|
||||
return $this->intersections;
|
||||
}
|
||||
|
||||
public function hasIntersections(): bool
|
||||
{
|
||||
if (!$this->computed) {
|
||||
throw new LogicException(sprintf('You cannot call the method %s before ' .
|
||||
"'process'", __METHOD__));
|
||||
}
|
||||
|
||||
return count($this->intersections) > 0;
|
||||
}
|
||||
|
||||
private function addToSequence($timestamp, ?int $start = null, ?int $end = null)
|
||||
{
|
||||
if (!array_key_exists($timestamp, $this->sequence)) {
|
||||
$this->sequence[$timestamp] = ['s' => [], 'e' => []];
|
||||
}
|
||||
|
||||
if (null !== $start) {
|
||||
$this->sequence[$timestamp]['s'][] = $start;
|
||||
}
|
||||
|
||||
if (null !== $end) {
|
||||
$this->sequence[$timestamp]['e'][] = $end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user