From 284f60784e933ce7becddcf34f80313e747d1dbf Mon Sep 17 00:00:00 2001
From: Aivaras Gotovskis <aivaras.gotovskis@ongr.io>
Date: Thu, 9 Jul 2015 16:00:48 +0300
Subject: [PATCH] Add constructor arguments to StatsAggregation.

---
 src/Aggregation/StatsAggregation.php       | 15 +++++++++++++++
 tests/Aggregation/StatsAggregationTest.php | 19 +++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/src/Aggregation/StatsAggregation.php b/src/Aggregation/StatsAggregation.php
index 515e8a3..425bea5 100644
--- a/src/Aggregation/StatsAggregation.php
+++ b/src/Aggregation/StatsAggregation.php
@@ -22,6 +22,21 @@ class StatsAggregation extends AbstractAggregation
     use MetricTrait;
     use ScriptAwareTrait;
 
+    /**
+     * Inner aggregations container init.
+     *
+     * @param string $name
+     * @param string $field
+     * @param string $script
+     */
+    public function __construct($name, $field = null, $script = null)
+    {
+        parent::__construct($name);
+
+        $this->setField($field);
+        $this->setScript($script);
+    }
+
     /**
      * {@inheritdoc}
      */
diff --git a/tests/Aggregation/StatsAggregationTest.php b/tests/Aggregation/StatsAggregationTest.php
index 621e0cf..f826b39 100644
--- a/tests/Aggregation/StatsAggregationTest.php
+++ b/tests/Aggregation/StatsAggregationTest.php
@@ -31,4 +31,23 @@ class StatsAggregationTest extends \PHPUnit_Framework_TestCase
 
         $this->assertEquals($expectedResult, $aggregation->toArray());
     }
+
+    /**
+     * Tests if parameter can be passed to constructor.
+     */
+    public function testConstructor()
+    {
+        $aggregation = new StatsAggregation('foo', 'fieldValue', 'scriptValue');
+        $this->assertSame(
+            [
+                'agg_foo' => [
+                    'stats' => [
+                        'field' => 'fieldValue',
+                        'script' => 'scriptValue',
+                    ],
+                ],
+            ],
+            $aggregation->toArray()
+        );
+    }
 }
-- 
GitLab