php-extended / php-vote-object
实现php-vote-interface包的库
Requires
- php: >=8.0
- php-extended/php-score-object: ^7
- php-extended/php-vote-interface: ^7
Requires (Dev)
- dev-master
- 7.0.6
- 7.0.5
- 7.0.4
- 7.0.3
- 7.0.2
- 7.0.1
- 7.0.0
- 6.1.4
- 6.1.3
- 6.1.2
- 6.1.1
- 6.1.0
- 6.0.3
- 6.0.2
- 6.0.1
- 6.0.0
- 5.0.1
- 5.0.0
- 4.0.2
- 4.0.1
- 4.0.0
- 3.2.8
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.17
- 3.1.16
- 3.1.15
- 3.1.14
- 3.1.13
- 3.1.12
- 3.1.11
- 3.1.10
- 3.1.9
- 3.1.8
- 3.1.7
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2024-08-31 11:35:29 UTC
README
实现php-vote-object包的库
安装
此库的安装是通过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 (查看许可文件)。