php-extended/php-vote-object

实现php-vote-interface包的库


README

实现php-vote-object包的库

coverage build status

安装

此库的安装是通过composer完成的,所有类的自动加载也是通过它们的自动加载器完成的。

  • 他们的网站下载composer.phar
  • 然后运行以下命令将此库作为依赖项安装
  • php composer.phar php-extended/php-vote-object ^7

基本用法

要运行此库,您至少需要另一个库,该库提供一种实现PhpExtended\Vote\VotingMethodInterface接口的特定投票方法。

此外,还可以使用一个提供在选举运行前修改投票偏见的额外库。这些偏见必须实现PhpExtended\Vote\BiasInterface接口。

最后,投票系统的核心是投票,因此您必须提供一个库,该库为投票过程创建公民。这些公民可以对任何候选人进行投票和排名。所有公民都必须实现PhpExtended\Vote\CitizenInterface接口。这些公民非常重要,它们提供候选人和他们的论据。他们的论据是不同公民在选择投票顺序时将要检查的内容,因此公民方法必须是一致的!

一旦所有部件都组合在一起,可以使用以下方式使用该引擎


use PhpExtended\Vote\ElectionRunner;
use PhpExtended\Vote\InvalidCandidateException;
use PhpExtended\Vote\InvalidVoteException;
use PhpExtended\Vote\UnsolvableSituationException;

$runner = new ElectionRunner();
$biases = new ArrayIterator(array(
	$yourBiasedObjects,	// each implement BiasInterface
));
$citizens = new ArrayIterator(array(
	$yourCitizenObjects,	// each implement CitizenInterface
));
$method = $theChoosenMethod;	// implements VotingMethodInterface

try
{
	$result = $runner->runElection($method, $biases, $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.
}

foreach($result->getRankings() as $individualResult)
{
	// The rankings are given in order of importance, winner
	// first and loser last
	/* @var $individualResult \PhpExtended\Vote\ElectionResultInterface */
	$candidate = $individualResult->getCandidate();
	// here the candidate is one of the candidate object that
	// was given by one of your citizens, and you retrieve
	// its objects within the arguments of the citizen.
}

此库提供了一系列标准公民,他们将根据预定的行为进行投票。它们在选举中使用以下方式很有用


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

此公民提出一个只有布尔值作为论据的候选人。如果候选人只有一个布尔值论据,则此公民将对所有候选人排名100%,对其他人排名0%。

UniqueFloatEqualsCitizen

此公民提出一个只有浮点值作为论据的候选人。如果候选人只有一个浮点值论据,则此公民将对所有候选人排名100%,对其他人排名0%。

UniqueIntegerEqualsCitizen

此公民提出一个只有整数值作为论据的候选人。如果候选人只有一个整数值论据,则此公民将对所有候选人排名100%,对其他人排名0%。

UniqueStringEqualsCitizen

此公民提出一个只有字符串值作为论据的候选人。如果候选人只有一个字符串值论据,则此公民将对所有候选人排名100%,对其他人排名0%。

许可

MIT (查看许可文件)。