1<?php
2
3namespace Elastica;
4
5use Elastica\Exception\ClientException;
6use Elastica\Exception\ConnectionException;
7use Elastica\Exception\InvalidException;
8use Elastica\Exception\ResponseException;
9
10/**
11 * Elastica index template object.
12 *
13 * @author Dmitry Balabka <dmitry.balabka@gmail.com>
14 *
15 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
16 */
17class IndexTemplate
18{
19    /**
20     * Index template name.
21     *
22     * @var string Index pattern
23     */
24    protected $_name;
25
26    /**
27     * @var Client
28     */
29    protected $_client;
30
31    /**
32     * Creates a new index template object.
33     *
34     * @param string $name Index template name
35     *
36     * @throws InvalidException
37     */
38    public function __construct(Client $client, $name)
39    {
40        $this->_client = $client;
41
42        if (!\is_scalar($name)) {
43            throw new InvalidException('Index template should be a scalar type');
44        }
45        $this->_name = (string) $name;
46    }
47
48    /**
49     * Deletes the index template.
50     *
51     * @throws ClientException
52     * @throws ConnectionException
53     * @throws ResponseException
54     *
55     * @return Response
56     */
57    public function delete()
58    {
59        return $this->request(Request::DELETE);
60    }
61
62    /**
63     * Creates a new index template with the given arguments.
64     *
65     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
66     *
67     * @param array $args OPTIONAL Arguments to use
68     *
69     * @throws ClientException
70     * @throws ConnectionException
71     * @throws ResponseException
72     *
73     * @return Response
74     */
75    public function create(array $args = [])
76    {
77        return $this->request(Request::PUT, $args);
78    }
79
80    /**
81     * Checks if the given index template is already created.
82     *
83     * @throws ClientException
84     * @throws ConnectionException
85     * @throws ResponseException
86     *
87     * @return bool
88     */
89    public function exists()
90    {
91        $response = $this->request(Request::HEAD);
92
93        return 200 === $response->getStatus();
94    }
95
96    /**
97     * Returns the index template name.
98     *
99     * @return string Index name
100     */
101    public function getName()
102    {
103        return $this->_name;
104    }
105
106    /**
107     * Returns index template client.
108     *
109     * @return Client
110     */
111    public function getClient()
112    {
113        return $this->_client;
114    }
115
116    /**
117     * Makes calls to the elasticsearch server based on this index template name.
118     *
119     * @param string $method Rest method to use (GET, POST, DELETE, PUT)
120     * @param array  $data   OPTIONAL Arguments as array
121     *
122     * @throws ClientException
123     * @throws ConnectionException
124     * @throws ResponseException
125     *
126     * @return Response
127     */
128    public function request($method, $data = [])
129    {
130        $path = '_template/'.$this->getName();
131
132        return $this->getClient()->request($path, $method, $data);
133    }
134}
135