public function testFromVoixAndExprimes() { $score = Score::fromVoixAndExprimes(500, 1000); $this->assertEquals(500, $score->toVoix()); $this->assertLessThanOrEqual(50.001, $score->toPourcentage()); $this->assertGreaterThanOrEqual(49.999, $score->toPourcentage()); }
public function getScore(Echeance $echeance, $territoire, $candidat) { /* * Si le territoire est un tableau, on boucle sur la fonction elle-meme * et on attribue le total à $$score. */ if (is_array($territoire) || $territoire instanceof \ArrayAccess || $territoire instanceof \IteratorAggregate) { $score = 0; foreach ($territoire as $division) { $scoreVO = $this->getScore($echeance, $division, $candidat); $score += $scoreVO->toVoix(); } if (!$score) { return new Score(); } $score = Score::fromVoix($score); } /** * Si $score est vide est qu'on a un groupe de nuance, on passe par * le NuanceOptimizer et retourne. */ if ((!isset($score) || !$score) && $candidat instanceof CandidatNuanceSpecification) { $score = $this->nuanceOptimizer->getScore($echeance, $territoire, $candidat); return $score ? Score::fromVoixAndExprimes($score->toVoix(), $this->getVoteInfo($echeance, $territoire)->getExprimes()) : new Score(); } /* * Si on a pas retourné avec le NuanceOptimizer et que le score est vide * et qu'on a un candidat unique à une élection connue on passe par * l'election optimizer. */ if ((!isset($score) || !$score) && $candidat instanceof Candidat && $this->get($echeance, $territoire)) { $score = $this->sameElectionOptimizer->getScore($echeance, $territoire, $candidat); return $score ? Score::fromVoixAndExprimes($score->toVoix(), $this->getVoteInfo($echeance, $territoire)->getExprimes()) : new Score(); } /* * En dernier recours on fait une requete classique sur l'unique * territoire. */ if (!isset($score) || !$score) { $score = $this->doScoreQuery($echeance, $territoire, $candidat); } /* * Et si on a toujours rien, on fait une requete de consolidation * des échelons plus petits. */ if (!$score) { if ($territoire instanceof Region) { $score = $this->doScoreRegionQuery($echeance, $territoire, $candidat); } if ($territoire instanceof Departement) { $score = $this->doScoreDepartementQuery($echeance, $territoire, $candidat); } if ($territoire instanceof CirconscriptionEuropeenne) { $score = $this->getScore($echeance, $territoire->getRegions(), $candidat); } if ($territoire instanceof Pays) { $score = $this->getScore($echeance, $territoire->getCirconscriptionsEuropeennes(), $candidat); } } return $score ? Score::fromVoixAndExprimes($score->toVoix(), $this->getVoteInfo($echeance, $territoire)->getExprimes()) : new Score(); }
/** * Mettre à jour le nombre de voix d'un candidat sur un territoire donné, * ou par défaut sur la circonscription de l'élection. Si le nombre de * suffrages exprimés est déjà réglé dans l'élection, le pourcentage est * mis à jour automatiquement. Sinon, pourcentage et voix sont effacés et * remplacés par cette donnée. * * @param int $voix Le nombre de voix du candidat. * @param CandidatInterface $candidat Le candidat dont il s'agit. * @param AbstractTerritoire $territoire Le territoire du score. */ public function setVoixCandidat($voix, CandidatInterface $candidat, AbstractTerritoire $territoire = null) { if (!in_array($candidat, $this->getCandidats(), true)) { throw new \Exception('Le candidat doit déjà participer à l\'élection' . 'avant d\'avoir un score'); } if (null === $territoire) { $territoire = $this->circonscription; } $voteInfo = $this->getVoteInfo($territoire); if ($voteInfo) { $exprimes = $voteInfo->getExprimes(); } if (isset($exprimes)) { $score = Score::fromVoixAndExprimes($voix, $exprimes); } else { $score = Score::fromVoix($voix); } $scoreAssignment = $this->getScoreAssignmentCandidat($candidat, $territoire); $scoreAssignment->setScoreVO($score); }