-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathOptionFacet.php
59 lines (47 loc) · 1.67 KB
/
OptionFacet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.io and write us
* an email on [email protected].
*/
declare(strict_types=1);
namespace BitBag\SyliusElasticsearchPlugin\Facet;
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
use Elastica\Aggregation\AbstractAggregation;
use Elastica\Aggregation\Terms;
use Elastica\Query\AbstractQuery;
use Elastica\Query\Terms as TermsQuery;
use Sylius\Component\Product\Model\ProductOptionInterface;
final class OptionFacet implements FacetInterface
{
public function __construct(
private ConcatedNameResolverInterface $optionNameResolver,
private ProductOptionInterface $productOption,
) {
}
public function getAggregation(): AbstractAggregation
{
$aggregation = new Terms('');
$aggregation->setField($this->getFieldName());
return $aggregation;
}
public function getQuery(array $selectedBuckets): AbstractQuery
{
return new TermsQuery($this->getFieldName(), $selectedBuckets);
}
public function getBucketLabel(array $bucket): string
{
$label = ucwords(str_replace('_', ' ', $bucket['key']));
return sprintf('%s (%s)', $label, $bucket['doc_count']);
}
public function getLabel(): string
{
return (string) $this->productOption->getName();
}
private function getFieldName(): string
{
return $this->optionNameResolver->resolvePropertyName((string) $this->productOption->getCode()) . '.keyword';
}
}