From 22f2e28c9ea1f0955dc82ef8086f8b2cc5ed8f19 Mon Sep 17 00:00:00 2001 From: Bas de Nooijer Date: Fri, 2 Dec 2016 11:48:46 +0100 Subject: [PATCH] Add support for simple param parsing --- .../Solarium/QueryBuilder/QueryBuilder.php | 35 +++++++++++++++++++ .../QueryBuilder/Tests/QueryBuilderTest.php | 10 ++++++ 2 files changed, 45 insertions(+) diff --git a/library/Solarium/QueryBuilder/QueryBuilder.php b/library/Solarium/QueryBuilder/QueryBuilder.php index dd43267..9b0d450 100644 --- a/library/Solarium/QueryBuilder/QueryBuilder.php +++ b/library/Solarium/QueryBuilder/QueryBuilder.php @@ -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. * @@ -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); } /** @@ -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); + } + } + } } diff --git a/tests/Solarium/QueryBuilder/Tests/QueryBuilderTest.php b/tests/Solarium/QueryBuilder/Tests/QueryBuilderTest.php index e976466..66b621b 100644 --- a/tests/Solarium/QueryBuilder/Tests/QueryBuilderTest.php +++ b/tests/Solarium/QueryBuilder/Tests/QueryBuilderTest.php @@ -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()); + } } \ No newline at end of file