Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,6 +18,7 @@
*
* @author Lee Siong Chan <ahlee2326@me.com>
* @author Alan Poulain <contact@alanpoulain.eu>
* @author Samuel Chiriluta <samuel4x4@gmail.com>
*/
interface RangeFilterInterface
{
Expand All@@ -26,4 +27,5 @@ interface RangeFilterInterface
public const PARAMETER_GREATER_THAN_OR_EQUAL = 'gte';
public const PARAMETER_LESS_THAN = 'lt';
public const PARAMETER_LESS_THAN_OR_EQUAL = 'lte';
public const PARAMETER_NOT_EQUAL = 'ne';
}
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@
*
* @author Lee Siong Chan <ahlee2326@me.com>
* @author Alan Poulain <contact@alanpoulain.eu>
* @author Samuel Chiriluta <samuel4x4@gmail.com>
*/
trait RangeFilterTrait
{
Expand DownExpand Up@@ -49,6 +50,7 @@ public function getDescription(string $resourceClass): array
$description += $this->getFilterDescription($property, self::PARAMETER_GREATER_THAN_OR_EQUAL);
$description += $this->getFilterDescription($property, self::PARAMETER_LESS_THAN);
$description += $this->getFilterDescription($property, self::PARAMETER_LESS_THAN_OR_EQUAL);
$description += $this->getFilterDescription($property, self::PARAMETER_NOT_EQUAL);
}

return $description;
Expand DownExpand Up@@ -78,7 +80,7 @@ protected function getFilterDescription(string $fieldName, string $operator): ar

private function normalizeValues(array $values, string $property): ?array
{
$operators = [self::PARAMETER_BETWEEN, self::PARAMETER_GREATER_THAN, self::PARAMETER_GREATER_THAN_OR_EQUAL, self::PARAMETER_LESS_THAN, self::PARAMETER_LESS_THAN_OR_EQUAL];
$operators = [self::PARAMETER_BETWEEN, self::PARAMETER_GREATER_THAN, self::PARAMETER_GREATER_THAN_OR_EQUAL, self::PARAMETER_LESS_THAN, self::PARAMETER_LESS_THAN_OR_EQUAL, self::PARAMETER_NOT_EQUAL];

foreach ($values as $operator => $value) {
if (!\in_array($operator, $operators, true)) {
Expand Down
10 changes: 10 additions & 0 deletions src/Core/Bridge/Doctrine/MongoDbOdm/Filter/RangeFilter.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,6 +24,7 @@
*
* @author Lee Siong Chan <ahlee2326@me.com>
* @author Alan Poulain <contact@alanpoulain.eu>
* @author Samuel Chiriluta <samuel4x4@gmail.com>
*/
final class RangeFilter extends AbstractFilter implements RangeFilterInterface
{
Expand DownExpand Up@@ -122,6 +123,15 @@ protected function addMatch(Builder $aggregationBuilder, string $field, string $

$aggregationBuilder->match()->field($matchField)->lte($value);

break;
case self::PARAMETER_NOT_EQUAL:
$value = $this->normalizeValue($value, $operator);
if (null === $value) {
return;
}

$aggregationBuilder->match()->field($matchField)->notEqual($value);

break;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/Core/Bridge/Doctrine/Orm/Filter/RangeFilter.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@
* Filters the collection by range.
*
* @author Lee Siong Chan <ahlee2326@me.com>
* @author Samuel Chiriluta <samuel4x4@gmail.com>
*
* @final
*/
Expand DownExpand Up@@ -144,6 +145,17 @@ protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterf
->andWhere(sprintf('%s.%s <= :%s', $alias, $field, $valueParameter))
->setParameter($valueParameter, $value);

break;
case self::PARAMETER_NOT_EQUAL:
$value = $this->normalizeValue($value, $operator);
if (null === $value) {
return;
}

$queryBuilder
->andWhere(sprintf('%s.%s <> :%s', $alias, $field, $valueParameter))
->setParameter($valueParameter, $value);

break;
}
}
Expand Down
95 changes: 95 additions & 0 deletions tests/Bridge/Doctrine/MongoDbOdm/Filter/RangeFilterTest.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
* @group mongodb
*
* @author Alan Poulain <contact@alanpoulain.eu>
* @author Samuel Chiriluta <samuel4x4@gmail.com>
*/
class RangeFilterTest extends DoctrineMongoDbOdmFilterTestCase
{
Expand DownExpand Up@@ -58,6 +59,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'id[ne]' => [
'property' => 'id',
'type' => 'string',
'required' => false,
],
'name[between]' => [
'property' => 'name',
'type' => 'string',
Expand All@@ -83,6 +89,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'name[ne]' => [
'property' => 'name',
'type' => 'string',
'required' => false,
],
'alias[between]' => [
'property' => 'alias',
'type' => 'string',
Expand All@@ -108,6 +119,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'alias[ne]' => [
'property' => 'alias',
'type' => 'string',
'required' => false,
],
'description[between]' => [
'property' => 'description',
'type' => 'string',
Expand All@@ -133,6 +149,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'description[ne]' => [
'property' => 'description',
'type' => 'string',
'required' => false,
],
'dummy[between]' => [
'property' => 'dummy',
'type' => 'string',
Expand All@@ -158,6 +179,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'dummy[ne]' => [
'property' => 'dummy',
'type' => 'string',
'required' => false,
],
'dummyDate[between]' => [
'property' => 'dummyDate',
'type' => 'string',
Expand All@@ -183,6 +209,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'dummyDate[ne]' => [
'property' => 'dummyDate',
'type' => 'string',
'required' => false,
],
'dummyFloat[between]' => [
'property' => 'dummyFloat',
'type' => 'string',
Expand All@@ -208,6 +239,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'dummyFloat[ne]' => [
'property' => 'dummyFloat',
'type' => 'string',
'required' => false,
],
'dummyPrice[between]' => [
'property' => 'dummyPrice',
'type' => 'string',
Expand All@@ -233,6 +269,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'dummyPrice[ne]' => [
'property' => 'dummyPrice',
'type' => 'string',
'required' => false,
],
'jsonData[between]' => [
'property' => 'jsonData',
'type' => 'string',
Expand All@@ -258,6 +299,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'jsonData[ne]' => [
'property' => 'jsonData',
'type' => 'string',
'required' => false,
],
'arrayData[between]' => [
'property' => 'arrayData',
'type' => 'string',
Expand All@@ -283,6 +329,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'arrayData[ne]' => [
'property' => 'arrayData',
'type' => 'string',
'required' => false,
],
'nameConverted[between]' => [
'property' => 'nameConverted',
'type' => 'string',
Expand All@@ -308,6 +359,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'nameConverted[ne]' => [
'property' => 'nameConverted',
'type' => 'string',
'required' => false,
],
'dummyBoolean[between]' => [
'property' => 'dummyBoolean',
'type' => 'string',
Expand All@@ -333,6 +389,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'dummyBoolean[ne]' => [
'property' => 'dummyBoolean',
'type' => 'string',
'required' => false,
],
'relatedDummy[between]' => [
'property' => 'relatedDummy',
'type' => 'string',
Expand All@@ -358,6 +419,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'relatedDummy[ne]' => [
'property' => 'relatedDummy',
'type' => 'string',
'required' => false,
],
'relatedDummies[between]' => [
'property' => 'relatedDummies',
'type' => 'string',
Expand All@@ -383,6 +449,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'relatedDummies[ne]' => [
'property' => 'relatedDummies',
'type' => 'string',
'required' => false,
],
'relatedOwnedDummy[between]' => [
'property' => 'relatedOwnedDummy',
'type' => 'string',
Expand All@@ -408,6 +479,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'relatedOwnedDummy[ne]' => [
'property' => 'relatedOwnedDummy',
'type' => 'string',
'required' => false,
],
'relatedOwningDummy[between]' => [
'property' => 'relatedOwningDummy',
'type' => 'string',
Expand All@@ -433,6 +509,11 @@ public function testGetDescriptionDefaultFields()
'type' => 'string',
'required' => false,
],
'relatedOwningDummy[ne]' => [
'property' => 'relatedOwningDummy',
'type' => 'string',
'required' => false,
],
], $filter->getDescription($this->resourceClass));
}

Expand DownExpand Up@@ -545,6 +626,20 @@ public function provideApplyTestData(): array
],
],
],
'ne' => [
[
[
'$match' => [
'dummyPrice' => [
'$notEqual' => 9.99,
],
],
],
],
],
'ne (non-numeric)' => [
[],
],
]
);
}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,6 +15,7 @@

/**
* @author Lee Siong Chan <ahlee2326@me.com>
* @author Samuel Chiriluta <samuel4x4@gmail.com>
*/
trait RangeFilterTestTrait
{
Expand DownExpand Up@@ -134,6 +135,22 @@ private function provideApplyTestArguments(): array
],
],
],
'ne' => [
null,
[
'dummyPrice' => [
'ne' => '9.99',
],
],
],
'ne (non-numeric)' => [
null,
[
'dummyPrice' => [
'ne' => '127.0.0.1',
],
],
],
];
}
}
Loading