Skip to content
Snippets Groups Projects
ProductDocument.php 3.29 KiB
Newer Older
Pavel's avatar
Pavel committed
<?php

namespace IQDEV\ElasticSearch\Document;

Pavel's avatar
Pavel committed
use IQDEV\ElasticSearch\Config\MappingValidator;
use IQDEV\ElasticSearch\Configuration;
use IQDEV\ElasticSearch\Facet\Collection\FacetCollection;
use IQDEV\ElasticSearch\Facet\Facet;
Pavel's avatar
Pavel committed
use IQDEV\ElasticSearch\Helper\ArrayHelper;
Pavel's avatar
Pavel committed

class ProductDocument implements Document
{
Pavel's avatar
Pavel committed
    private array $properties = [];
Pavel's avatar
Pavel committed
    private FacetCollection $keywordFacets;
    private FacetCollection $numberFacets;
Pavel's avatar
Pavel committed
    private ?string $searchContent = null;
    private array $info = [];
    private bool $skipEmpty = false;
Pavel's avatar
Pavel committed

Pavel's avatar
Pavel committed
    public function __construct(
        private Facet $categoryFacet
    ) {
Pavel's avatar
Pavel committed
        $this->keywordFacets = new FacetCollection();
        $this->numberFacets = new FacetCollection();
    public static function create(Facet $categoryFacet): self
Pavel's avatar
Pavel committed
    {
        return new self($categoryFacet);
Pavel's avatar
Pavel committed
    }

    /**
     * @return FacetCollection
     */
    public function getKeywordFacets(): FacetCollection
    {
        return $this->keywordFacets;
    }

    /**
     * @return Facet
Pavel's avatar
Pavel committed
     */
    public function getCategoryFacet(): Facet
Pavel's avatar
Pavel committed
    {
        return $this->categoryFacet;
    }

    /**
     * @return FacetCollection
     */
    public function getNumberFacets(): FacetCollection
    {
        return $this->numberFacets;
    }

    /**
Pavel's avatar
Pavel committed
     * @param string|null $searchContent
Pavel's avatar
Pavel committed
     */
Pavel's avatar
Pavel committed
    public function setSearchContent(?string $searchContent): void
Pavel's avatar
Pavel committed
    {
Pavel's avatar
Pavel committed
        $this->searchContent = $searchContent;
    }

    public function setAdditionData(array $info): self
    {
        $this->info = $info;
        return $this;
    }

    /**
     * Установка значения свойства документа индекса по параметрам конфигурации.
     * Имеет приоритет по сравнению с вызовами функций для установки данных.
     *
     * @param Configuration $configuration
     * @param string $property
     * @param $value
     *
     * @return $this
     */
    public function setByConfiguration(Configuration $configuration, string $property, $value): self
    {
        if (!MappingValidator::isPropertyExists($configuration, $property)) {
            throw new \InvalidArgumentException('Property ' . $property . ' doesnt exist');
        }

        $this->properties[$property] = $value;

        return $this;
    }

    public function skipEmpty(bool $skipEmpty = false): self
    {
        $this->skipEmpty = $skipEmpty;

        return $this;
Pavel's avatar
Pavel committed
    }

    public function es(): array
    {
        $document = [
            'category_id' => $this->getCategoryFacet()->es()['category_id'],
            'search_data' => [
                'keyword_facet' => $this->getKeywordFacets()->es(),
                'number_facet' => $this->getNumberFacets()->es()
            ],
Pavel's avatar
Pavel committed
        ];

Pavel's avatar
Pavel committed
        if (isset($this->searchContent)) {
            $document['full_search_content'] = $this->searchContent;
            $document['suggest_search_content'] = $this->searchContent;
        }

        $result = array_replace_recursive($document, $this->properties);
        if (true === $this->skipEmpty) {
            $result = ArrayHelper::array_filter_recursive($result, static fn ($val) => $val !== null || $val === false);
Pavel's avatar
Pavel committed
        }

Pavel's avatar
Pavel committed
        return $result;
Pavel's avatar
Pavel committed
    }
}