php-extended / php-vote-citizen
All the standard citizens for the basic variable types php provides
Requires
- php: >=7.1
- php-extended/php-vote-object: ^3
- php-extended/polyfill-php80-stringable: >=1
Requires (Dev)
README
All the standard citizens for the basic variable types php provides
Installation
The installation of this library is made via composer.
Download composer.phar
from their website.
Then add to your composer.json :
"require": {
...
"php-extended/php-vote-citizen": "^3",
...
}
Then run php composer.phar update
to install this library.
The autoloading of all classes of this library is made through composer's autoloader.
Basic Usage
This library proposes a list of standard citizens that will vote with a predetermined behavior. They are useful to use in an election the following way :
use PhpExtended\Vote\ElectionRunner;
use PhpExtended\Vote\InvalidCandidateException;
use PhpExtended\Vote\InvalidVoteException;
use PhpExtended\Vote\UnsolvableSituationException;
use PhpExtended\Vote\UniqueStringEqualsCitizen;
$runner = new ElectionRunner();
$citizens = array();
foreach($yourObjectsThatProvideStrings as $yourObject)
{
$id = $yourObject->__toString(); // or anything else that may be useful for an id
$citizens[] = new UniqueStringEqualsCitizen($id, $yourObject->yourMethodThatReturnsString());
}
try
{
$runner->runElection($method, new ArrayIterator(array()), new ArrayIterator($citizens));
}
catch(InvalidCandidateException $ice)
{
// TODO handle the fact that one candidate is refused
}
catch(InvalidVoteException $ive)
{
// TODO handle the fact that one vote is refused
}
catch(UnsolvableSituationException $use)
{
// TODO handle the fact that an ordering between the
// candidates could not be decided by the current voting
// method and the votes that were given.
}
UniqueBooleanEqualsCitizen
This citizen proposes a candidate with only one argument which is a boolean value. This citizen ranks all the candidates with 100% if they have only one argument which is the boolean value, and 0% for anyone else.
UniqueFloatEqualsCitizen
This citizen proposes a candidate with only one argument which is a float value. This citizen ranks all the candidates with 100% if they have only one argument which is the float value, and 0% for anyone else.
UniqueIntegerEqualsCitizen
This citizen proposes a candidate with only one argument which is an integer value. This citizen ranks all the candidates with 100% if they have only one argument which is the integer value, and 0% for anyone else.
UniqueStringEqualsCitizen
This citizen proposes a candidate with only one argument which is a string value. This citizen ranks all the candidates with 100% if they have only one argument which is the string value, and 0% for anyone else.
License
MIT (See license file).