Skip to content

Commit

Permalink
Fixes original order for some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbalcytis committed Jul 31, 2015
1 parent 0078924 commit 07cdd32
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Condition/ConditionalValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@ public function __construct(
public function resolveValue(array $conditionalValues, $default = null)
{
$priorityResolver = $this->conditionPriorityResolver;
usort($conditionalValues, function(ConditionalValue $a, ConditionalValue $b) use ($priorityResolver) {

$order = new \SplObjectStorage();
$i = 0;
foreach ($conditionalValues as $value) {
$order->attach($value, $i++);
}

usort($conditionalValues, function(ConditionalValue $a, ConditionalValue $b) use ($priorityResolver, $order) {
$r = $priorityResolver->getConditionPriority($a->getTimeCondition());
$r -= $priorityResolver->getConditionPriority($b->getTimeCondition());

if ($r === 0) {
return $order->offsetGet($a) - $order->offsetGet($b);
}
return -$r; // negate result as we need from biggest to smallest
});

Expand Down
7 changes: 7 additions & 0 deletions test/Condition/ConditionalValueResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ public function dataProvider()
$this->buildConditionalValue('b', 1, true),
$this->buildConditionalValue('c', 2, true),
), 'default'),
'gives first matching if priority is the same #2' => array('c', array(
$this->buildConditionalValue('a', 1, false),
$this->buildConditionalValue('b', 2, false),
$this->buildConditionalValue('c', 3, true),
$this->buildConditionalValue('d', 3, true),
$this->buildConditionalValue('e', 3, true),
), 'default'),
'gives default on no conditions' => array('default', array(), 'default'),
'gives default if none matches' => array('default', array(
$this->buildConditionalValue('a', 2, false),
Expand Down

0 comments on commit 07cdd32

Please sign in to comment.