/** * Execute the console command. * * @return mixed */ public function handle() { $election = new Election(); $candidates = [1 => new Candidate('Bjørnar Moxnes'), 2 => new Candidate('Kari Elisabeth Kaski'), 3 => new Candidate('Lan Marie Nguyen Berg'), 4 => new Candidate('Abid Raja'), 5 => new Candidate('Per Sandberg')]; foreach ($candidates as $candidate) { $election->addCandidate($candidate); } $votes = [[$candidates[1], $candidates[3], $candidates[2]], [$candidates[1], $candidates[3], $candidates[4]], [$candidates[1], $candidates[2], $candidates[4]], [$candidates[1], $candidates[2]], [$candidates[1], $candidates[2], $candidates[4]], [$candidates[5]], [$candidates[1], $candidates[2], $candidates[5]], [$candidates[1], $candidates[5]], [$candidates[1]], [$candidates[1]], [$candidates[2], $candidates[1], $candidates[3], $candidates[4]], [$candidates[2], $candidates[3], $candidates[4], $candidates[1], $candidates[5]], [$candidates[5], $candidates[4], $candidates[3]]]; foreach ($votes as $vote) { $election->addVote($vote); } $result = $election->getResult('RankedPairs'); Condorcet::format($result); }
/* NO INTERFACE here, see html examples for it */ # Quick tour of the main features of Condorcet PHP // I - Install use Condorcet\Condorcet; use Condorcet\Election; use Condorcet\Candidate; use Condorcet\Vote; $firstPart = ''; /* THE FOLLOWING CODE IS LIVE FOLLOWING THE FIRST PART EXPRESSED IN THE PREVIOUS FILE "1. Overview.php" */ require_once '1. Overview.php'; // VI - Play with Condorcet objects (Advanced) // Create a second election $election2 = new Election(); // Create three candidate : 'A', 'B' and 'C' for ($i = 0; $i < 3; $i++) { $election2->addCandidate(); } # Same candidate in multiple elections // Add two participating candidates from $election1 $election2->addCandidate($election1->getCandidateObjectByName('Debussy')); $election2->addCandidate($myLutoCandidate); // And, I can change again theirs name. The new name is now applied in the two elections and their votes. If namesake in another election, an exception is throw. $myLutoCandidate->setName('W.Lutoslawski'); // Have a look on $myLutoCandidate history $myLutoCandidate->getHistory(); // Have a look to a vote from $election1. The candidate name have changed. $myVote4->getContextualVote($election1, true); // In what elections, this candidates have a part ? $myLutoCandidate->getLinks(); // Get his the two Election objects $myLutoCandidate->countLinks();
declare (strict_types=1); /* NO INTERFACE here, see html examples for it */ # Quick tour of the main features of Condorcet PHP // I - Install use Condorcet\Condorcet; use Condorcet\Election; use Condorcet\Candidate; use Condorcet\Vote; require_once '../__CondorcetAutoload.php'; $start_time = microtime(TRUE); // II - Create Election $election1 = new Election(); // III - Manage Candidate # -A- Add candidates // A string $election1->addCandidate('Debussy'); $election1->addCandidate('Caplet'); $election1->addCandidate('A'); $myLutoCandidate = $election1->addCandidate('Lutoslawski'); // Return the Condorcet\Candidate object. // Automatic name $myAutomaticCandidate = $election1->addCandidate(); // Return an automatic Condorcet\Candidate $myAutomaticCandidate->getName(); // return 'B'. Because we have already register the A candidate above. // An objet $myMessiaenCandidate = new Candidate('Olivier Messiaen'); $election1->addCandidate($myMessiaenCandidate); $election1->addCandidate(new Candidate('Ligeti')); $election1->addCandidate(new Candidate('Koechlin')); # -B- Change your mind ?
<?php /* NO INTERFACE here, see html examples for it */ # Quick tour of the main features of Condorcet PHP // I - Install use Condorcet\Condorcet; use Condorcet\Election; use Condorcet\Candidate; use Condorcet\Vote; use Condorcet\DataManager\DataHandlerDrivers\PdoHandlerDriver; require_once '../../__CondorcetAutoload.php'; $start_time = microtime(TRUE); // II - Create Election $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;