private function encodeFilterRange(FacetFieldTransformationRegistry $transformationRegistry) : string { if (!$transformationRegistry->hasTransformationForCode((string) $this->attributeCode)) { throw new NoFacetFieldTransformationRegisteredException(sprintf('No facet field transformation is registered for "%s" attribute.', $this->attributeCode)); } $transformation = $transformationRegistry->getTransformationByCode((string) $this->attributeCode); $boundaries = explode(' TO ', $this->value); $facetFilterRange = FacetFilterRange::create($this->getRangeBoundaryValue($boundaries[0]), $this->getRangeBoundaryValue($boundaries[1])); return $transformation->encode($facetFilterRange); }
/** * @param string $filterCode * @param string[] $filterValues * @return string */ private function getFormattedFacetQueryValues(string $filterCode, array $filterValues) : string { if ($this->facetFieldTransformationRegistry->hasTransformationForCode($filterCode)) { $transformation = $this->facetFieldTransformationRegistry->getTransformationByCode($filterCode); $formattedValues = array_map(function (string $filterValue) use($transformation) { $facetValue = $transformation->decode($filterValue); if ($facetValue instanceof FacetFilterRange) { $from = $this->escapeQueryChars($facetValue->from()); $to = $this->escapeQueryChars($facetValue->to()); return sprintf('[%s TO %s]', $from, $to); } return sprintf('"%s"', $this->escapeQueryChars($facetValue)); }, $filterValues); return sprintf('%s:(%s)', $this->escapeQueryChars($filterCode), implode(' OR ', $formattedValues)); } $escapedFilterValues = array_map([$this, 'escapeQueryChars'], $filterValues); return sprintf('%s:("%s")', $this->escapeQueryChars($filterCode), implode('" OR "', $escapedFilterValues)); }
public function testFacetQueriesAreReturned() { $attributeCodeString = 'price'; $attributeValueFrom = '*'; $attributeValueTo = '500'; $attributeValueCount = 2; $query = sprintf('%s:[%s TO %s]', $attributeCodeString, $attributeValueFrom, $attributeValueTo); $encodedQueryValue = sprintf('From %s to %s', $attributeValueFrom, $attributeValueTo); $responseArray = ['facet_counts' => ['facet_queries' => [$query => $attributeValueCount]]]; $selectedFilterAttributeCodes = []; $stubFacetFieldTransformation = $this->createMock(FacetFieldTransformation::class); $stubFacetFieldTransformation->method('encode')->willReturn($encodedQueryValue); $this->stubFacetFieldTransformationRegistry->method('hasTransformationForCode')->with($attributeCodeString)->willReturn(true); $this->stubFacetFieldTransformationRegistry->method('getTransformationByCode')->with($attributeCodeString)->willReturn($stubFacetFieldTransformation); $response = SolrResponse::fromSolrResponseArray($responseArray, $this->stubFacetFieldTransformationRegistry); $expectedFacetField = new FacetField(AttributeCode::fromString($attributeCodeString), new FacetFieldValue($encodedQueryValue, $attributeValueCount)); $this->assertEquals([$expectedFacetField], $response->getNonSelectedFacetFields($selectedFilterAttributeCodes)); }
public function testFacetFieldTransformationIsAppliedToFacetField() { $testRangedAttributeCode = 'foo'; $testNormalAttributeCode = 'bar'; $this->stubFacetFiltersToIncludeInResult->method('getFields')->willReturn([$this->createStubFacetFilterRequestField($testRangedAttributeCode), $this->createStubFacetFilterRequestField($testNormalAttributeCode)]); $testFilterSelection = [$testRangedAttributeCode => ['Value does not matter as it will be transformed.'], $testNormalAttributeCode => ['Does not matter either.']]; $rangeFrom = 'baz'; $rangeTo = 'qux'; $stubFacetFilterRange = $this->createMock(FacetFilterRange::class); $stubFacetFilterRange->method('from')->willReturn($rangeFrom); $stubFacetFilterRange->method('to')->willReturn($rangeTo); $stubRangedTransformation = $this->createMock(FacetFieldTransformation::class); $stubRangedTransformation->method('decode')->willReturn($stubFacetFilterRange); $transformedValue = 'Transformed value'; $stubNormalTransformation = $this->createMock(FacetFieldTransformation::class); $stubNormalTransformation->method('decode')->willReturn($transformedValue); $this->stubFacetFieldTransformationRegistry->method('hasTransformationForCode')->willReturn(true); $this->stubFacetFieldTransformationRegistry->method('getTransformationByCode')->willReturnMap([[$testRangedAttributeCode, $stubRangedTransformation], [$testNormalAttributeCode, $stubNormalTransformation]]); $solrFacetFilterRequest = new SolrFacetFilterRequest($this->stubFacetFiltersToIncludeInResult, $testFilterSelection, $this->stubFacetFieldTransformationRegistry); $expectedArray = ['facet' => 'on', 'facet.mincount' => 1, 'facet.limit' => -1, 'facet.sort' => 'index', 'facet.field' => [$testRangedAttributeCode, $testNormalAttributeCode], 'facet.query' => [], 'fq' => [sprintf('%s:([%s TO %s])', $testRangedAttributeCode, $rangeFrom, $rangeTo), sprintf('%s:("%s")', $testNormalAttributeCode, $transformedValue)]]; $this->assertSame($expectedArray, $solrFacetFilterRequest->toArray()); }