Skip to content
Snippets Groups Projects
Commit b448600b authored by Simonas Šerlinskas's avatar Simonas Šerlinskas
Browse files

introducing builder bag instead of named builder bag

parent b263d6b9
No related branches found
No related tags found
No related merge requests found
...@@ -11,18 +11,18 @@ ...@@ -11,18 +11,18 @@
namespace ONGR\ElasticsearchDSL\Aggregation; namespace ONGR\ElasticsearchDSL\Aggregation;
use ONGR\ElasticsearchDSL\NamedBuilderBag; use ONGR\ElasticsearchDSL\BuilderBag;
use ONGR\ElasticsearchDSL\NamedBuilderInterface; use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\NameAwareTrait;
use ONGR\ElasticsearchDSL\ParametersTrait; use ONGR\ElasticsearchDSL\ParametersTrait;
/** /**
* AbstractAggregation class. * AbstractAggregation class.
*/ */
abstract class AbstractAggregation implements NamedBuilderInterface abstract class AbstractAggregation implements BuilderInterface
{ {
use ParametersTrait; use ParametersTrait;
use NameAwareTrait;
const PREFIX = 'agg_';
/** /**
* @var string * @var string
...@@ -30,20 +30,10 @@ abstract class AbstractAggregation implements NamedBuilderInterface ...@@ -30,20 +30,10 @@ abstract class AbstractAggregation implements NamedBuilderInterface
private $field; private $field;
/** /**
* @var string * @var BuilderBag
*/
private $name;
/**
* @var NamedBuilderBag
*/ */
private $aggregations; private $aggregations;
/**
* @return string
*/
abstract public function getType();
/** /**
* Abstract supportsNesting method. * Abstract supportsNesting method.
* *
...@@ -63,9 +53,7 @@ abstract class AbstractAggregation implements NamedBuilderInterface ...@@ -63,9 +53,7 @@ abstract class AbstractAggregation implements NamedBuilderInterface
*/ */
public function __construct($name) public function __construct($name)
{ {
$this->name = $name; $this->setName($name);
$this->aggregations = new NamedBuilderBag();
} }
/** /**
...@@ -84,14 +72,6 @@ abstract class AbstractAggregation implements NamedBuilderInterface ...@@ -84,14 +72,6 @@ abstract class AbstractAggregation implements NamedBuilderInterface
return $this->field; return $this->field;
} }
/**
* {@inheritdoc}
*/
public function getName()
{
return self::PREFIX . $this->name;
}
/** /**
* Adds a sub-aggregation. * Adds a sub-aggregation.
* *
...@@ -99,17 +79,25 @@ abstract class AbstractAggregation implements NamedBuilderInterface ...@@ -99,17 +79,25 @@ abstract class AbstractAggregation implements NamedBuilderInterface
*/ */
public function addAggregation(AbstractAggregation $abstractAggregation) public function addAggregation(AbstractAggregation $abstractAggregation)
{ {
if (!$this->aggregations) {
$this->aggregations = $this->createBuilderBag();
}
$this->aggregations->add($abstractAggregation); $this->aggregations->add($abstractAggregation);
} }
/** /**
* Returns all sub aggregations. * Returns all sub aggregations.
* *
* @return AbstractAggregation[] * @return BuilderBag[]
*/ */
public function getAggregations() public function getAggregations()
{ {
return $this->aggregations->all(); if ($this->aggregations) {
return $this->aggregations->all();
} else {
return [];
}
} }
/** /**
...@@ -149,4 +137,14 @@ abstract class AbstractAggregation implements NamedBuilderInterface ...@@ -149,4 +137,14 @@ abstract class AbstractAggregation implements NamedBuilderInterface
return $result; return $result;
} }
/**
* Creates BuilderBag new instance.
*
* @return BuilderBag
*/
private function createBuilderBag()
{
return new BuilderBag();
}
} }
...@@ -14,27 +14,17 @@ namespace ONGR\ElasticsearchDSL; ...@@ -14,27 +14,17 @@ namespace ONGR\ElasticsearchDSL;
/** /**
* Container for named builders. * Container for named builders.
*/ */
class NamedBuilderBag class BuilderBag
{ {
/** /**
* @var NamedBuilderInterface[] * @var BuilderInterface[]
*/ */
private $bag = []; private $bag = [];
/** /**
* @param NamedBuilderInterface[] $builders * @param BuilderInterface[] $builders
*/ */
public function __construct(array $builders = []) public function __construct($builders = [])
{
$this->set($builders);
}
/**
* Replaces builders with new ones.
*
* @param NamedBuilderInterface[] $builders
*/
public function set(array $builders)
{ {
foreach ($builders as $builder) { foreach ($builders as $builder) {
$this->add($builder); $this->add($builder);
...@@ -44,15 +34,25 @@ class NamedBuilderBag ...@@ -44,15 +34,25 @@ class NamedBuilderBag
/** /**
* Adds a builder. * Adds a builder.
* *
* @param NamedBuilderInterface $builder * @param BuilderInterface $builder
*
* @return string
*/ */
public function add(NamedBuilderInterface $builder) public function add(BuilderInterface $builder)
{ {
$this->bag[$builder->getName()] = $builder; if (method_exists($builder, 'getName')) {
$name = $builder->getName();
} else {
$name = uniqid();
}
$this->bag[$name] = $builder;
return $name;
} }
/** /**
* Checks if builder is set by name. * Checks if builder exists by a specific name.
* *
* @param string $name Builder name. * @param string $name Builder name.
* *
...@@ -66,7 +66,7 @@ class NamedBuilderBag ...@@ -66,7 +66,7 @@ class NamedBuilderBag
/** /**
* Removes a builder by name. * Removes a builder by name.
* *
* @param string $name Builder name. * @param string $name builder name.
*/ */
public function remove($name) public function remove($name)
{ {
...@@ -84,9 +84,9 @@ class NamedBuilderBag ...@@ -84,9 +84,9 @@ class NamedBuilderBag
/** /**
* Returns a builder by name. * Returns a builder by name.
* *
* @param string $name Builder name. * @param string $name builder name.
* *
* @return NamedBuilderInterface * @return BuilderInterface
*/ */
public function get($name) public function get($name)
{ {
...@@ -96,16 +96,16 @@ class NamedBuilderBag ...@@ -96,16 +96,16 @@ class NamedBuilderBag
/** /**
* Returns all builders contained. * Returns all builders contained.
* *
* @param string|null $type Builder type. * @param string|null $type builder type.
* *
* @return NamedBuilderInterface[] * @return BuilderInterface[]
*/ */
public function all($type = null) public function all($type = null)
{ {
return array_filter( return array_filter(
$this->bag, $this->bag,
/** @var NamedBuilderInterface $builder */ /** @var BuilderInterface $builder */
function (NamedBuilderInterface $builder) use ($type) { function (BuilderInterface $builder) use ($type) {
return $type === null || $builder->getType() == $type; return $type === null || $builder->getType() == $type;
} }
); );
...@@ -116,11 +116,10 @@ class NamedBuilderBag ...@@ -116,11 +116,10 @@ class NamedBuilderBag
*/ */
public function toArray() public function toArray()
{ {
$out = []; $output = [];
foreach ($this->all() as $builder) { foreach ($this->all() as $builder) {
$out = array_merge($out, $builder->toArray()); $output = array_merge($output, $builder->toArray());
} }
return $output;
return $out;
} }
} }
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