| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file was created by developers working at BitBag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Do you need more information about us and what we do? Visit our https://bitbag.io website! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace BitBag\SyliusElasticsearchPlugin\QueryBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Elastica\Query\AbstractQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Elastica\Query\MatchQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Sylius\Component\Locale\Context\LocaleContextInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | final class ContainsNameQueryBuilder implements QueryBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var LocaleContextInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $localeContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var ConcatedNameResolverInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $productNameNameResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $namePropertyPrefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         LocaleContextInterface $localeContext, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         ConcatedNameResolverInterface $productNameNameResolver, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         string $namePropertyPrefix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->localeContext = $localeContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->productNameNameResolver = $productNameNameResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->namePropertyPrefix = $namePropertyPrefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     public function buildQuery(array $data): ?AbstractQuery | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $localeCode = $this->localeContext->getLocaleCode(); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $propertyName = $this->productNameNameResolver->resolvePropertyName($localeCode); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         if (!$name = $data[$this->namePropertyPrefix]) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $nameQuery = new MatchQuery(); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $nameQuery->setFieldQuery($propertyName, $name); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $nameQuery->setFieldFuzziness($propertyName, 2); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $nameQuery->setFieldMinimumShouldMatch($propertyName, 2); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         return $nameQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  |