From 1d74bb2cceacf87c121e0a1863a1578dde811002 Mon Sep 17 00:00:00 2001
From: Martynas Sudintas <martynas.sudintas@ongr.io>
Date: Fri, 6 Feb 2015 15:49:43 +0200
Subject: [PATCH] cardinality aggregation now must have field or script set

---
 Aggregation/CardinalityAggregation.php | 34 +++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 4 deletions(-)

diff --git a/Aggregation/CardinalityAggregation.php b/Aggregation/CardinalityAggregation.php
index 070f77b..36aec86 100644
--- a/Aggregation/CardinalityAggregation.php
+++ b/Aggregation/CardinalityAggregation.php
@@ -30,21 +30,31 @@ class CardinalityAggregation extends AbstractAggregation
      */
     private $rehash;
 
+    /**
+     * @var string
+     */
+    private $script;
+
     /**
      * {@inheritdoc}
      */
     public function getArray()
     {
-        if (!$this->getField()) {
-            return new \stdClass();
+        $out = [];
+
+        if ($this->getField()) {
+            $out['field'] = $this->getField();
+        } elseif ($this->getScript()) {
+            $out['script'] = $this->getScript();
+        } else {
+            throw new \LogicException('Cardinality aggregation must have field or script set.');
         }
 
-        $out['field'] = $this->getField();
         if ($this->getPrecisionThreshold()) {
             $out['precision_threshold'] = $this->getPrecisionThreshold();
         }
 
-        if ($this->isRehash() !== null) {
+        if ($this->isRehash()) {
             $out['rehash'] = $this->isRehash();
         }
 
@@ -85,6 +95,22 @@ class CardinalityAggregation extends AbstractAggregation
         $this->rehash = $rehash;
     }
 
+    /**
+     * @return string
+     */
+    public function getScript()
+    {
+        return $this->script;
+    }
+
+    /**
+     * @param string $script
+     */
+    public function setScript($script)
+    {
+        $this->script = $script;
+    }
+
     /**
      * {@inheritdoc}
      */
-- 
GitLab