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 GetField
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 GetField extends AbstractEndpoint
20{
21    /**
22     * @var string
23     */
24    private $fields;
25
26    /**
27     * @param string|array $fields
28     */
29    public function setFields($fields): GetField
30    {
31        if (isset($fields) !== true) {
32            return $this;
33        }
34
35        if (is_array($fields) === true) {
36            $fields = implode(",", $fields);
37        }
38
39        $this->fields = $fields;
40
41        return $this;
42    }
43
44    /**
45     * @throws \Elasticsearch\Common\Exceptions\RuntimeException
46     */
47    public function getURI(): string
48    {
49        if (isset($this->fields) !== true) {
50            throw new Exceptions\RuntimeException(
51                'fields is required for Get Field Mapping'
52            );
53        }
54        $fields = $this->fields;
55        $index = $this->index ?? null;
56        $type = $this->type ?? null;
57        if (isset($index) && isset($type)) {
58            return "/$index/_mapping/$type/field/$fields";
59        }
60        if (isset($type)) {
61            return "/_mapping/$type/field/$fields";
62        }
63        if (isset($index)) {
64            return "/$index/_mapping/field/$fields";
65        }
66        return "/_mapping/field/$fields";
67    }
68
69    public function getParamWhitelist(): array
70    {
71        return [
72            'include_type_name',
73            'include_defaults',
74            'ignore_unavailable',
75            'allow_no_indices',
76            'expand_wildcards',
77            'local'
78        ];
79    }
80
81    public function getMethod(): string
82    {
83        return 'GET';
84    }
85}
86