From b6cf5eb6f72fa9b6db4338b93ca3feb35253edde Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= <simonas.serlinskas@nfq.com>
Date: Wed, 5 Aug 2015 10:49:00 +0300
Subject: [PATCH] simplifying highlight functionality

---
 src/Highlight/Field.php     | 211 ------------------------------------
 src/Highlight/Highlight.php | 189 +++++---------------------------
 2 files changed, 26 insertions(+), 374 deletions(-)
 delete mode 100644 src/Highlight/Field.php

diff --git a/src/Highlight/Field.php b/src/Highlight/Field.php
deleted file mode 100644
index 536240c..0000000
--- a/src/Highlight/Field.php
+++ /dev/null
@@ -1,211 +0,0 @@
-<?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\ElasticsearchDSL\Highlight;
-
-use ONGR\ElasticsearchDSL\BuilderInterface;
-use ONGR\ElasticsearchDSL\NamedBuilderInterface;
-
-/**
- * This class holds data for highlighting field.
- */
-class Field implements NamedBuilderInterface
-{
-    const TYPE_PLAIN = 'plain';
-    const TYPE_POSTINGS = 'postings';
-    const TYPE_FVH = 'fvh';
-
-    /**
-     * @var string Field name.
-     */
-    private $name;
-
-    /**
-     * @var string Highlighter type. By default 'plain'.
-     */
-    private $type;
-
-    /**
-     * @var int Size of the highlighted fragment in characters. By default 100.
-     */
-    private $fragmentSize;
-
-    /**
-     * @var int Maximum number of fragments to return. By default 5.
-     */
-    private $numberOfFragments;
-
-    /**
-     * @var array Combine matches on multiple fields to highlight a single field.
-     */
-    private $matchedFields;
-
-    /**
-     * @var array BuilderInterface query to highlight.
-     */
-    private $highlightQuery;
-
-    /**
-     * @var int Show part of string even if there are no matches to highlight. Defaults to 0.
-     */
-    private $noMatchSize;
-
-    /**
-     * @var bool Highlight fields based on the source.
-     */
-    private $forceSource;
-
-    /**
-     * Creates a highlight for a field.
-     *
-     * @param string $name Field name.
-     */
-    public function __construct($name)
-    {
-        $this->name = $name;
-        $this->setMatchedFields([$name]);
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getName()
-    {
-        return $this->name;
-    }
-
-    /**
-     * Sets highlighter type (forces). Available options 'plain', 'postings', 'fvh'.
-     *
-     * @param string $type
-     *
-     * @return Field
-     */
-    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 Field
-     */
-    public function setFragmentSize($fragmentSize)
-    {
-        $this->fragmentSize = $fragmentSize;
-
-        return $this;
-    }
-
-    /**
-     * Sets maximum number of fragments to return.
-     *
-     * @param int $numberOfFragments
-     *
-     * @return Field
-     */
-    public function setNumberOfFragments($numberOfFragments)
-    {
-        $this->numberOfFragments = $numberOfFragments;
-
-        return $this;
-    }
-
-    /**
-     * Set fields to match.
-     *
-     * @param array $matchedFields
-     *
-     * @return Field
-     */
-    public function setMatchedFields($matchedFields)
-    {
-        $this->matchedFields = $matchedFields;
-
-        return $this;
-    }
-
-    /**
-     * Set query to highlight.
-     *
-     * @param BuilderInterface $query
-     *
-     * @return Field
-     */
-    public function setHighlightQuery(BuilderInterface $query)
-    {
-        $this->highlightQuery = [$query->getType() => $query->toArray()];
-
-        return $this;
-    }
-
-    /**
-     * Shows set length of a string even if no matches found.
-     *
-     * @param int $noMatchSize
-     *
-     * @return Field
-     */
-    public function setNoMatchSize($noMatchSize)
-    {
-        $this->noMatchSize = $noMatchSize;
-
-        return $this;
-    }
-
-    /**
-     * Set to force highlighting from source.
-     *
-     * @param bool $forceSource
-     *
-     * @return Field
-     */
-    public function setForceSource($forceSource)
-    {
-        $this->forceSource = $forceSource;
-
-        return $this;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function toArray()
-    {
-        return array_filter(
-            [
-                'fragment_size' => $this->fragmentSize,
-                'number_of_fragments' => $this->numberOfFragments,
-                'type' => $this->type,
-                'matched_fields' => $this->matchedFields,
-                'highlight_query' => $this->highlightQuery,
-                'no_match_size' => $this->noMatchSize,
-                'force_source' => $this->forceSource,
-            ]
-        );
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getType()
-    {
-        return $this->type;
-    }
-}
diff --git a/src/Highlight/Highlight.php b/src/Highlight/Highlight.php
index 13fa0d8..7723ea0 100644
--- a/src/Highlight/Highlight.php
+++ b/src/Highlight/Highlight.php
@@ -12,68 +12,34 @@
 namespace ONGR\ElasticsearchDSL\Highlight;
 
 use ONGR\ElasticsearchDSL\BuilderInterface;
-use ONGR\ElasticsearchDSL\NamedBuilderBag;
-use ONGR\ElasticsearchDSL\NamedBuilderInterface;
+use ONGR\ElasticsearchDSL\ParametersTrait;
 
 /**
  * Data holder for highlight api.
  */
-class Highlight extends NamedBuilderBag implements BuilderInterface
+class Highlight implements BuilderInterface
 {
-    const TYPE_PLAIN = 'plain';
-    const TYPE_POSTINGS = 'postings';
-    const TYPE_FVH = 'fvh';
+    use ParametersTrait;
 
     /**
-     * @var array Holds html tag name and class that highlight will be put in (default 'em' tag).
+     * @var array Holds fields for highlight.
      */
-    private $tags = [];
+    private $fields = [];
 
     /**
-     * @var string Holds tag schema name. 'styled' is the only option yet.
+     * @var array
      */
-    private $tagsSchema = null;
+    private $tags;
 
     /**
-     * @var string Fragments sort type.
-     */
-    private $order = null;
-
-    /**
-     * @var string Highlighter type. By default plain.
-     */
-    private $type = null;
-
-    /**
-     * @var int Size of the highlighted fragment in characters. By default 100.
-     */
-    private $fragmentSize = null;
-
-    /**
-     * @var int Maximum number of fragments to return. By default 5.
-     */
-    private $numberOfFragments = null;
-
-    /**
-     * {@inheritdoc}
-     *
-     * @return Highlight
-     */
-    public function add(NamedBuilderInterface $builder)
-    {
-        parent::add($builder);
-
-        return $this;
-    }
-
-    /**
-     * {@inheritdoc}
+     * @param $name
+     * @param array $params
      *
-     * @return Highlight
+     * @return $this
      */
-    public function set(array $builders)
+    public function addField($name, array $params = [])
     {
-        parent::set($builders);
+        $this->fields[$name] = $params;
 
         return $this;
     }
@@ -81,92 +47,15 @@ class Highlight extends NamedBuilderBag implements BuilderInterface
     /**
      * Sets html tag and its class used in highlighting.
      *
-     * @param string $tag
-     * @param string $class
+     * @param array $preTags
+     * @param array $postTags
      *
-     * @return Highlight
+     * @return $this
      */
-    public function setTag($tag, $class = null)
+    public function setTags(array $preTags, array $postTags)
     {
-        $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;
+        $this->tags['pre_tags'] = $preTags;
+        $this->tags['post_tags'] = $postTags;
 
         return $this;
     }
@@ -184,44 +73,18 @@ class Highlight extends NamedBuilderBag implements BuilderInterface
      */
     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(),
-            ]
-        );
-
-        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']);
-                }
-            }
+        $output = [];
+
+        if (is_array($this->tags)) {
+            $output = $this->tags;
         }
 
-        return $highlight;
-    }
+        $output = $this->processArray($output);
 
-    /**
-     * Returns fields as array.
-     *
-     * @return array
-     */
-    private function getFields()
-    {
-        $out = [];
-        foreach ($this->all() as $builder) {
-            $out = array_merge($out, [$builder->getName() => $builder->toArray()]);
+        foreach ($this->fields as $field => $params) {
+            $output['fields'][$field] = count($params) ? $params : new \stdClass();
         }
 
-        return $out;
+        return $output;
     }
 }
-- 
GitLab