An implementation of the php-merge-interface library
An implementation of the php-merge-interface library.
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-merge-interface": "^1",
...
}
Then run php composer.phar update
to install this library.
The autoloading of all classes of this library is made through composer's autoloader.
This library may be used the following way :
use PhpExtended\Merge\Scorer;
use PhpExtended\Merge\ScoreCalculationDefinitionList;
/* @var $source \PhpExtended\Record\RecordProviderInterface */
/* @var $srcNamespace string */
/* @var $srcClassname string */
/* @var $challenger \PhpExtended\Record\AssignableRecordProviderInterface */
/* @var $chlNamespace string */
/* @var $chlClassname string */
/* @var $scoreCollectionFactory \PhpExtended\Score\ScoreCollectionFactoryInterface */
/* @var $scoreFactory \PhpExtended\Score\ScoreFactory */
/* @var $scorePolicyFactory \PhpExtended\Score\ScorePolicyFactoryInterface */
$scoreCalculationDefinitionList = new ScoreCalculationDefinitionList();
foreach(['srcAttr1' => 'dstAttr1', 'srcAttr2' => 'dstAttr2'] as $srcAttr => $dstAttr)
{
$scoreCalculationDefinition = new ScoreCalculationDefinition(
$srcAttr,
$dstAttr,
$scoreCollectionFactory,
$scoreFactory, /* the score factory may differ for each field name, but not needed every time */
$scorePolicyFactory
);
$scoreCalculationDefinitionList->append($scoreCalculationDefinition);
}
$scorer = new Scorer();
$results = $scorer->calculateScore($source, $srcNamespace, $srcClassname, $challenger, $chlNamespace, $chlClassname, $scoreCaculationDefinitionList);
foreach($results as $result)
{
/* @var $result \PhpExtended\Merge\ScoreResultInterface */
// get namespace, classname, fieldname and score
// for each $chlNamespace, $chlClassname and $dstAttr
}
use PhpExtended\Merge\Assigner;
/* @var $source \PhpExtended\Record\RecordProviderInterface */
/* @var $srcNamespace string */
/* @var $srcClassname string */
/* @var $challenger \PhpExtended\Record\AssignableRecordProviderInterface */
/* @var $chlNamespace string */
/* @var $chlClassname string */
/* @var $comparator \PhpExtended\Record\CompatorInterface */
$assigner = new Assigner();
$assignments = $assigner->doAssignments($source, $srcNamespace, $srcClassname, $challenger, $chlNamespace, $chlClassname, $comparator);
// returns the number of records that were effectively assigned
use PhpExtended\Merge\Merger;
/* @var $source \PhpExtended\Record\RecordProviderInterface */
/* @var $srcNamespace string */
/* @var $srcClassname string */
/* @var $challengerList \PhpExtended\Record\AssignableRecordProviderListInterface */
// note that the challenger list must be created with all the available challengers
// to avoid at most the unsolvable elections
/* @var $chlNamespace string */
/* @var $chlClassname string */
// warning : the challengers should be normalized (via an interface if possible)
/* @var $votingMethodFactory \PhpExtended\Vote\VotingMethodFactoryInterface */
/* @var $citizenFactory \PhpExtended\Vote\CitizenFactoryInterface */
/* @var $candidateFactory \PhpExtended\Vote\CandidateFactoryInterface */
/* @var $argumentFactory \PhpExtended\Vote\ArgumentFactoryInterface */
/* @var $biasFactory \PhpExtended\Vote\BiasFactoryInterface */
/* @var $electionRunnerFactory \PhpExtended\Vote\ElectionRunnerFactoryInterface */
$mergeCalculationDefintitionList = new MergeCalculationDefinitionList();
foreach(['srcAttr1' => 'dstAttr1', 'srcAttr2' => 'dstAttr2'] as $srcAttr => $dstAttr)
{
$mergeCalculationDefinition = new MergeCalculationDefinition(
$srcAttr,
$dstAttr, // warning, should be the same for each challenger in the list
$votingMethodFactory,
$citizenFactory, // warning, should be the same for each challenger in the list
$candidateFactory,
$argumentFactory,
$biasFactory, // warning, should be the same for each challenger in the list
$electionRunnerFactory
);
$mergeCalculationDefinitionList->append($mergeCalculationDefinition);
}
$merger = new Merger();
$informations = $merger->doMerge($source, $srcNamespace, $srcClassname, $challengerList, $chlNamespace, $chlClassname, $mergeCalculationDefinitionList);
foreach($informations as $information)
{
/* @var $information \PhpExtended\Information\InformationInterface */
// get the $srcNamespace, $srcClassname, $sourceId and $srcFieldName
// alongside with the $value
}
MIT (See license file).