1<?php
2
3declare(strict_types = 1);
4
5namespace Elasticsearch\Endpoints;
6
7use Elasticsearch\Common\Exceptions\RuntimeException;
8
9/**
10 * Class Mget
11 *
12 * @category Elasticsearch
13 * @package  Elasticsearch\Endpoints
14 * @author   Zachary Tong <zach@elastic.co>
15 * @license  http://www.apache.org/licenses/LICENSE-2.0 Apache2
16 * @link     http://elastic.co
17 */
18class Mget extends AbstractEndpoint
19{
20    public function setBody($body): Mget
21    {
22        if (isset($body) !== true) {
23            return $this;
24        }
25
26        $this->body = $body;
27
28        return $this;
29    }
30
31    public function getURI(): string
32    {
33        $index = $this->index ?? null;
34        $type = $this->type ?? null;
35
36        if (isset($index) && isset($type)) {
37            return "/$index/$type/_mget";
38        }
39        if (isset($index)) {
40            return "/$index/_mget";
41        }
42        return '/_mget';
43    }
44
45    public function getParamWhitelist(): array
46    {
47        return [
48            'stored_fields',
49            'preference',
50            'realtime',
51            'refresh',
52            'routing',
53            '_source',
54            '_source_excludes',
55            '_source_includes'
56        ];
57    }
58
59    /**
60     * @throws RuntimeException
61     */
62    public function getBody()
63    {
64        if (isset($this->body) !== true) {
65            throw new RuntimeException('Body is required for MGet');
66        }
67
68        return $this->body;
69    }
70
71    public function getMethod(): string
72    {
73        return isset($this->body) ? 'POST' : 'GET';
74    }
75}
76