// An objet $myMessiaenCandidate = new Candidate('Olivier Messiaen'); $election1->addCandidate($myMessiaenCandidate); $election1->addCandidate(new Candidate('Ligeti')); $election1->addCandidate(new Candidate('Koechlin')); # -B- Change your mind ? $election1->removeCandidate('A'); $election1->removeCandidate($myAutomaticCandidate); // Lutoslawski change his name $myLutoCandidate->setName('Wiltod Lutoslawski'); # Done ! # What was his old names? $myLutoCandidate->getHistory(); // return the full history with timestamp of this Candidate naming # -C- Check your candidate list, if you forget it $election1->getCandidatesList(); // Return an array pupulate by each Candidate objet $election1->getCandidatesList(true); // Return an array pupulate by each Candidate name as String. // OK, I need my Debussy (want his candidate object) $myDebussyCandidate = $election1->getCandidateObjectByName('Debussy'); // IV - Votes # -A- Add Votes $myVote1 = $election1->addVote(array(1 => 'Debussy', 2 => ['Caplet', 'Olivier Messiaen'], 3 => 'Ligeti', 4 => ['Wiltod Lutoslawski', 'Koechlin'])); // Return the new Vote object $myVote2 = new Vote(array(1 => 'Debussy', 2 => 'Caplet', 3 => 'Olivier Messiaen', 4 => 'Koechlin', 5 => 'Wiltod Lutoslawski', 6 => 'Ligeti')); $election1->addVote($myVote2); $myVote3 = $election1->addVote('Debussy > Olivier Messiaen = Ligeti > Caplet'); // Last rank rank will be automatically deducted. A vote can not be expressed on a limited number of candidates. Non-expressed candidates are presumed to last Exaequo // Off course, you can vote by candidate object. Which is recommended. Or mix the two methods. $myVote4 = $election1->addVote(array($election1->getCandidateObjectByName('Ligeti'), $myLutoCandidate, [$myMessiaenCandidate, 'Koechlin']));
Number of Candidates : <?php echo $election->countCandidates(); ?> | Number of votes : <?php echo $election->countVotes(); ?> </em> <h2>Candidates list :</h2> <ul> <?php foreach ($election->getCandidatesList() as $candidatName) { echo '<li>' . $candidatName . '</li>'; } ?> </ul> <h2>Registered votes details :</h2> <?php foreach ($election->getVotesList() as $vote) { echo '<div class="votant">'; echo '<strong style="color:green;">' . implode(' / ', $vote->getTags()) . '</strong><br>'; echo "<ol>"; foreach ($vote as $rank => $value) { if ($rank == 'tag') { continue;
$myElection = new Election(); $myElection->addCandidate(new Candidate('A')); $myElection->addCandidate(new Candidate('B')); $myElection->addCandidate(new Candidate('C')); $myElection->addCandidate(new Candidate('D')); $myElection->addCandidate(new Candidate('E')); $myElection->addCandidate(new Candidate('F')); // II - Setup external drivers /* We will use PDO SQLITE, but can be MySQL or else */ unlink(__DIR__ . '/bdd.sqlite'); $pdo_object = new \PDO('sqlite:' . __DIR__ . '/bdd.sqlite'); $database_map = ['tableName' => 'Entitys', 'primaryColumnName' => 'id', 'dataColumnName' => 'data']; $driver = new PdoHandlerDriver($pdo_object, true, $database_map); // true = Try to create table $myElection->setExternalDataHandler($driver); // III - Add hundred of thousands votes set_time_limit(60 * 5); $howMany = 100000; $voteModel = $myElection->getCandidatesList(); for ($i = 0; $i < $howMany; $i++) { shuffle($voteModel); $myElection->addVote($voteModel); } // IV - Get somes results $myElection->getWinner(); $myElection->getResult('Schulze'); print 'Success! Process in: ' . round(microtime(true) - $start_time, 2) . "s\n"; echo ' Peak of memory allocated : ' . round(memory_get_peak_usage() / pow(1024, $i = floor(log(memory_get_peak_usage(), 1024))), 2) . ' ' . ['b', 'kb', 'mb', 'gb', 'tb', 'pb'][$i] . '<br>'; // Close external driver and and retrieve data into classical internal RAM memory. /* $myElection->closeHandler(); */