Skip to content

Commit 903caa2

Browse files
authored
Merge pull request #58 from RhubarbPHP/feature/ListContainsFilter
ListContains Filter
2 parents ad76eaa + fd2200a commit 903caa2

5 files changed

Lines changed: 253 additions & 1 deletion

File tree

src/Filters/ListContains.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/*
3+
* Copyright 2017 RhubarbPHP
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Rhubarb\Stem\Filters;
19+
20+
use Rhubarb\Stem\Models\Model;
21+
22+
class ListContains extends ColumnFilter
23+
{
24+
/**
25+
* @var null|array
26+
*/
27+
public $contains = null;
28+
29+
public function __construct($columnName, array $contains)
30+
{
31+
parent::__construct($columnName);
32+
33+
$this->contains = $contains;
34+
}
35+
36+
public function evaluate(Model $model)
37+
{
38+
$placeHolder = $this->detectPlaceHolder($this->contains);
39+
40+
if ($placeHolder) {
41+
$contains = $model[$placeHolder];
42+
} else {
43+
$contains = $this->contains;
44+
}
45+
46+
$contains = $this->getTransformedComparisonValue($contains, $model);
47+
48+
foreach ($contains as $contain) {
49+
if (in_array($contain, $model[$this->columnName]) === false) {
50+
return true;
51+
}
52+
}
53+
54+
return false;
55+
}
56+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/*
3+
* Copyright 2017 RhubarbPHP
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace Rhubarb\Stem\Repositories\MySql\Filters;
18+
19+
use Rhubarb\Stem\Collections\Collection;
20+
use Rhubarb\Stem\Filters\Filter;
21+
use Rhubarb\Stem\Filters\ListContains;
22+
use Rhubarb\Stem\Repositories\Repository;
23+
use Rhubarb\Stem\Sql\WhereExpressionCollector;
24+
25+
class MySqlListContains extends ListContains
26+
{
27+
use MySqlFilterTrait;
28+
29+
public static function fromGenericFilter(Filter $filter)
30+
{
31+
/**
32+
* @var ListContains $filter
33+
*/
34+
return new static($filter->columnName, $filter->contains);
35+
}
36+
37+
/**
38+
* Returns the SQL fragment needed to filter where a column equals a given value.
39+
*
40+
* @param Collection $collection
41+
* @param Repository $repository
42+
* @param WhereExpressionCollector $whereExpressionCollector
43+
* @param array $params
44+
* @return bool
45+
*/
46+
protected function doFilterWithRepository(
47+
Collection $collection,
48+
Repository $repository,
49+
WhereExpressionCollector $whereExpressionCollector,
50+
&$params
51+
) {
52+
$filtered = true;
53+
54+
foreach ($this->contains as $contain) {
55+
$currentFiltered = $this->createColumnWhereClauseExpression(
56+
"LIKE",
57+
[$contain],
58+
$collection,
59+
$repository,
60+
$whereExpressionCollector,
61+
$params);
62+
63+
$keys = array_keys($params);
64+
65+
$end = end($keys);
66+
67+
$params[$end] = "%{$params[$end]}%";
68+
69+
if ($filtered && !$currentFiltered) {
70+
$filtered = false;
71+
}
72+
}
73+
74+
75+
return $filtered;
76+
}
77+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Rhubarb\Stem\Tests\unit\Filters;
4+
5+
use Rhubarb\Stem\Collections\RepositoryCollection;
6+
use Rhubarb\Stem\Filters\ListContains;
7+
use Rhubarb\Stem\Tests\unit\Fixtures\ModelUnitTestCase;
8+
use Rhubarb\Stem\Tests\unit\Fixtures\TestContact;
9+
10+
class ListContainsTest extends ModelUnitTestCase
11+
{
12+
/**
13+
* @var RepositoryCollection
14+
*/
15+
private $list;
16+
17+
protected function setUp()
18+
{
19+
parent::setUp();
20+
21+
$example = new TestContact();
22+
$example->getRepository()->clearObjectCache();
23+
$example->Forename = "John";
24+
$example->AgesOfFriends = [21,24,15,43,12];
25+
$example->save();
26+
27+
$example = new TestContact();
28+
$example->Forename = "Mary";
29+
$example->AgesOfFriends = [21,24,31,43,12];
30+
$example->save();
31+
32+
$example = new TestContact();
33+
$example->Forename = "Tom";
34+
$example->Surname = "Thumb";
35+
$example->AgesOfFriends = [21,24,31,43,12];
36+
$example->save();
37+
38+
$this->list = new RepositoryCollection(TestContact::class);
39+
}
40+
41+
public function testFilterMatchesRows()
42+
{
43+
$this->assertCount(3, $this->list);
44+
45+
$filter = new ListContains('AgesOfFriends', [15]);
46+
$this->list->replaceFilter($filter);
47+
48+
$this->assertCount(1, $this->list);
49+
50+
$filter = new ListContains('AgesOfFriends', [21,31]);
51+
$this->list->replaceFilter($filter);
52+
53+
$this->assertCount(2, $this->list);
54+
}
55+
}

tests/unit/Fixtures/TestContact.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Rhubarb\Stem\Models\Model;
66
use Rhubarb\Stem\Schema\Columns\AutoIncrementColumn;
77
use Rhubarb\Stem\Schema\Columns\BooleanColumn;
8+
use Rhubarb\Stem\Schema\Columns\CommaSeparatedListColumn;
89
use Rhubarb\Stem\Schema\Columns\DateColumn;
910
use Rhubarb\Stem\Schema\Columns\DateTimeColumn;
1011
use Rhubarb\Stem\Schema\Columns\DecimalColumn;
@@ -21,6 +22,7 @@
2122
* @property string $Forename
2223
* @property string $Surname
2324
* @property \Date $DateOfBirth
25+
* @property int[] $AgesOfFriends
2426
*/
2527
class TestContact extends Model
2628
{
@@ -42,7 +44,8 @@ protected function createSchema()
4244
new DecimalColumn("CreditLimit", 10, 2),
4345
new DecimalColumn("Balance", 8, 4),
4446
new StringColumn("Postcode", 50),
45-
new StringColumn("AddressLine1", 50)
47+
new StringColumn("AddressLine1", 50),
48+
new CommaSeparatedListColumn('AgesOfFriends', 500, [], true)
4649
);
4750

4851
$schema->uniqueIdentifierColumnName = "ContactID";
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Rhubarb\Stem\Tests\unit\Filters;
4+
5+
6+
use Rhubarb\Stem\Collections\RepositoryCollection;
7+
use Rhubarb\Stem\Filters\ListContains;
8+
use Rhubarb\Stem\Repositories\MySql\MySql;
9+
use Rhubarb\Stem\Tests\unit\Fixtures\TestContact;
10+
use Rhubarb\Stem\Tests\unit\Repositories\MySql\MySqlTestCase;
11+
12+
class MySqlListContainsTest extends MySqlTestCase
13+
{
14+
/**
15+
* @var RepositoryCollection
16+
*/
17+
private $list;
18+
19+
protected function setUp()
20+
{
21+
parent::setUp();
22+
23+
MySql::executeStatement("TRUNCATE TABLE tblContact");
24+
25+
$example = new TestContact();
26+
$example->getRepository()->clearObjectCache();
27+
$example->Forename = "John";
28+
$example->AgesOfFriends = [21,24,15,43,12];
29+
$example->save();
30+
31+
$example = new TestContact();
32+
$example->Forename = "Mary";
33+
$example->AgesOfFriends = [21,24,31,43,12];
34+
$example->save();
35+
36+
$example = new TestContact();
37+
$example->Forename = "Tom";
38+
$example->Surname = "Thumb";
39+
$example->AgesOfFriends = [21,24,31,43,12];
40+
$example->save();
41+
42+
$this->list = TestContact::find();
43+
}
44+
45+
public function testFilterMatchesRows()
46+
{
47+
$this->assertCount(3, $this->list);
48+
49+
$filter = new ListContains('AgesOfFriends', [15]);
50+
$this->list->replaceFilter($filter);
51+
52+
$this->assertCount(1, $this->list);
53+
54+
$filter = new ListContains('AgesOfFriends', [21,31]);
55+
$this->list->replaceFilter($filter);
56+
57+
$this->assertCount(2, $this->list);
58+
59+
$this->assertTrue($this->list->getFilter()->wasFilteredByRepository());
60+
}
61+
}

0 commit comments

Comments
 (0)