1<?php
2
3declare(strict_types = 1);
4
5namespace Elasticsearch\Endpoints;
6
7use Elasticsearch\Common\Exceptions\RuntimeException;
8
9/**
10 * Class Index
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 Index extends AbstractEndpoint
19{
20    public function setBody($body): Index
21    {
22        if (isset($body) !== true) {
23            return $this;
24        }
25        $this->body = $body;
26        return $this;
27    }
28
29    /**
30     * @throws RuntimeException
31     */
32    public function getURI(): string
33    {
34        if (isset($this->index) !== true) {
35            throw new RuntimeException(
36                'index is required for Index'
37            );
38        }
39
40        $id    = $this->id ?? null;
41        $index = $this->index;
42        $type  = $this->type ?? '_doc';
43
44        if (isset($id)) {
45            return "/$index/$type/$id";
46        }
47        return "/$index/$type";
48    }
49
50    public function getParamWhitelist(): array
51    {
52        return [
53            'wait_for_active_shards',
54            'op_type',
55            'parent',
56            'refresh',
57            'routing',
58            'timeout',
59            'version',
60            'version_type',
61            'if_seq_no',
62            'if_primary_term',
63            'pipeline'
64        ];
65    }
66
67    public function getMethod(): string
68    {
69        return 'POST';
70    }
71
72    /**
73     * @throws RuntimeException
74     */
75    public function getBody()
76    {
77        if (isset($this->body) !== true) {
78            throw new RuntimeException('Document body must be set for index request');
79        } else {
80            return $this->body;
81        }
82    }
83}
84