PHP code example of oceanbigone / majorityjudgment

1. Go to this page and download the library: Download oceanbigone/majorityjudgment library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

oceanbigone / majorityjudgment example snippets




//start Ballot
$ballot= new Ballot();

//create Mention
$excellent  = new Mention("Excellent");
$good       = new Mention("Good");
$prettyGood = new Mention("Pretty good");
[...]

//create Candidate
$candidate1 = new Candidate("Mrs ABCDE");
$candidate2 = new Candidate("Mr FGHIJ");

//add Mentions -- from the best to the worst (order is important) !!!!
$ballot->addMention($excellent);
$ballot->addMention($good);
$ballot->addMention($prettyGood);
[...]

//add some Candidats
$ballot->addCandidate($candidate1);
$ballot->addCandidate($candidate2);
[...]


//add votes (keep in mind that each participation need a vote for each candidate !)
$ballot->addVote(new Vote($candidate1,$excellent));
$ballot->addVote(new Vote($candidate2,$prettyGood));

$ballot->addVote(new Vote($candidate1,$good));
$ballot->addVote(new Vote($candidate2,$excellent));
[...]

//get an array of candidate sorted by Majority Jugement, if there is full ex-aequo (even after index added) then they are ordered by name.
$sortedCandidates=$ballot->proceedElection();
var_dump($sortedCandidates);

//details with MeritProfile object
foreach($sortedCandidates as $candidate){
    
    $meritProfil=new MeritProfile();
    
    //get merit profil as Array of Merit object (Merit is an object with two property : mention and percent of this mention) 
    $merits=$meritProfil->getAsMeritArray($candidate,$ballot->getVotes(),$ballot->getMentions());
    
    //display majority mention
    echo $meritProfil->processMajorityMention($candidate,$ballot->getVotes(),$ballot->getMentions()))->getLabel();
    
    //display percent of majority mention
    echo $meritProfil->processPercentOfMajorityMention($candidate,$ballot->getVotes(),$ballot->getMentions()));
        
}

//clear mentions
$ballot->clearMentions();

//clear candidates
$ballot->clearCandidates();

//clear Votes
$ballot->clearVotes();