1<?php
2
3namespace Elastica;
4
5/**
6 * Elastica tools.
7 *
8 * @author Nicolas Ruflin <spam@ruflin.com>
9 * @author Thibault Duplessis <thibault.duplessis@gmail.com>
10 * @author Oleg Zinchenko <olegz@default-value.com>
11 * @author Roberto Nygaard <roberto@nygaard.es>
12 */
13class Util
14{
15    /** @var array */
16    protected static $dateMathSymbols = ['<', '>', '/', '{', '}', '|', '+', ':', ','];
17
18    /** @var array */
19    protected static $escapedDateMathSymbols = ['%3C', '%3E', '%2F', '%7B', '%7D', '%7C', '%2B', '%3A', '%2C'];
20
21    /**
22     * Checks if date math is already escaped within request URI.
23     *
24     * @param string $requestUri
25     *
26     * @return bool
27     */
28    public static function isDateMathEscaped($requestUri)
29    {
30        // In practice, the only symbol that really needs to be escaped in URI is '/' => '%2F'
31        return false !== \strpos(\strtoupper($requestUri), '%2F');
32    }
33
34    /**
35     * Escapes date math symbols within request URI.
36     *
37     * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.x/date-math-index-names.html
38     *
39     * @param string $requestUri
40     *
41     * @return string
42     */
43    public static function escapeDateMath($requestUri)
44    {
45        if (empty($requestUri)) {
46            return $requestUri;
47        }
48
49        // Check if date math if used at all. Find last '>'. E.g. /<log-{now/d}>,log-2011.12.01/log/_refresh
50        $pos1 = \strrpos($requestUri, '>');
51        if (false === $pos1) {
52            return $requestUri;
53        }
54
55        // Find the position up to which we should escape.
56        // Should be next slash '/' after last '>' E.g. /<log-{now/d}>,log-2011.12.01/log/_refresh
57        $pos2 = \strpos($requestUri, '/', $pos1);
58        $pos2 = false !== $pos2 ? $pos2 : \strlen($requestUri);
59
60        // Cut out the bit we need to escape: /<log-{now/d}>,log-2011.12.01
61        $uriSegment = \substr($requestUri, 0, $pos2);
62
63        // Escape using character map
64        $escapedUriSegment = \str_replace(static::$dateMathSymbols, static::$escapedDateMathSymbols, $uriSegment);
65
66        // '\\{' and '\\}' should not be escaped
67        if (false !== \strpos($uriSegment, '\\\\')) {
68            $escapedUriSegment = \str_replace(['\\\\%7B', '\\\\%7D'], ['\\\\{', '\\\\}'], $escapedUriSegment);
69        }
70
71        // Replace part of the string. E.g. /%3Clog-%7Bnow%2Fd%7D%3E%2Clog-2011.12.01/log/_refresh
72        return \substr_replace($requestUri, $escapedUriSegment, 0, $pos2);
73    }
74
75    /**
76     * Replace known reserved words (e.g. AND OR NOT)
77     * and
78     * escape known special characters (e.g. + - && || ! ( ) { } [ ] ^ " ~ * ? : etc.).
79     *
80     * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-query-string-query.html#_boolean_operators
81     * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-query-string-query.html#_reserved_characters
82     *
83     * @param string $term Query term to replace and escape
84     *
85     * @return string Replaced and escaped query term
86     */
87    public static function replaceBooleanWordsAndEscapeTerm($term)
88    {
89        $result = $term;
90        $result = self::replaceBooleanWords($result);
91        $result = self::escapeTerm($result);
92
93        return $result;
94    }
95
96    /**
97     * Escapes the following terms (because part of the query language)
98     * + - && || ! ( ) { } [ ] ^ " ~ * ? : \ < >.
99     *
100     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters
101     *
102     * @param string $term Query term to escape
103     *
104     * @return string Escaped query term
105     */
106    public static function escapeTerm($term)
107    {
108        $result = $term;
109        // \ escaping has to be first, otherwise escaped later once again
110        $escapableChars = ['\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '/'];
111
112        foreach ($escapableChars as $char) {
113            $result = \str_replace($char, '\\'.$char, $result);
114        }
115
116        // < and > cannot be escaped, so they should be removed
117        // @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters
118        $nonEscapableChars = ['<', '>'];
119
120        foreach ($nonEscapableChars as $char) {
121            $result = \str_replace($char, '', $result);
122        }
123
124        return $result;
125    }
126
127    /**
128     * Replace the following reserved words (because part of the query language)
129     * AND OR NOT.
130     *
131     * @see http://lucene.apache.org/java/2_4_0/queryparsersyntax.html#Boolean%20operators
132     *
133     * @param string $term Query term to replace
134     *
135     * @return string Replaced query term
136     */
137    public static function replaceBooleanWords($term)
138    {
139        $replacementMap = [' AND ' => ' && ', ' OR ' => ' || ', ' NOT ' => ' !'];
140        $result = \strtr($term, $replacementMap);
141
142        return $result;
143    }
144
145    /**
146     * Converts a snake_case string to CamelCase.
147     *
148     * For example: hello_world to HelloWorld
149     *
150     * @param string $string snake_case string
151     *
152     * @return string CamelCase string
153     */
154    public static function toCamelCase($string)
155    {
156        return \str_replace(' ', '', \ucwords(\str_replace('_', ' ', $string)));
157    }
158
159    /**
160     * Converts a CamelCase string to snake_case.
161     *
162     * For Example HelloWorld to hello_world
163     *
164     * @param string $string CamelCase String to Convert
165     *
166     * @return string SnakeCase string
167     */
168    public static function toSnakeCase($string)
169    {
170        $string = \preg_replace('/([A-Z])/', '_$1', $string);
171
172        return \strtolower(\substr($string, 1));
173    }
174
175    /**
176     * Converts given time to format: 1995-12-31T23:59:59Z.
177     *
178     * This is the lucene date format
179     *
180     * @param int|string $date Date input (could be string etc.) -> must be supported by strtotime
181     *
182     * @return string Converted date string
183     */
184    public static function convertDate($date)
185    {
186        if (\is_int($date)) {
187            $timestamp = $date;
188        } else {
189            $timestamp = \strtotime($date);
190        }
191        $string = \date('Y-m-d\TH:i:s\Z', $timestamp);
192
193        return $string;
194    }
195
196    /**
197     * Convert a \DateTime object to format: 1995-12-31T23:59:59Z+02:00.
198     *
199     * Converts it to the lucene format, including the appropriate TimeZone
200     *
201     * @param \DateTime $dateTime
202     * @param bool      $includeTimezone
203     *
204     * @return string
205     */
206    public static function convertDateTimeObject(\DateTime $dateTime, $includeTimezone = true)
207    {
208        $formatString = 'Y-m-d\TH:i:s'.(true === $includeTimezone ? 'P' : '\Z');
209        $string = $dateTime->format($formatString);
210
211        return $string;
212    }
213
214    /**
215     * Tries to guess the name of the param, based on its class
216     * Example: \Elastica\Query\MatchAll => match_all.
217     *
218     * @param string|object Object or class name
219     *
220     * @return string parameter name
221     */
222    public static function getParamName($class)
223    {
224        if (\is_object($class)) {
225            $class = \get_class($class);
226        }
227
228        $parts = \explode('\\', $class);
229        $last = \array_pop($parts);
230        $last = \preg_replace('/Query$/', '', $last); // for BoolQuery
231
232        return self::toSnakeCase($last);
233    }
234
235    /**
236     * Converts Request to Curl console command.
237     *
238     * @param Request $request
239     *
240     * @return string
241     */
242    public static function convertRequestToCurlCommand(Request $request)
243    {
244        $message = 'curl -X'.\strtoupper($request->getMethod()).' ';
245        $message .= '\'http://'.$request->getConnection()->getHost().':'.$request->getConnection()->getPort().'/';
246        $message .= $request->getPath();
247
248        $query = $request->getQuery();
249        if (!empty($query)) {
250            $message .= '?'.\http_build_query($query);
251        }
252
253        $message .= '\'';
254
255        $data = $request->getData();
256        if (!empty($data)) {
257            $message .= ' -d \''.JSON::stringify($data).'\'';
258        }
259
260        return $message;
261    }
262}
263