Skip to content

Commit

Permalink
Merge pull request #5636 from pamil/grid/introduce-variadics
Browse files Browse the repository at this point in the history
[Grid] Introduce variadics
  • Loading branch information
Paweł Jędrzejewski committed Jul 29, 2016
2 parents ecc674d + 56c1551 commit 4262d4c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 49 deletions.
20 changes: 4 additions & 16 deletions src/Sylius/Bundle/GridBundle/Doctrine/DBAL/ExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,17 @@ function __construct(QueryBuilder $queryBuilder)
/**
* {@inheritdoc}
*/
public function andX($expressions)
public function andX(...$expressions)
{
$expr = $this->queryBuilder->expr();

if (is_array($expressions)) {
return call_user_func_array([$expr, 'andX'], $expressions);
}

return $this->queryBuilder->expr()->andX(func_get_args());
return $this->queryBuilder->expr()->andX(...$expressions);
}

/**
* {@inheritdoc}
*/
public function orX($expressions)
public function orX(...$expressions)
{
$expr = $this->queryBuilder->expr();

if (is_array($expressions)) {
return call_user_func_array([$expr, 'orX'], $expressions);
}

return $this->queryBuilder->expr()->orX(func_get_args());
return $this->queryBuilder->expr()->orX(...$expressions);
}

/**
Expand Down
20 changes: 4 additions & 16 deletions src/Sylius/Bundle/GridBundle/Doctrine/ORM/ExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,17 @@ public function __construct(QueryBuilder $queryBuilder)
/**
* {@inheritdoc}
*/
public function andX($expressions)
public function andX(...$expressions)
{
$expr = $this->queryBuilder->expr();

if (is_array($expressions)) {
return call_user_func_array([$expr, 'andX'], $expressions);
}

return $this->queryBuilder->expr()->andX(func_get_args());
return $this->queryBuilder->expr()->andX(...$expressions);
}

/**
* {@inheritdoc}
*/
public function orX($expressions)
public function orX(...$expressions)
{
$expr = $this->queryBuilder->expr();

if (is_array($expressions)) {
return call_user_func_array([$expr, 'orX'], $expressions);
}

return $this->queryBuilder->expr()->orX(func_get_args());
return $this->queryBuilder->expr()->orX(...$expressions);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ public function __construct(CollectionsExpressionBuilder $expressionBuilder = nu
/**
* {@inheritdoc}
*/
public function andX($expressions)
public function andX(...$expressions)
{
return $this->expressionBuilder->andX($expressions);
return $this->expressionBuilder->andX(...$expressions);
}

/**
* {@inheritdoc}
*/
public function orX($expressions)
public function orX(...$expressions)
{
return $this->expressionBuilder->orX($expressions);
return $this->expressionBuilder->orX(...$expressions);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Sylius/Component/Grid/Data/ExpressionBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
interface ExpressionBuilderInterface
{
/**
* @param mixed $expressions
* @param mixed ...$expressions
*
* @return self
*/
public function andX($expressions);
public function andX(...$expressions);

/**
* @param mixed $expressions
* @param mixed ...$expressions
*
* @return self
*/
public function orX($expressions);
public function orX(...$expressions);

/**
* @param string $field
Expand Down
15 changes: 7 additions & 8 deletions src/Sylius/Component/Grid/Filter/StringFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,17 @@ public function apply(DataSourceInterface $dataSource, $name, $data, array $opti
}

if (1 === count($fields)) {
$expression = $this->getExpression($expressionBuilder, $type, $fields[0], $value);
} else {
$expressions = [];
$dataSource->restrict($this->getExpression($expressionBuilder, $type, current($fields), $value));

foreach ($fields as $field) {
$expressions[] = $this->getExpression($expressionBuilder, $type, $field, $value);
}
return;
}

$expression = $expressionBuilder->orX($expressions);
$expressions = [];
foreach ($fields as $field) {
$expressions[] = $this->getExpression($expressionBuilder, $type, $field, $value);
}

$dataSource->restrict($expression);
$dataSource->restrict($expressionBuilder->orX(...$expressions));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Component/Grid/spec/Filter/StringFilterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function it_filters_in_multiple_fields(

$expressionBuilder->like('firstName', '%John%')->willReturn('EXPR1');
$expressionBuilder->like('lastName', '%John%')->willReturn('EXPR2');
$expressionBuilder->orX(['EXPR1', 'EXPR2'])->willReturn('EXPR');
$expressionBuilder->orX('EXPR1', 'EXPR2')->willReturn('EXPR');

$dataSource->restrict('EXPR')->shouldBeCalled();

Expand Down

0 comments on commit 4262d4c

Please sign in to comment.