Skip to content

Commit

Permalink
Add regex type
Browse files Browse the repository at this point in the history
  • Loading branch information
zidar-bot committed Aug 8, 2023
1 parent fec88ae commit 7e9c06b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/SearchParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public function __construct(
public ?SearchParamDirection $direction = null,
public ?string $field = null,
public ?array $callable = null,
public ?bool $invert = null
public ?bool $invert = null,
public ?string $flags = null
)
{
}
Expand Down
10 changes: 10 additions & 0 deletions src/SearchParamParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\ODM\MongoDB\Query\Builder;
use IntBackedEnum;
use InvalidArgumentException;
use MongoDB\BSON\Regex;
use ReflectionClass;
use ReflectionException;
use StringBackedEnum;
Expand Down Expand Up @@ -47,6 +48,7 @@ public static function parse($filter, Builder|MatchStage $builder): void
SearchParamType::RangeFloat => self::setRangeFloat($attribute, $builder, $field, $value),
SearchParamType::RangeInt => self::setRangeInt($attribute, $builder, $field, $value),
SearchParamType::RangeIntEnum => self::setRangeIntEnum($attribute, $builder, $field, $value),
SearchParamType::Regex => self::setRegex($attribute, $builder, $field, $value),
SearchParamType::String => self::setString($attribute, $builder, $field, $value),
SearchParamType::StringArray => self::setStringArray($attribute, $builder, $field, $value),
SearchParamType::StringEnum => self::setStringEnum($attribute, $builder, $field, $value),
Expand Down Expand Up @@ -197,6 +199,14 @@ private static function setRangeIntEnum(SearchParam $attribute, Builder|MatchSta
: $builder->field($field)->lte((int)$value->value);
}

private static function setRegex(SearchParam $attribute, Builder|MatchStage $builder, string $field, mixed $value): void
{
if ($value)
{
$builder->field($field)->equals(new Regex($value, $attribute->flags ?? ''));
}
}

private static function setString(SearchParam $attribute, Builder|MatchStage $builder, string $field, $value): void
{
if ($attribute->invert)
Expand Down
1 change: 1 addition & 0 deletions src/SearchParamType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum SearchParamType
case RangeFloat;
case RangeInt;
case RangeIntEnum;
case Regex;
case String;
case StringArray;
case StringEnum;
Expand Down
3 changes: 3 additions & 0 deletions tests/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class SearchFilter
#[SearchParam(type: SearchParamType::RangeIntEnum, direction: SearchParamDirection::To)]
public ?IntEnum $rangeIntEnumToProperty = null;

#[SearchParam(type: SearchParamType::Regex, flags: 'i')]
public ?string $regexProperty = null;

#[SearchParam(type: SearchParamType::String)]
public ?string $stringProperty = null;

Expand Down
17 changes: 17 additions & 0 deletions tests/SearchParamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nebkam\OdmSearchParam\Tests;

use DateTime;
use MongoDB\BSON\Regex;
use MongoDB\BSON\UTCDateTime;
use Nebkam\OdmSearchParam\SearchParamParser;
use Nebkam\OdmSearchParam\Tests\Documents\SearchableDocument;
Expand Down Expand Up @@ -333,6 +334,22 @@ public function testRangeEnumIntTo(): void
self::assertBuiltMatchStageEquals($matchStage, ['rangeIntEnumToProperty' => ['$lte' => IntEnum::FOO->value]]);
}

/**
* @throws ReflectionException
*/
public function testRegex(): void
{
$filter = new SearchFilter();
$filter->regexProperty = 'foo';
$queryBuilder = self::createTestQueryBuilder(SearchableDocument::class);
$matchStage = self::createTestAggregationBuilder(SearchableDocument::class)->match();

SearchParamParser::parse($filter, $queryBuilder);
SearchParamParser::parse($filter, $matchStage);
self::assertEquals(new Regex('foo', 'i'), $queryBuilder->getQuery()->debug()['query']['regexProperty']);
self::assertEquals(new Regex('foo', 'i'), $matchStage->getExpression()['$match']['regexProperty']);
}

/**
* @throws ReflectionException
*/
Expand Down

0 comments on commit 7e9c06b

Please sign in to comment.