1<?php
2
3namespace Elastica\Index;
4
5use Elastica\Index as BaseIndex;
6use Elastica\Response;
7
8/**
9 * Elastica index stats object.
10 *
11 * @author Nicolas Ruflin <spam@ruflin.com>
12 *
13 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html
14 */
15class Stats
16{
17    /**
18     * Response.
19     *
20     * @var Response Response object
21     */
22    protected $_response;
23
24    /**
25     * Stats info.
26     *
27     * @var array Stats info
28     */
29    protected $_data = [];
30
31    /**
32     * Index.
33     *
34     * @var BaseIndex Index object
35     */
36    protected $_index;
37
38    /**
39     * Construct.
40     *
41     * @param BaseIndex $index Index object
42     */
43    public function __construct(BaseIndex $index)
44    {
45        $this->_index = $index;
46        $this->refresh();
47    }
48
49    /**
50     * Returns the raw stats info.
51     *
52     * @return array Stats info
53     */
54    public function getData(): array
55    {
56        return $this->_data;
57    }
58
59    /**
60     * Returns the entry in the data array based on the params.
61     * Various params possible.
62     *
63     * @return mixed Data array entry or null if not found
64     */
65    public function get()
66    {
67        $data = $this->getData();
68
69        foreach (\func_get_args() as $arg) {
70            if (isset($data[$arg])) {
71                $data = $data[$arg];
72            } else {
73                return null;
74            }
75        }
76
77        return $data;
78    }
79
80    /**
81     * Returns the index object.
82     *
83     * @return BaseIndex Index object
84     */
85    public function getIndex(): BaseIndex
86    {
87        return $this->_index;
88    }
89
90    /**
91     * Returns response object.
92     *
93     * @return Response Response object
94     */
95    public function getResponse(): Response
96    {
97        return $this->_response;
98    }
99
100    /**
101     * Reloads all status data of this object.
102     */
103    public function refresh()
104    {
105        $this->_response = $this->getIndex()->requestEndpoint(new \Elasticsearch\Endpoints\Indices\Stats());
106        $this->_data = $this->getResponse()->getData();
107    }
108}
109