Skip to content

Commit

Permalink
Add support for simple param parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Bas de Nooijer authored and Bas de Nooijer committed Dec 2, 2016
1 parent e93978f commit 22f2e28
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
35 changes: 35 additions & 0 deletions library/Solarium/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@
*/
class QueryBuilder extends AbstractQueryBuilder implements QueryBuilderInterface
{
/**
* Define which params are allowed to be mapped
*/
protected $paramWhitelist = ['d'];

/**
* @return array
*/
public function getParamWhitelist()
{
return $this->paramWhitelist;
}

/**
* @param array $paramWhitelist
*/
public function setParamWhitelist($paramWhitelist)
{
$this->paramWhitelist = $paramWhitelist;
}

/**
* Build query object from a request.
*
Expand All @@ -74,6 +95,7 @@ public function build(Query $query, Request $request)
$this->parseSortParam($query, $request);
$this->parseFilterQueries($query, $request);
$this->parseComponents($query, $request);
$this->parseSimpleParams($query, $request);
}

/**
Expand Down Expand Up @@ -153,4 +175,17 @@ protected function parseComponents(Query $query, Request $request)
$componentBuilder->buildQuery($query, $request);
}
}

/**
* @param Query $query
* @param Request $request
*/
protected function parseSimpleParams(Query $query, Request $request)
{
foreach ($request->getParams() as $key => $value) {
if (in_array($key, $this->paramWhitelist)) {
$query->addParam($key, $value);
}
}
}
}
10 changes: 10 additions & 0 deletions tests/Solarium/QueryBuilder/Tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,14 @@ public function testQueryWithComponents()
{
$this->markTestIncomplete('not yet implemented');
}

public function testRemainingRequestParamsAreMapped()
{
$this->request->addParam('sort', 'price ASC');
$this->request->addParam('d', 50);
$this->request->addParam('foo', 'bar');
$this->queryBuilder->build($this->query, $this->request);
$this->assertCount(1, $this->query->getParams());
$this->assertEquals(['d' => 50], $this->query->getParams());
}
}

0 comments on commit 22f2e28

Please sign in to comment.