Candidates
Registering
Regular
Reference
php
use CondorcetPHP\Condorcet\Candidate;
// From string, returns the newly built candidate object
$election->addCandidate('Wagner');
// Directly from a Candidate Object
$election->addCandidate(new Candidate('Edgard Varèse'));
// Empty argument will return a candidate object with an automatic name for you (From A to ZZZZZ)
$myAutoCandidate = $election->addCandidate();
// If you use an integer, it will be converted to string (= '2')
$election->addCandidate(2);
Text File
Reference
txt
CandidateA
CandidateB # You can add comments
CandidateC ; CandidateD # Or in the same line separated by ; with or without space (will be trimmed)
CandidateE
php
$election1->parseCandidates('candidates.txt', isFile: true); // Path to text file. Absolute or relative.
$election2->parseCandidates($myCandidatesToParse); // A string
Json
Reference
php
$json = json_encode([
'CandidateName1',
'CandidateName2'
]);
$election->addCandidatesFromJson($json);
Removing
Reference
php
use CondorcetPHP\Condorcet\Candidate;
$election->addCandidate('Wagner');
$debussyCandidate = new Candidate('Debussy');
$election->addCandidate($debussyCandidate);
$election->removeCandidates('Wagner');
$election->removeCandidates($debussyCandidate); // Does not destroy your Candidate object if it exists outside of the election object scope. It just unlinks it from this Election.
Verify the candidates list
php
$election->getCandidatesList(); // Will return an array with all Candidate objects.
$election->getCandidatesListAsString(); // Will return an array with all candidate names as strings.
php
use CondorcetPHP\Condorcet\Candidate;
use CondorcetPHP\Condorcet\Election;
$election = new Election(); // Create a new election
$candidate = new Candidate('Wagner'); // Add a candidate to the election
expect($election->canAddCandidate($candidate))->toBeTrue(); // true: the candidate object and the candidate string name are available.
expect($election->hasCandidate($candidate))->toBeFalse(); // false: the candidate object and the candidate string name are available.
$election->addCandidate($candidate); // Finally add the candidate to the election
expect($election->hasCandidate($candidate))->toBeTrue();
expect($election->hasCandidate(candidate: 'Wagner', strictMode: false))->toBeTrue(); // true: the candidate object and the candidate string name are available.