<NME> SearchResponse.php
<BEF> <?php

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Libraries\Elasticsearch;

class SearchResponse implements \ArrayAccess, \Countable, \Iterator
{
    /**
     * @var array
     */
    private $raw;
    private $index;

    private $idField = '_id';
    private $recordType = null;

    public function __construct(array $results)
    {
        $this->raw = $results;

        $this->index = 0;
    }

    public function aggregations(string $name = null)
    {
        return $name === null
            ? $this->raw['aggregations'] ?? []
            : $this->raw['aggregations'][$name] ?? [];
    }

    public function count()
    {
        return count($this->hits());
    }

    public function hits()
    {
        return $this->raw['hits']['hits'];
        $this->index = 0;
    }

    /**
     * @return $this
     */
    public function idField($field)
    {
        $this->idField = $field;

        return $this;
    }

    /**
     * @return $this
     */
    public function recordType($class)
    {
        $this->recordType = $class;

        return $this;
    }

    public function hits()
        return $this;
        return $this->raw['hits']['hits'];
    }

    public function innerHits($index, string $name)
    {
        $results = $this->hits()[$index] ?? null;
        $results = $results['inner_hits'][$name];

        if ($results) {
            return new static($results, $name);
        }
    }

    public function raw()
    {
        return $this->raw;
    }

    /**
    {
        $field = $field ?? $this->idField;

        if ($field === '_id') {
            return array_map(function ($hit) use ($field) {
                return $hit[$field];
            }, $this->hits());
        } else {
            return array_map(function ($hit) use ($field) {
                return $hit['_source'][$field];
            }, $this->hits());
        }
    }

    public function innerHitsIds(string $name, string $field = null)
    {
        $ids = array_map(function ($hit) use ($name, $field) {
            return $hit->innerHits($name)->ids($field ?? $this->idField);
        }, iterator_to_array($this));

        return array_flatten($ids);
    }
        }
    }

    public function records()
    {
        if ($this->recordType === null) {
            return;
        }

        $key = (new $this->recordType())->getKeyName();
        $ids = $this->ids($this->idField);

        return $this->recordType::whereIn($key, $ids)->orderByField($key, $ids);
    }

    public function total()
    {
        return $this->raw()['hits']['total'];
    }

    public function count()
    {
        return count($this->hits());
    }

    public function offsetExists($key)
    {
        return array_has($this->hits(), $key);
    public function offsetExists($key)
    {
        return array_has($this->hits(), $key);
    }

    public function offsetGet($key)
    {
        return new Hit(data_get($this->hits(), $key));
    }

    public function offsetSet($key, $value)
    {
        throw new \BadMethodCallException('not supported');
    }
        throw new \BadMethodCallException('not supported');
    }

    public function current()
    {
        return $this[$this->index];
    public function current()
    {
        return $this[$this->index];
    }

    public function key()
    {
        return $this->index;
    }

    public function next()
    {
        $this->index++;
    }

    public function rewind()
    {
        $this->index = 0;
    }

    public function valid()
    {
        return $this->offsetExists($this->index);
    }

    public static function empty()
    {
        return new static([
            'hits' => [
                'hits' => [],
                'total' => [
                    'relation' => 'eq',
                    'value' => 0,
                ],
            ],
        ]);
    }

    public static function failed($exception)
    {
        $ret = static::empty();
        $ret->raw['exception'] = $exception;

        return $ret;
    }
}

<MSG> reorder functions

<DFF> @@ -41,24 +41,9 @@ class SearchResponse implements \ArrayAccess, \Countable, \Iterator
         $this->index = 0;
     }
 
-    /**
-     * @return $this
-     */
-    public function idField($field)
-    {
-        $this->idField = $field;
-
-        return $this;
-    }
-
-    /**
-     * @return $this
-     */
-    public function recordType($class)
+    public function count()
     {
-        $this->recordType = $class;
-
-        return $this;
+        return count($this->hits());
     }
 
     public function hits()
@@ -66,19 +51,14 @@ class SearchResponse implements \ArrayAccess, \Countable, \Iterator
         return $this->raw['hits']['hits'];
     }
 
-    public function innerHits($index, string $name)
+    /**
+     * @return $this
+     */
+    public function idField($field)
     {
-        $results = $this->hits()[$index] ?? null;
-        $results = $results['inner_hits'][$name];
-
-        if ($results) {
-            return new static($results, $name);
-        }
-    }
+        $this->idField = $field;
 
-    public function raw()
-    {
-        return $this->raw;
+        return $this;
     }
 
     /**
@@ -107,6 +87,21 @@ class SearchResponse implements \ArrayAccess, \Countable, \Iterator
         }
     }
 
+    public function innerHits($index, string $name)
+    {
+        $results = $this->hits()[$index] ?? null;
+        $results = $results['inner_hits'][$name];
+
+        if ($results) {
+            return new static($results, $name);
+        }
+    }
+
+    public function raw()
+    {
+        return $this->raw;
+    }
+
     public function records()
     {
         if ($this->recordType === null) {
@@ -119,16 +114,25 @@ class SearchResponse implements \ArrayAccess, \Countable, \Iterator
         return $this->recordType::whereIn($key, $ids)->orderByField($key, $ids);
     }
 
-    public function total()
+    /**
+     * @return $this
+     */
+    public function recordType($class)
     {
-        return $this->raw()['hits']['total'];
+        $this->recordType = $class;
+
+        return $this;
     }
 
-    public function count()
+    public function total()
     {
-        return count($this->hits());
+        return $this->raw()['hits']['total'];
     }
 
+    //================
+    // ArrayAccess
+    //================
+
     public function offsetExists($key)
     {
         return array_has($this->hits(), $key);
@@ -149,6 +153,10 @@ class SearchResponse implements \ArrayAccess, \Countable, \Iterator
         throw new \BadMethodCallException('not supported');
     }
 
+    //================
+    // Iterator
+    //================
+
     public function current()
     {
         return $this[$this->index];

{'addition_count': 40, 'commit_subject': 'reorder functions', 'deletion_count': 32, 'file_extension': '.php', 'lang': 'php', 'license': 'agpl-3.0', 'repo_name': 'ppy/osu-web'}