Skip to content

Commit

Permalink
Merge pull request #41 from rplansky/has-intersection-operator
Browse files Browse the repository at this point in the history
New has-intersection operator
  • Loading branch information
othillo authored Nov 17, 2017
2 parents 8ce4da2 + 082c430 commit 1380d55
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/Qandidate/Toggle/Operator/HasIntersection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the qandidate/toggle package.
*
* (c) Qandidate.com <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Qandidate\Toggle\Operator;

class HasIntersection extends EqualityOperator
{
private $values;

/**
* @param array $values
*/
public function __construct(array $values)
{
$this->values = $values;
}

/**
* {@inheritdoc}
*/
public function appliesTo($argument)
{
return null !== $argument
&& array_intersect($argument, $this->values);
}

/**
* @return array
*/
public function getValues()
{
return $this->values;
}
}
7 changes: 7 additions & 0 deletions lib/Qandidate/Toggle/Serializer/OperatorSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Qandidate\Toggle\Operator\EqualTo;
use Qandidate\Toggle\Operator\GreaterThan;
use Qandidate\Toggle\Operator\GreaterThanEqual;
use Qandidate\Toggle\Operator\HasIntersection;
use Qandidate\Toggle\Operator\InSet;
use Qandidate\Toggle\Operator\LessThan;
use Qandidate\Toggle\Operator\LessThanEqual;
Expand All @@ -41,6 +42,8 @@ public function serialize(Operator $operator)
return array('name' => 'greater-than', 'value' => $operator->getValue());
case $operator instanceof GreaterThanEqual:
return array('name' => 'greater-than-equal', 'value' => $operator->getValue());
case $operator instanceof HasIntersection:
return array('name' => 'has-intersection', 'values' => $operator->getValues());
case $operator instanceof InSet:
return array('name' => 'in-set', 'values' => $operator->getValues());
case $operator instanceof LessThan:
Expand Down Expand Up @@ -79,6 +82,10 @@ public function deserialize(array $operator)
$this->assertHasKey('value', $operator);

return new GreaterThanEqual($operator['value']);
case 'has-intersection':
$this->assertHasKey('values', $operator);

return new HasIntersection($operator['values']);
case 'in-set':
$this->assertHasKey('values', $operator);

Expand Down
65 changes: 65 additions & 0 deletions test/Qandidate/Toggle/Operator/HasIntersectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of the qandidate/toggle package.
*
* (c) Qandidate.com <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Qandidate\Toggle\Operator;

use Qandidate\Toggle\TestCase;

class HasIntersectionTest extends TestCase
{
/**
* @test
* @dataProvider valuesNotMatching
*/
public function it_not_applies_to_set_not_matching_values($values, $argument)
{
$operator = new HasIntersection($values);
$this->assertFalse($operator->appliesTo($argument));
}

public function valuesNotMatching()
{
return array(
array([4], [1, 2, 3]),
array([5, 6], [1, 2, 3]),
array(['foo'], ['qux', 'bar']),
);
}

/**
* @test
* @dataProvider valuesMatching
*/
public function it_applies_to_set_matching_values($values, $argument)
{
$operator = new HasIntersection($values);
$this->assertTrue ($operator->appliesTo($argument));
}

public function valuesMatching()
{
return array(
array([1], [1, 2, 3]),
array([2], [1, 2, 3]),
array([3, 2], [1, 2, 3]),
array(['bar'], ['foo', 'bar']),
);
}

/**
* @test
*/
public function it_exposes_its_value()
{
$operator = new HasIntersection(['a', 'b', 'c']);
$this->assertEquals(['a', 'b', 'c'], $operator->getValues());
}
}
3 changes: 3 additions & 0 deletions test/Qandidate/Toggle/Serializer/OperatorSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Qandidate\Toggle\Operator;
use Qandidate\Toggle\Operator\GreaterThan;
use Qandidate\Toggle\Operator\GreaterThanEqual;
use Qandidate\Toggle\Operator\HasIntersection;
use Qandidate\Toggle\Operator\InSet;
use Qandidate\Toggle\Operator\LessThan;
use Qandidate\Toggle\Operator\LessThanEqual;
Expand Down Expand Up @@ -53,6 +54,7 @@ public function knownOperators()
array(new LessThan(42), array('name' => 'less-than', 'value' => 42)),
array(new LessThanEqual(42), array('name' => 'less-than-equal', 'value' => 42)),
array(new Percentage(42, 5), array('name' => 'percentage', 'percentage' => 42, 'shift' => 5)),
array(new HasIntersection(array(1, 2, 3)), array('name' => 'has-intersection', 'values' => array(1, 2, 3))),
array(new InSet(array(1, 2, 3)), array('name' => 'in-set', 'values' => array(1, 2, 3))),
);
}
Expand Down Expand Up @@ -93,6 +95,7 @@ public function missingKeys()
array(array('name' => 'percentage', 'percentage' => 42)),
array(array('name' => 'percentage', 'shift' => 5)),
array(array('name' => 'in-set')),
array(array('name' => 'has-intersection')),
);
}

Expand Down

0 comments on commit 1380d55

Please sign in to comment.