1<?php
2
3namespace Elastica\Index;
4
5use Elastica\Index;
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 Index
35     */
36    protected $_index;
37
38    /**
39     * Construct.
40     */
41    public function __construct(Index $index)
42    {
43        $this->_index = $index;
44        $this->refresh();
45    }
46
47    /**
48     * Returns the raw stats info.
49     *
50     * @return array Stats info
51     */
52    public function getData(): array
53    {
54        return $this->_data;
55    }
56
57    /**
58     * Returns the entry in the data array based on the params.
59     * Various params possible.
60     *
61     * @return mixed Data array entry or null if not found
62     */
63    public function get(...$args)
64    {
65        $data = $this->getData();
66
67        foreach ($args as $arg) {
68            if (isset($data[$arg])) {
69                $data = $data[$arg];
70            } else {
71                return null;
72            }
73        }
74
75        return $data;
76    }
77
78    /**
79     * Returns the index object.
80     */
81    public function getIndex(): Index
82    {
83        return $this->_index;
84    }
85
86    /**
87     * Returns response object.
88     *
89     * @return Response Response object
90     */
91    public function getResponse(): Response
92    {
93        return $this->_response;
94    }
95
96    /**
97     * Reloads all status data of this object.
98     */
99    public function refresh(): void
100    {
101        $this->_response = $this->getIndex()->requestEndpoint(new \Elasticsearch\Endpoints\Indices\Stats());
102        $this->_data = $this->getResponse()->getData();
103    }
104}
105