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