1<?php
2
3declare(strict_types = 1);
4
5namespace Elasticsearch\Endpoints\Indices\Template;
6
7use Elasticsearch\Endpoints\AbstractEndpoint;
8use Elasticsearch\Common\Exceptions;
9
10/**
11 * Class Put
12 *
13 * @category Elasticsearch
14 * @package  Elasticsearch\Endpoints\Indices\Template
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     * The name of the template
23     *
24     * @var string
25     */
26    private $name;
27
28    /**
29     * @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
30     */
31    public function setBody($body): Put
32    {
33        if (isset($body) !== true) {
34            return $this;
35        }
36
37        $this->body = $body;
38
39        return $this;
40    }
41
42    public function setName(?string $name): Put
43    {
44        if (isset($name) !== true) {
45            return $this;
46        }
47
48        $this->name = $name;
49
50        return $this;
51    }
52
53    /**
54     * @throws \Elasticsearch\Common\Exceptions\RuntimeException
55     */
56    public function getURI(): string
57    {
58        if (isset($this->name) !== true) {
59            throw new Exceptions\RuntimeException(
60                'name is required for Put'
61            );
62        }
63        return "/_template/{$this->name}";
64    }
65
66    public function getParamWhitelist(): array
67    {
68        return [
69            'include_type_name',
70            'order',
71            'create',
72            'timeout',
73            'master_timeout',
74            'flat_settings'
75        ];
76    }
77
78    /**
79     * @throws \Elasticsearch\Common\Exceptions\RuntimeException
80     */
81    public function getBody()
82    {
83        if (isset($this->body) !== true) {
84            throw new Exceptions\RuntimeException('Body is required for Put Template');
85        }
86
87        return $this->body;
88    }
89
90    public function getMethod(): string
91    {
92        return 'PUT';
93    }
94}
95