From b448600bc9ce87487b04d8c6ff2ad55361128b1e 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:47:41 +0300 Subject: [PATCH] introducing builder bag instead of named builder bag --- src/Aggregation/AbstractAggregation.php | 56 ++++++++++---------- src/{NamedBuilderBag.php => BuilderBag.php} | 57 ++++++++++----------- 2 files changed, 55 insertions(+), 58 deletions(-) rename src/{NamedBuilderBag.php => BuilderBag.php} (60%) diff --git a/src/Aggregation/AbstractAggregation.php b/src/Aggregation/AbstractAggregation.php index 8f0ad9c..975cdaf 100644 --- a/src/Aggregation/AbstractAggregation.php +++ b/src/Aggregation/AbstractAggregation.php @@ -11,18 +11,18 @@ namespace ONGR\ElasticsearchDSL\Aggregation; -use ONGR\ElasticsearchDSL\NamedBuilderBag; -use ONGR\ElasticsearchDSL\NamedBuilderInterface; +use ONGR\ElasticsearchDSL\BuilderBag; +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\NameAwareTrait; use ONGR\ElasticsearchDSL\ParametersTrait; /** * AbstractAggregation class. */ -abstract class AbstractAggregation implements NamedBuilderInterface +abstract class AbstractAggregation implements BuilderInterface { use ParametersTrait; - - const PREFIX = 'agg_'; + use NameAwareTrait; /** * @var string @@ -30,20 +30,10 @@ abstract class AbstractAggregation implements NamedBuilderInterface private $field; /** - * @var string - */ - private $name; - - /** - * @var NamedBuilderBag + * @var BuilderBag */ private $aggregations; - /** - * @return string - */ - abstract public function getType(); - /** * Abstract supportsNesting method. * @@ -63,9 +53,7 @@ abstract class AbstractAggregation implements NamedBuilderInterface */ public function __construct($name) { - $this->name = $name; - - $this->aggregations = new NamedBuilderBag(); + $this->setName($name); } /** @@ -84,14 +72,6 @@ abstract class AbstractAggregation implements NamedBuilderInterface return $this->field; } - /** - * {@inheritdoc} - */ - public function getName() - { - return self::PREFIX . $this->name; - } - /** * Adds a sub-aggregation. * @@ -99,17 +79,25 @@ abstract class AbstractAggregation implements NamedBuilderInterface */ public function addAggregation(AbstractAggregation $abstractAggregation) { + if (!$this->aggregations) { + $this->aggregations = $this->createBuilderBag(); + } + $this->aggregations->add($abstractAggregation); } /** * Returns all sub aggregations. * - * @return AbstractAggregation[] + * @return BuilderBag[] */ 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 return $result; } + + /** + * Creates BuilderBag new instance. + * + * @return BuilderBag + */ + private function createBuilderBag() + { + return new BuilderBag(); + } } diff --git a/src/NamedBuilderBag.php b/src/BuilderBag.php similarity index 60% rename from src/NamedBuilderBag.php rename to src/BuilderBag.php index d1a2268..4894764 100644 --- a/src/NamedBuilderBag.php +++ b/src/BuilderBag.php @@ -14,27 +14,17 @@ namespace ONGR\ElasticsearchDSL; /** * Container for named builders. */ -class NamedBuilderBag +class BuilderBag { /** - * @var NamedBuilderInterface[] + * @var BuilderInterface[] */ private $bag = []; /** - * @param NamedBuilderInterface[] $builders + * @param BuilderInterface[] $builders */ - public function __construct(array $builders = []) - { - $this->set($builders); - } - - /** - * Replaces builders with new ones. - * - * @param NamedBuilderInterface[] $builders - */ - public function set(array $builders) + public function __construct($builders = []) { foreach ($builders as $builder) { $this->add($builder); @@ -44,15 +34,25 @@ class NamedBuilderBag /** * 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. * @@ -66,7 +66,7 @@ class NamedBuilderBag /** * Removes a builder by name. * - * @param string $name Builder name. + * @param string $name builder name. */ public function remove($name) { @@ -84,9 +84,9 @@ class NamedBuilderBag /** * Returns a builder by name. * - * @param string $name Builder name. + * @param string $name builder name. * - * @return NamedBuilderInterface + * @return BuilderInterface */ public function get($name) { @@ -96,16 +96,16 @@ class NamedBuilderBag /** * 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) { return array_filter( $this->bag, - /** @var NamedBuilderInterface $builder */ - function (NamedBuilderInterface $builder) use ($type) { + /** @var BuilderInterface $builder */ + function (BuilderInterface $builder) use ($type) { return $type === null || $builder->getType() == $type; } ); @@ -116,11 +116,10 @@ class NamedBuilderBag */ public function toArray() { - $out = []; + $output = []; foreach ($this->all() as $builder) { - $out = array_merge($out, $builder->toArray()); + $output = array_merge($output, $builder->toArray()); } - - return $out; + return $output; } } -- GitLab