1<?php
2
3declare(strict_types = 1);
4
5namespace Elasticsearch\Endpoints;
6
7use Elasticsearch\Common\Exceptions\RuntimeException;
8
9/**
10 * Class Explain
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 Explain extends AbstractEndpoint
19{
20    public function setBody($body): Explain
21    {
22        if (isset($body) !== true) {
23            return $this;
24        }
25
26        $this->body = $body;
27
28        return $this;
29    }
30
31    /**
32     * @throws RuntimeException
33     */
34    public function getURI(): string
35    {
36        if (isset($this->id) !== true) {
37            throw new RuntimeException(
38                'id is required for Explain'
39            );
40        }
41        if (isset($this->index) !== true) {
42            throw new RuntimeException(
43                'index is required for Explain'
44            );
45        }
46        $id = $this->id;
47        $index = $this->index;
48        $type = $this->type ?? null;
49
50        $uri = isset($type)
51            ? "/$index/$type/$id/_explain"
52            : "/$index/_explain/$id";
53
54        return $uri;
55    }
56
57    public function getParamWhitelist(): array
58    {
59        return [
60            'analyze_wildcard',
61            'analyzer',
62            'default_operator',
63            'df',
64            'stored_fields',
65            'lenient',
66            'parent',
67            'preference',
68            'q',
69            'routing',
70            '_source',
71            '_source_excludes',
72            '_source_includes'
73        ];
74    }
75
76    public function getMethod(): string
77    {
78        return isset($this->body) ? 'POST' : 'GET';
79    }
80}
81