<?php namespace IQDEV\ElasticSearchTests\Helpers; use IQDEV\Search\Document\Product; use IQDEV\Search\Facet\Facet; use IQDEV\Search\Facet\Item\FacetItemList; use IQDEV\Search\Facet\Item\FacetItemRange; use IQDEV\Search\Facet\Type\FacetListType; use IQDEV\Search\Facet\Type\FacetRangeType; use IQDEV\Search\Result; class FormatData { public static function formatData(Result $result): array { $oProductCollection = $result->getProducts(); $aResult = ['hits' => []]; foreach ($oProductCollection as $oProduct) { /** @var Product $oProduct */ $aResult['hits'][] = $oProduct->id; } return $aResult; } public static function formatDataWFacets(Result $result): array { $aResult = ['facets' => []]; $aResult['hits'] = static::formatData($result)['hits']; foreach ($result->getFacets() as $facet) { /** @var Facet $facet */ $dataFacet = [ 'code' => $facet->getCode(), 'label' => null, // $facet->getLabel(), 'type' => $facet->getType() instanceof FacetRangeType ? 'range' : ($facet->getType() instanceof FacetListType ? 'list' : null), 'items' => [ 'list' => [], 'range' => [] ], ]; $items = $facet->products->sort('getValue'); foreach ($items as $item) { if ($item instanceof FacetItemList) { /** @var FacetItemList $item */ $dataFacet['items']['list'][] = [ 'label' => $item->getLabel(), 'value' => $item->getValue(), 'count' => $item->getCount(), 'active' => $item->isActive(), ]; } if ($item instanceof FacetItemRange) { /** @var FacetItemRange $item */ $aData = [ 'label' => $item->getLabel(), 'count' => $item->getCount(), 'active' => $item->isActive(), ]; $aData['fullRange'] = $item->getFullRange(); $aData['activeRange'] = $item->getSelectedRange(); if ($result->getTotal() > 0 && empty(array_filter($aData['activeRange']))) { $aData['activeRange'] = $aData['fullRange']; } $dataFacet['items']['range'][] = $aData; } } $aResult['facets'][] = $dataFacet; } return $aResult; } }