Skip to content
Snippets Groups Projects
Highlight.php 4.78 KiB
Newer Older
ONGR Team's avatar
ONGR Team committed
<?php

/*
 * This file is part of the ONGR package.
 *
 * (c) NFQ Technologies UAB <info@nfq.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ONGR\ElasticsearchBundle\DSL\Highlight;

use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\NamedBuilderBag;
use ONGR\ElasticsearchBundle\DSL\NamedBuilderInterface;
ONGR Team's avatar
ONGR Team committed
/**
 * Data holder for highlight api.
 */
class Highlight extends NamedBuilderBag implements BuilderInterface
ONGR Team's avatar
ONGR Team committed
{
    const TYPE_PLAIN = 'plain';
    const TYPE_POSTINGS = 'postings';
    const TYPE_FVH = 'fvh';

    /**
Martynas Sudintas's avatar
Martynas Sudintas committed
     * @var array Holds html tag name and class that highlight will be put in (default 'em' tag).
ONGR Team's avatar
ONGR Team committed
     */
    private $tags = [];
ONGR Team's avatar
ONGR Team committed

    /**
Martynas Sudintas's avatar
Martynas Sudintas committed
     * @var string Holds tag schema name. 'styled' is the only option yet.
ONGR Team's avatar
ONGR Team committed
     */
    private $tagsSchema = null;
ONGR Team's avatar
ONGR Team committed

    /**
Martynas Sudintas's avatar
Martynas Sudintas committed
     * @var string Fragments sort type.
ONGR Team's avatar
ONGR Team committed
     */
    private $order = null;
ONGR Team's avatar
ONGR Team committed

    /**
Martynas Sudintas's avatar
Martynas Sudintas committed
     * @var string Highlighter type. By default plain.
ONGR Team's avatar
ONGR Team committed
     */
    private $type = null;
ONGR Team's avatar
ONGR Team committed

    /**
Martynas Sudintas's avatar
Martynas Sudintas committed
     * @var int Size of the highlighted fragment in characters. By default 100.
ONGR Team's avatar
ONGR Team committed
     */
    private $fragmentSize = null;
ONGR Team's avatar
ONGR Team committed

    /**
Martynas Sudintas's avatar
Martynas Sudintas committed
     * @var int Maximum number of fragments to return. By default 5.
ONGR Team's avatar
ONGR Team committed
     */
    private $numberOfFragments = null;
ONGR Team's avatar
ONGR Team committed

    /**
     * {@inheritdoc}
ONGR Team's avatar
ONGR Team committed
     *
     * @return Highlight
     */
    public function add(NamedBuilderInterface $builder)
ONGR Team's avatar
ONGR Team committed
    {
        parent::add($builder);
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
     * {@inheritdoc}
ONGR Team's avatar
ONGR Team committed
     *
     * @return Highlight
     */
    public function set(array $builders)
ONGR Team's avatar
ONGR Team committed
    {
        parent::set($builders);
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
     * Sets html tag and its class used in highlighting.
     *
     * @param string $tag
     * @param string $class
     *
     * @return Highlight
     */
    public function setTag($tag, $class = null)
    {
        $this->tags[] = array_filter(
            [
                'tag' => $tag,
                'class' => $class,
            ]
        );

        return $this;
    }

    /**
     * Sets html tag and its class used in highlighting.
     *
     * @param string $tagsSchema
     *
     * @return Highlight
     */
    public function setTagsSchema($tagsSchema)
    {
        $this->tagsSchema = $tagsSchema;

        return $this;
    }

    /**
     * Sets fragments sort order.
     *
     * @param string $order
     *
     * @return Highlight
     */
    public function setOrder($order)
    {
        $this->order = $order;

        return $this;
    }

    /**
     * Sets highlighter type (forces). Available options plain, postings, fvh.
     *
     * @param string $type
     *
     * @return Highlight
     */
    public function setHighlighterType($type)
    {
        $reflection = new \ReflectionClass(__CLASS__);
        if (in_array($type, $reflection->getConstants())) {
            $this->type = $type;
        }

        return $this;
    }

    /**
     * Sets field fragment size.
     *
     * @param int $fragmentSize
     *
     * @return Highlight
     */
    public function setFragmentSize($fragmentSize)
    {
        $this->fragmentSize = $fragmentSize;

        return $this;
    }

    /**
     * Sets maximum number of fragments to return.
     *
     * @param int $numberOfFragments
     *
     * @return Highlight
     */
    public function setNumberOfFragments($numberOfFragments)
    {
        $this->numberOfFragments = $numberOfFragments;

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function getType()
    {
        return 'highlight';
    }

ONGR Team's avatar
ONGR Team committed
    /**
     * {@inheritdoc}
     */
    public function toArray()
    {
        $highlight = array_filter(
            [
                'order' => $this->order,
                'type' => $this->type,
                'fragment_size' => $this->fragmentSize,
                'number_of_fragments' => $this->numberOfFragments,
                'tags_schema' => $this->tagsSchema,
                'fields' => $this->getFields(),
ONGR Team's avatar
ONGR Team committed
            ]
        );

        foreach ($this->tags as $tag) {
            if (isset($tag['tag'])) {
                $highlight['post_tags'][] = sprintf('</%s>', $tag['tag']);

                if (isset($tag['class'])) {
                    $highlight['pre_tags'][] = sprintf('<%s class="%s">', $tag['tag'], $tag['class']);
                } else {
                    $highlight['pre_tags'][] = sprintf('<%s>', $tag['tag']);
                }
            }
        }

        return $highlight;
    }

    /**
     * Returns fields as array.
     *
     * @return array
     */
    private function getFields()
    {
        $out = [];
        foreach ($this->all() as $builder) {
            $out = array_merge($out, [$builder->getName() => $builder->toArray()]);
ONGR Team's avatar
ONGR Team committed
        }

ONGR Team's avatar
ONGR Team committed
    }
}