|
|
@@ -1,28 +1,19 @@
|
|
|
<?php
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
-namespace App\Service\Utils;
|
|
|
+namespace App\Service\Constraint;
|
|
|
|
|
|
use App\Entity\Access\Access;
|
|
|
use App\Service\Organization\Utils as organizationUtils;
|
|
|
-use App\Tests\Service\Utils\DateTimeConstraintTest;
|
|
|
+use App\Tests\Service\Constraint\DateTimeConstraintTest;
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
|
/**
|
|
|
* Classe DateTimeConstraint qui définie les dates de débuts et fin de périodes
|
|
|
* par rapport au contraintes temporelles choisies par un utilisateur.
|
|
|
*/
|
|
|
-class DateTimeConstraint
|
|
|
+class DateTimeConstraint extends AbstractTimeConstraintUtils
|
|
|
{
|
|
|
- const NULL = 0;
|
|
|
- const INF = 1;
|
|
|
- const EQUAL = 3;
|
|
|
- const SUP = 5;
|
|
|
- const CANCEL_OPERATION = 9;
|
|
|
- const START_KEY = 'start';
|
|
|
- const END_KEY = 'end';
|
|
|
- const NULL_VALUE = 'NULL';
|
|
|
-
|
|
|
public function __construct(
|
|
|
private EntityManagerInterface $entityManager,
|
|
|
)
|
|
|
@@ -58,16 +49,6 @@ class DateTimeConstraint
|
|
|
return $this->cleanConstraints($contraints);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Retourne true si l'utilisateur veux une période précise
|
|
|
- * @param $historical
|
|
|
- * @return bool
|
|
|
- * @see DateTimeConstraintTest::testHasCustomPeriods()
|
|
|
- */
|
|
|
- private function hasCustomPeriods($historical): bool{
|
|
|
- return array_key_exists('dateStart', $historical) && $historical['dateStart'] && array_key_exists('dateEnd', $historical) && $historical['dateEnd'];
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Retourne le tableau des périodes custom
|
|
|
* @param string $dateStart
|
|
|
@@ -103,86 +84,6 @@ class DateTimeConstraint
|
|
|
return $periods;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Fonction permettant d'ajouter une nouvelle contrainte de date
|
|
|
- * @param array $contraints
|
|
|
- * @param array $newContraint
|
|
|
- * @return array
|
|
|
- * @see DateTimeConstraintTest::testAddConstraint()
|
|
|
- */
|
|
|
- private function addConstraint(array $contraints, array $newContraint): array{
|
|
|
- $contraints = $this->mergeConstraint($contraints,$newContraint,self::START_KEY);
|
|
|
- $contraints = $this->mergeConstraint($contraints,$newContraint,self::END_KEY);
|
|
|
- return $contraints;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Construit le tableau de contraintes pour une condition (start, end)
|
|
|
- * @param array $contraints
|
|
|
- * @param array $newContraint
|
|
|
- * @param string $key
|
|
|
- * @return array
|
|
|
- */
|
|
|
- private function mergeConstraint(array $contraints, array $newContraint, string $key): array{
|
|
|
- if(array_key_exists($key, $newContraint)){
|
|
|
- foreach ($newContraint[$key] as $dateKey => $arithmeticValue){
|
|
|
- //Si la date à déjà des conditions
|
|
|
- if(array_key_exists($dateKey, $contraints[$key])){
|
|
|
- //Si la conditions (<, >, =, ...) n'est pas encore appliquée à la date
|
|
|
- if(!in_array($arithmeticValue, $contraints[$key][$dateKey]))
|
|
|
- $contraints[$key][$dateKey][] = $arithmeticValue;
|
|
|
- }else{
|
|
|
- $contraints[$key][$dateKey] = [$arithmeticValue];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return $contraints;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Nettoyage des contraintes (toutes celles supérieur à la condition cancel et les valeurs null isolées)
|
|
|
- * @param array $constraints
|
|
|
- * @return array
|
|
|
- * @see DateTimeConstraintTest::testCleanConstraints()
|
|
|
- */
|
|
|
- private function cleanConstraints(array $constraints): array{
|
|
|
- $constraints[self::START_KEY] = $this->filterConstraint($constraints, self::START_KEY);
|
|
|
- $constraints[self::START_KEY] = $this->clearNull($constraints, self::START_KEY);
|
|
|
-
|
|
|
- $constraints[self::END_KEY] = $this->filterConstraint($constraints, self::END_KEY);
|
|
|
- $constraints[self::END_KEY] = $this->clearNull($constraints, self::END_KEY);
|
|
|
- return $constraints;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Pour chaque contraintes appliquées à une date on vérifie qu'on ne dépasse pas la valeur cancel : c'est à dire
|
|
|
- * la condition qui démontre que toutes les valeurs arithmétiques ont été choisies
|
|
|
- * @param array $constraints
|
|
|
- * @param $key
|
|
|
- * @return array
|
|
|
- * @see DateTimeConstraintTest::testFilterConstraint()
|
|
|
- */
|
|
|
- private function filterConstraint(array $constraints, string $key): array{
|
|
|
- return array_filter($constraints[$key], function($constraint){
|
|
|
- return array_sum($constraint) < self::CANCEL_OPERATION ;
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * On ne conserve pas les contraintes sur des conditions start et end si ces dernieres ne possède qu'une valeur null :
|
|
|
- * une valeur null doit obligatoirement s'appliquer avec un OR
|
|
|
- * @param array $constraints
|
|
|
- * @param $key
|
|
|
- * @return array
|
|
|
- * @see DateTimeConstraintTest::testClearNull()
|
|
|
- */
|
|
|
- private function clearNull(array $constraints, string $key): array{
|
|
|
- if(count($constraints[$key]) === 1 && array_key_exists(self::NULL_VALUE, $constraints[$key]))
|
|
|
- $constraints[$key] = [];
|
|
|
- return $constraints[$key];
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Une période est dans le présent si :
|
|
|
* - la date de début est plus petite ou égale (<=) à la date de fin de période à afficher
|