|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ONGR package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ONGR\FilterManagerBundle\Filter\Widget\Search; |
|
13
|
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\Query\TermQuery; |
|
15
|
|
|
use ONGR\ElasticsearchDSL\Search; |
|
16
|
|
|
use ONGR\FilterManagerBundle\Filter\FilterState; |
|
17
|
|
|
use ONGR\FilterManagerBundle\Filter\Relation\RelationAwareTrait; |
|
18
|
|
|
use ONGR\FilterManagerBundle\Search\SearchRequest; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Filter for filtering on exact value in specified field. |
|
23
|
|
|
*/ |
|
24
|
|
|
class DocumentValue extends AbstractSingleValue |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
use RelationAwareTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getState(Request $request) |
|
33
|
|
|
{ |
|
34
|
|
|
$state = new FilterState(); |
|
35
|
|
|
$document = $request->get('document'); |
|
36
|
|
|
|
|
37
|
|
|
if (is_object($document)) { |
|
38
|
|
|
try { |
|
39
|
|
|
$closure = \Closure::bind(function ($document, $field) { |
|
40
|
|
|
return $document->$field; |
|
41
|
|
|
}, null, $document); |
|
42
|
|
|
$state->setValue($closure($document, $this->getOption('field'))); |
|
43
|
|
|
$state->setActive(true); |
|
44
|
|
|
} catch (\Exception $e) { |
|
45
|
|
|
throw new \LogicException("Cannot access document field."); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $state; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null) |
|
56
|
|
|
{ |
|
57
|
|
|
$search->addPostFilter(new TermQuery($this->getDocumentField(), $state->getValue())); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: