Skip to content

Add Votes

Note: You can add new votes after the results have already been given.

Vote by vote

With a string

You can do the following:

php
$election->parseCandidates('A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z');

$election->addVote('A>B=C=H>G=T>Q');

// It works with spaces, if you want to be less strict...
$election->addVote('A> B = C=H >G=T > Q');

// But you cannot use the '<' operator
$vote = 'A<B<C'; // This is not correct
// The following won't work either
$vote = 'A>BC>D'; // This is not correct

The last rank is optional, it will be automatically deduced.

With an array

php
use CondorcetPHP\Condorcet\Candidate;

$pucciniCandidate = new Candidate('Puccini');
$election->parseCandidates('Wagner;Debussy;Varese');
$election->addCandidate($pucciniCandidate);

$vote = [];
$vote[1] = 'Wagner';
$vote[2] = 'Debussy';
$vote[3] = $pucciniCandidate; // can be a string or a candidate object
$vote[4] = 'Varese'; // The last rank is optional
$election->addVote($vote);

// Use commas or an array in the case of a tie:
$vote = [];
$vote[1] = 'Debussy,Wagner';
$vote[2] = [$pucciniCandidate, 'Varese'];
$election->addVote($vote);

The last rank is optional, it will be automatically deduced.

With Vote object

You can also use Vote objects for more advanced usage:

php
use CondorcetPHP\Condorcet\Election;
use CondorcetPHP\Condorcet\Vote;

$election = new Election;
$election->parseCandidates('A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T');

$vote1 = new Vote('A>B=C=H>G=T>Q');
$vote2 = new Vote([
    1 => 'A',
    2 => 'C',
    3 => 'B',
    4 => ['H', 'G']
]);

$vote3 = new Vote([
    1 => 'A',
    2 => $election->getCandidateObjectFromName('B'),
    3 => 'C' // Condorcet will do the job for you.
]);

$election->addVote($vote1);
$election->addVote($vote2);
$election->addVote($vote3);

Add multiple votes from string or text file

Native format

Once your list of candidates has been previously recorded, you can parse a text file or a PHP string to record a large number of votes at once.

You can combine this method with the traditional PHP calls above.

Syntax

txt
tag1,tag2,tag3[...] || A>B=D>C # A comment at the end of the line prefixed by '#'.
Titan,CoteBoeuf || A>B=D>C # Tags at first, vote at second, separated by '||'
A>C>D>B # Line break to start a new vote. Tags are optional. See above for vote syntax.
tag1,tag2,tag3 || A>B=D>C * 5 # This vote and its tags will be registered 5 times
   tag1  ,  tag2, tag3     ||    A>B=D>C*2        # Works too.
C>D>B*8;A=D>B;Julien,Christelle||A>C>D>B*4;D=B=C>A # Alternatively, you can replace the line break by a semicolon.

Method

php
$election->parseCandidates('A;B;C;D');

$election->parseVotes('votes.txt', isFile: true); // Path to text file. Absolute or relative.
$election->parseVotes($myVotesToParse); // Just a vote string

From Json

Syntax

In this example, all parameters are optional except vote.

  • 'multi' is used to record N times the vote.
  • 'tag' is used in the same way as addVote()
  • 'vote' is used in the same way as addVote()

Method

php
$election->parseCandidates('A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z');

$json_votes = json_encode( [
	['vote' => 'A>B=D>C', 'tag' => 'ben,jerry'],
	['vote' => ['D', 'B,A', 'C'], 'tag' => ['bela','bartok'], 'multi' => 5],
	['vote' => ['A', ['B','C'], 'D']]
] );

$election->addVotesFromJson($json_votes);

Election files formats

Look at the dedicated section.

Prevent flooding

Applied and reset after each call to parseVotes() or addVotesFromJson()

php
use CondorcetPHP\Condorcet\Election;

Election::$maxParseIteration = 500; // Will generate an exception and stop after 500 registered votes by call. No votes will be registered.
Election::$maxParseIteration = null; // No limit (default mode)

Restrict the number of possible votes for one election

Note: By default, there is no limit

php
use CondorcetPHP\Condorcet\Election;

Election::$maxVotePerElection = 2042; // All elections, new or woken up, will be limited to this maximum vote number.
Election::$maxVotePerElection = null; // No limit for everybody. (Default)

Released under the MIT License.