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\Facet; |
12
|
|
|
|
13
|
|
|
use Elastica\Aggregation\AbstractAggregation; |
14
|
|
|
use Elastica\Aggregation\Terms; |
15
|
|
|
use Elastica\Query\AbstractQuery; |
16
|
|
|
use Elastica\Query\Terms as TermsQuery; |
17
|
|
|
use Sylius\Component\Core\Model\Taxon; |
18
|
|
|
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; |
19
|
|
|
|
20
|
|
|
final class TaxonFacet implements FacetInterface |
21
|
|
|
{ |
22
|
|
|
public const FACET_ID = 'taxon'; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
private $taxonsPropertyName; |
26
|
|
|
|
27
|
|
|
/** @var TaxonRepositoryInterface */ |
28
|
|
|
private $taxonRepository; |
29
|
|
|
|
30
|
|
|
public function __construct(TaxonRepositoryInterface $taxonRepository, string $taxonsPropertyName) |
31
|
|
|
{ |
32
|
|
|
$this->taxonRepository = $taxonRepository; |
33
|
|
|
$this->taxonsPropertyName = $taxonsPropertyName; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getAggregation(): AbstractAggregation |
37
|
|
|
{ |
38
|
|
|
$terms = new Terms(self::FACET_ID); |
39
|
|
|
$terms->setField($this->getField()); |
40
|
|
|
|
41
|
|
|
return $terms; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getQuery(array $selectedBuckets): AbstractQuery |
45
|
|
|
{ |
46
|
|
|
return new TermsQuery($this->getField(), $selectedBuckets); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getBucketLabel(array $bucket): string |
50
|
|
|
{ |
51
|
|
|
$label = $taxonCode = $bucket['key']; |
52
|
|
|
$taxon = $this->taxonRepository->findOneBy(['code' => $taxonCode]); |
53
|
|
|
if ($taxon instanceof Taxon) { |
54
|
|
|
$label = $taxon->getName(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return sprintf('%s (%s)', $label, $bucket['doc_count']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function getField(): string |
61
|
|
|
{ |
62
|
|
|
return $this->taxonsPropertyName . '.keyword'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getLabel(): string |
66
|
|
|
{ |
67
|
|
|
return 'bitbag_sylius_elasticsearch_plugin.ui.facet.taxon.label'; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|