From fec0e17c537c898d5ec91ba099438afc952326e7 Mon Sep 17 00:00:00 2001 From: Martynas Sudintas <martynas.sudintas@nfq.lt> Date: Thu, 22 Jan 2015 17:20:07 +0200 Subject: [PATCH] Highlight implemented friendly builder --- Highlight/Field.php | 33 ++++++++++------- Highlight/Highlight.php | 79 ++++++++++------------------------------- 2 files changed, 38 insertions(+), 74 deletions(-) diff --git a/Highlight/Field.php b/Highlight/Field.php index 51f9f47..10eecb4 100644 --- a/Highlight/Field.php +++ b/Highlight/Field.php @@ -12,11 +12,12 @@ namespace ONGR\ElasticsearchBundle\DSL\Highlight; use ONGR\ElasticsearchBundle\DSL\BuilderInterface; +use ONGR\ElasticsearchBundle\DSL\FriendlyBuilderInterface; /** * This class holds data for highlighting field. */ -class Field +class Field implements FriendlyBuilderInterface { const TYPE_PLAIN = 'plain'; const TYPE_POSTINGS = 'postings'; @@ -25,42 +26,42 @@ class Field /** * @var string Field name. */ - protected $name; + private $name; /** * @var string Highlighter type. By default 'plain'. */ - protected $type; + private $type; /** * @var int Size of the highlighted fragment in characters. By default 100. */ - protected $fragmentSize; + private $fragmentSize; /** * @var int Maximum number of fragments to return. By default 5. */ - protected $numberOfFragments; + private $numberOfFragments; /** * @var array Combine matches on multiple fields to highlight a single field. */ - protected $matchedFields; + private $matchedFields; /** * @var BuilderInterface Query to highlight. */ - protected $highlightQuery; + private $highlightQuery; /** * @var int Show part of string even if there are no matches to highlight. Defaults to 0. */ - protected $noMatchSize; + private $noMatchSize; /** * @var bool Highlight fields based on the source. */ - protected $forceSource; + private $forceSource; /** * Creates a highlight for a field. @@ -74,7 +75,7 @@ class Field } /** - * @return string + * {@inheritdoc} */ public function getName() { @@ -183,9 +184,7 @@ class Field } /** - * Returns an array of field parameters. - * - * @return array + * {@inheritdoc} */ public function toArray() { @@ -201,4 +200,12 @@ class Field ] ); } + + /** + * {@inheritdoc} + */ + public function getType() + { + return $this->type; + } } diff --git a/Highlight/Highlight.php b/Highlight/Highlight.php index ba28361..33a0ff3 100644 --- a/Highlight/Highlight.php +++ b/Highlight/Highlight.php @@ -11,115 +11,72 @@ namespace ONGR\ElasticsearchBundle\DSL\Highlight; +use ONGR\ElasticsearchBundle\DSL\FriendlyBuilderBag; +use ONGR\ElasticsearchBundle\DSL\FriendlyBuilderInterface; + /** * Data holder for highlight api. */ -class Highlight +class Highlight extends FriendlyBuilderBag { const TYPE_PLAIN = 'plain'; const TYPE_POSTINGS = 'postings'; const TYPE_FVH = 'fvh'; - /** - * @var array Holds fields to highlight. - */ - protected $fields = []; - /** * @var array Holds html tag name and class that highlight will be put in (default 'em' tag). */ - protected $tags = []; + private $tags = []; /** * @var string Holds tag schema name. 'styled' is the only option yet. */ - protected $tagsSchema = null; + private $tagsSchema = null; /** * @var string Fragments sort type. */ - protected $order = null; + private $order = null; /** * @var string Highlighter type. By default plain. */ - protected $type = null; + private $type = null; /** * @var int Size of the highlighted fragment in characters. By default 100. */ - protected $fragmentSize = null; + private $fragmentSize = null; /** * @var int Maximum number of fragments to return. By default 5. */ - protected $numberOfFragments = null; + private $numberOfFragments = null; /** - * Adds field to highlight. - * - * @param Field $field + * {@inheritdoc} * * @return Highlight */ - public function addField(Field $field) + public function add(FriendlyBuilderInterface $builder) { - if (!$this->hasField($field->getName())) { - $this->fields[] = $field; - } + parent::add($builder); return $this; } /** - * Checks if field already will be highlighted. - * - * @param string $fieldName - * - * @return bool - */ - public function hasField($fieldName) - { - /** @var Field $field */ - foreach ($this->fields as $field) { - if ($field->getName() == $fieldName) { - return true; - } - } - - return false; - } - - /** - * Removes field from highlighting. - * - * @param string $fieldName + * {@inheritdoc} * * @return Highlight */ - public function removeField($fieldName) + public function set(array $builders) { - /** @var Field $field */ - foreach ($this->fields as $key => $field) { - if ($field->getName() == $fieldName) { - unset($this->fields[$key]); - break; - } - } + parent::set($builders); return $this; } - /** - * Returns all fields to highlight. - * - * @return array - */ - public function getFields() - { - return $this->fields; - } - /** * Sets html tag and its class used in highlighting. * @@ -240,8 +197,8 @@ class Highlight } } - /** @var Field $field */ - foreach ($this->getFields() as $field) { + /** @var FriendlyBuilderInterface $field */ + foreach ($this->all() as $field) { $highlight['fields'][$field->getName()] = $field->toArray(); } -- GitLab