Created
June 30, 2016 10:19
-
-
Save wodor/29576638a260187c678f18f5da2e11a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use eZ\Publish\API\Repository\Values\Content\Search\Facet; | |
| class IntervalFacet extends Facet | |
| { | |
| /** | |
| * @var array | |
| */ | |
| public $entries; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use eZ\Publish\API\Repository\Values\Content\Query\FacetBuilder; | |
| use EzSystems\EzPlatformSolrSearchEngine\Query\FacetBuilderVisitor; | |
| class IntervalFacetBuilderVisitor extends FacetBuilderVisitor | |
| { | |
| /** | |
| * @var string | |
| */ | |
| private $searchEngineFieldName; | |
| /** | |
| * @var string | |
| */ | |
| private $fieldName; | |
| /** | |
| * @param $fieldName | |
| * @param string $searchEngineFieldName | |
| */ | |
| public function __construct($fieldName, $searchEngineFieldName) | |
| { | |
| $this->fieldName = $fieldName; | |
| $this->searchEngineFieldName = $searchEngineFieldName; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function canMap($field) | |
| { | |
| return $field === $this->searchEngineFieldName; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function map($field, array $data) | |
| { | |
| return new IntervalFacet( | |
| array( | |
| 'name' => $this->fieldName, | |
| 'entries' => $data, // facet_intervals is actually normal object, unlike facet_fields | |
| ) | |
| ); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function canVisit(FacetBuilder $facetBuilder) | |
| { | |
| if (!$facetBuilder instanceof IntervalFacetBuilder) { | |
| return false; | |
| } | |
| return $this->searchEngineFieldName === $facetBuilder->searchEngineFieldName; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| * @param $facetBuilder IntervalFacetBuilder | |
| */ | |
| public function visit(FacetBuilder $facetBuilder) | |
| { | |
| $set = array_map('strval', $facetBuilder->facetIntervalSet); | |
| return [ | |
| 'facet.interval' => $this->searchEngineFieldName, | |
| 'f.'.$this->searchEngineFieldName.'.facet.interval.set' => $set | |
| ]; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class FacetInterval | |
| { | |
| /** | |
| * @var string | |
| */ | |
| private $from; | |
| /** | |
| * @var string | |
| */ | |
| private $to; | |
| /** | |
| * @var string | |
| */ | |
| private $key; | |
| /** | |
| * @param string $from | |
| * @param string $to | |
| * @param string $key | |
| */ | |
| public function __construct($from, $to, $key) | |
| { | |
| $this->from = $from; | |
| $this->to = $to; | |
| $this->key = $key; | |
| } | |
| /** | |
| * @return string | |
| */ | |
| private function getFacetIntervalString() | |
| { | |
| return sprintf("{!key='%s'}[%s,%s)", $this->key, $this->from, $this->to); | |
| } | |
| public function __toString() | |
| { | |
| return $this->getFacetIntervalString(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment