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