Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add memberOf() method #282

Merged
merged 1 commit into from
Dec 7, 2022
Merged
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
9 changes: 9 additions & 0 deletions src/Bundle/Doctrine/DBAL/ExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\DBAL\Query\QueryBuilder;
use Sylius\Component\Grid\Data\ExpressionBuilderInterface;
use Sylius\Component\Resource\Exception\UnsupportedMethodException;

final class ExpressionBuilder implements ExpressionBuilderInterface
{
Expand Down Expand Up @@ -82,6 +83,14 @@ public function greaterThanOrEqual(string $field, $value)
return $this->queryBuilder->expr()->gte($field, ':' . $field);
}

/**
* Method not supported by Doctrine\DBAL\Query\Expression\ExpressionBuilder
*/
public function memberOf($value, string $field)
{
throw new UnsupportedMethodException('memberOf');
}

public function in(string $field, array $values)
{
return $this->queryBuilder->expr()->in($field, $values);
Expand Down
7 changes: 7 additions & 0 deletions src/Bundle/Doctrine/ORM/ExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ public function greaterThanOrEqual(string $field, $value)
return $this->queryBuilder->expr()->gte($this->resolveFieldByAddingJoins($field), ':' . $parameterName);
}

public function memberOf($value, string $field)
{
$field = $this->adjustField($field);

return $this->queryBuilder->expr()->isMemberOf($value, $this->resolveFieldByAddingJoins($field));
}

public function in(string $field, array $values)
{
$field = $this->adjustField($field);
Expand Down
5 changes: 5 additions & 0 deletions src/Bundle/Doctrine/PHPCRODM/ExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public function greaterThanOrEqual(string $field, $value)
return $this->expressionBuilder->gte($field, $value);
}

public function memberOf($value, string $field)
{
return $this->expressionBuilder->memberOf($value, $field);
}

public function in(string $field, array $values)
{
return $this->expressionBuilder->in($field, $values);
Expand Down
7 changes: 7 additions & 0 deletions src/Component/Data/ExpressionBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ public function greaterThan(string $field, $value);
*/
public function greaterThanOrEqual(string $field, $value);

/**
* @param mixed $value
*
* @return mixed
*/
public function memberOf($value, string $field);

/**
* @return mixed
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Component/Filter/StringFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ final class StringFilter implements FilterInterface

public const TYPE_ENDS_WITH = 'ends_with';

public const TYPE_MEMBER_OF = 'member_of';

public const TYPE_IN = 'in';

public const TYPE_NOT_IN = 'not_in';
Expand Down Expand Up @@ -107,6 +109,8 @@ private function getExpression(
return $expressionBuilder->in($field, array_map('trim', explode(',', $value)));
case self::TYPE_NOT_IN:
return $expressionBuilder->notIn($field, array_map('trim', explode(',', $value)));
case self::TYPE_MEMBER_OF:
return $expressionBuilder->memberOf($value, $field);
default:
throw new \InvalidArgumentException(sprintf('Could not get an expression for type "%s"!', $type));
}
Expand Down
12 changes: 12 additions & 0 deletions src/Component/spec/Filter/StringFilterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ function it_filters_data_containing_one_of_strings(
$this->apply($dataSource, 'firstName', ['type' => StringFilter::TYPE_IN, 'value' => 'John, Paul,Rick'], []);
}

function it_filters_data_containing_value_being_member_of_field(
DataSourceInterface $dataSource,
ExpressionBuilderInterface $expressionBuilder,
): void {
$dataSource->getExpressionBuilder()->willReturn($expressionBuilder);

$expressionBuilder->memberOf('Rick', 'firstName')->willReturn('EXPR');
$dataSource->restrict('EXPR')->shouldBeCalled();

$this->apply($dataSource, 'firstName', ['type' => StringFilter::TYPE_MEMBER_OF, 'value' => 'Rick'], []);
}

function it_filters_data_containing_none_of_strings(
DataSourceInterface $dataSource,
ExpressionBuilderInterface $expressionBuilder,
Expand Down