1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\SyliusElasticsearchPlugin\QueryBuilder; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface; |
16
|
|
|
use Elastica\Query\AbstractQuery; |
17
|
|
|
use Elastica\Query\Match; |
|
|
|
|
18
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
19
|
|
|
|
20
|
|
|
final class ContainsNameQueryBuilder implements QueryBuilderInterface |
21
|
|
|
{ |
22
|
|
|
/** @var LocaleContextInterface */ |
23
|
|
|
private $localeContext; |
24
|
|
|
|
25
|
|
|
/** @var ConcatedNameResolverInterface */ |
26
|
|
|
private $productNameNameResolver; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $namePropertyPrefix; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
LocaleContextInterface $localeContext, |
33
|
|
|
ConcatedNameResolverInterface $productNameNameResolver, |
34
|
|
|
string $namePropertyPrefix |
35
|
|
|
) { |
36
|
|
|
$this->localeContext = $localeContext; |
37
|
|
|
$this->productNameNameResolver = $productNameNameResolver; |
38
|
|
|
$this->namePropertyPrefix = $namePropertyPrefix; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function buildQuery(array $data): ?AbstractQuery |
42
|
|
|
{ |
43
|
|
|
$localeCode = $this->localeContext->getLocaleCode(); |
44
|
|
|
$propertyName = $this->productNameNameResolver->resolvePropertyName($localeCode); |
45
|
|
|
|
46
|
|
|
if (!$name = $data[$this->namePropertyPrefix]) { |
47
|
|
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$nameQuery = new Match(); |
51
|
|
|
|
52
|
|
|
$nameQuery->setFieldQuery($propertyName, $name); |
53
|
|
|
$nameQuery->setFieldFuzziness($propertyName, 2); |
54
|
|
|
$nameQuery->setFieldMinimumShouldMatch($propertyName, 2); |
55
|
|
|
|
56
|
|
|
return $nameQuery; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|