Skip to content
Snippets Groups Projects
Commit 61dd80ec authored by Martynas Sudintas's avatar Martynas Sudintas
Browse files

implemented FriendlyBuilderBag

parent 31a93297
No related branches found
No related tags found
No related merge requests found
......@@ -11,13 +11,16 @@
namespace ONGR\ElasticsearchBundle\DSL\Aggregation;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\FriendlyBuilderBag;
use ONGR\ElasticsearchBundle\DSL\FriendlyBuilderInterface;
/**
* AbstractAggregation class.
*/
abstract class AbstractAggregation implements BuilderInterface
abstract class AbstractAggregation implements FriendlyBuilderInterface
{
const PREFIX = 'agg_';
/**
* @var string
*/
......@@ -29,7 +32,7 @@ abstract class AbstractAggregation implements BuilderInterface
protected $name;
/**
* @var Aggregations
* @var FriendlyBuilderBag
*/
public $aggregations;
......@@ -58,7 +61,7 @@ abstract class AbstractAggregation implements BuilderInterface
public function __construct($name)
{
$this->name = $name;
$this->aggregations = new Aggregations();
$this->aggregations = new FriendlyBuilderBag();
}
/**
......@@ -78,11 +81,11 @@ abstract class AbstractAggregation implements BuilderInterface
}
/**
* @return string
* {@inheritdoc}
*/
public function getName()
{
return Aggregations::PREFIX . $this->name;
return self::PREFIX . $this->name;
}
/**
......
<?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\Aggregation;
/**
* Aggregations class.
*/
class Aggregations
{
const PREFIX = 'agg_';
/**
* @var array
*/
private $aggregations = [];
/**
* @param AbstractAggregation $agg
*/
public function addAggregation(AbstractAggregation $agg)
{
$this->aggregations[$agg->getName()] = $agg;
}
/**
* Checks if aggregation is set.
*
* @param string $name
*
* @return bool
*/
public function has($name)
{
return isset($this->aggregations[$name]);
}
/**
* Removes aggregation by it's name.
*
* @param string $name
*/
public function remove($name)
{
unset($this->aggregations[$name]);
}
/**
* Gets aggregation by it's name.
*
* @param string $name
*
* @return AbstractAggregation
*/
public function get($name)
{
return $this->aggregations[$name];
}
/**
* Returns all aggregations.
*
* @param string|null $type
*
* @return AbstractAggregation[]
*/
public function all($type = null)
{
return array_filter(
$this->aggregations,
function ($aggregation) use ($type) {
/** @var AbstractAggregation $aggregation */
return $type === null || $aggregation->getType() == $type;
}
);
}
}
<?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;
/**
* Container for friendly builders.
*/
class FriendlyBuilderBag
{
/**
* @var FriendlyBuilderInterface[]
*/
private $bag = [];
/**
* @param FriendlyBuilderInterface[] $builders
*/
public function __construct(array $builders = [])
{
$this->set($builders);
}
/**
* Replaces builders with new ones.
*
* @param FriendlyBuilderInterface[] $builders
*/
public function set(array $builders)
{
foreach ($builders as $builder) {
$this->add($builder);
}
}
/**
* Adds a builder.
*
* @param FriendlyBuilderInterface $builder
*/
public function add(FriendlyBuilderInterface $builder)
{
$this->bag[$builder->getName()] = $builder;
}
/**
* Checks if builder is set by name.
*
* @param string $name Builder name.
*
* @return bool
*/
public function has($name)
{
return isset($this->bag[$name]);
}
/**
* Removes a builder by name.
*
* @param string $name Builder name.
*/
public function remove($name)
{
unset($this->bag[$name]);
}
/**
* Clears contained builders.
*/
public function clear()
{
$this->bag = [];
}
/**
* Returns a builder by name.
*
* @param string $name Builder name.
*
* @return FriendlyBuilderInterface
*/
public function get($name)
{
return $this->bag[$name];
}
/**
* Returns all builders contained.
*
* @param string|null $name Builder name.
*
* @return FriendlyBuilderInterface[]
*/
public function all($name = null)
{
return array_filter(
$this->bag,
function ($builder) use ($name) {
/** @var FriendlyBuilderInterface $builder */
return $name === null || $builder->getName() == $name;
}
);
}
}
<?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;
/**
* Interface used by builders with names.
*/
interface FriendlyBuilderInterface extends BuilderInterface
{
/**
* Returns builder name.
*
* @return string
*/
public function getName();
}
......@@ -12,7 +12,6 @@
namespace ONGR\ElasticsearchBundle\DSL;
use ONGR\ElasticsearchBundle\DSL\Aggregation\AbstractAggregation;
use ONGR\ElasticsearchBundle\DSL\Aggregation\Aggregations;
use ONGR\ElasticsearchBundle\DSL\Bool\Bool;
use ONGR\ElasticsearchBundle\DSL\Filter\PostFilter;
use ONGR\ElasticsearchBundle\DSL\Highlight\Highlight;
......@@ -21,7 +20,6 @@ use ONGR\ElasticsearchBundle\DSL\Query\Query;
use ONGR\ElasticsearchBundle\DSL\Sort\AbstractSort;
use ONGR\ElasticsearchBundle\DSL\Sort\Sorts;
use ONGR\ElasticsearchBundle\DSL\Suggester\AbstractSuggester;
use ONGR\ElasticsearchBundle\DSL\Suggester\Suggesters;
/**
* Search object that can be executed by a manager.
......@@ -91,7 +89,7 @@ class Search
private $scriptFields;
/**
* @var Suggesters
* @var FriendlyBuilderBag
*/
private $suggesters;
......@@ -116,7 +114,7 @@ class Search
private $stats;
/**
* @var Aggregations
* @var FriendlyBuilderBag
*/
private $aggregations;
......@@ -361,9 +359,9 @@ class Search
public function addAggregation($agg)
{
if ($this->aggregations === null) {
$this->aggregations = new Aggregations();
$this->aggregations = new FriendlyBuilderBag();
}
$this->aggregations->addAggregation($agg);
$this->aggregations->add($agg);
return $this;
}
......@@ -406,7 +404,7 @@ class Search
public function addSuggester(AbstractSuggester $suggester)
{
if ($this->suggesters === null) {
$this->suggesters = new Suggesters();
$this->suggesters = new FriendlyBuilderBag();
}
$this->suggesters->add($suggester);
......@@ -475,7 +473,7 @@ class Search
}
/**
* @return Aggregations
* @return FriendlyBuilderBag
*/
public function getAggregations()
{
......@@ -611,7 +609,7 @@ class Search
}
/**
* @return Suggesters
* @return FriendlyBuilderBag
*/
public function getSuggesters()
{
......
......@@ -11,12 +11,12 @@
namespace ONGR\ElasticsearchBundle\DSL\Suggester;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\FriendlyBuilderInterface;
/**
* AbstractSuggester class.
*/
abstract class AbstractSuggester implements BuilderInterface
abstract class AbstractSuggester implements FriendlyBuilderInterface
{
/**
* @var string
......@@ -96,7 +96,7 @@ abstract class AbstractSuggester implements BuilderInterface
}
/**
* @return string
* {@inheritdoc}
*/
public function getName()
{
......
<?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\Suggester;
/**
* Suggesters class.
*/
class Suggesters
{
/**
* @var AbstractSuggester
*/
private $suggesters = [];
/**
* Adds a suggester.
*
* @param AbstractSuggester $suggester
*/
public function add(AbstractSuggester $suggester)
{
$this->suggesters[$suggester->getName()] = $suggester;
}
/**
* Checks if suggester is set.
*
* @param string $name
*
* @return bool
*/
public function has($name)
{
return isset($this->suggesters[$name]);
}
/**
* Removes suggester.
*
* @param string $name
*/
public function remove($name)
{
unset($this->suggesters[$name]);
}
/**
* Gets a suggester by it's name.
*
* @param string $name
*
* @return AbstractSuggester
*/
public function get($name)
{
return $this->suggesters[$name];
}
/**
* Gets all suggesters.
*
* @param string|null $name
*
* @return AbstractSuggester[]
*/
public function all($name = null)
{
return array_filter(
$this->suggesters,
function ($build) use ($name) {
/** @var AbstractSuggester $suggester */
return $name === null || $suggester->getName() == $name;
}
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment