-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from rplansky/has-intersection-operator
New has-intersection operator
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters