PageRenderTime 48ms CodeModel.GetById 41ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Elastica/JSON.php

Relevant Search: With Applications for Solr and Elasticsearch

For more in depth reading about search, ranking and generally everything you could ever want to know about how lucene, elasticsearch or solr work under the hood I highly suggest this book. Easily one of the most interesting technical books I have read in a long time. If you are tasked with solving search relevance problems even if not in Solr or Elasticsearch it should be your first reference. Amazon Affiliate Link
http://github.com/ruflin/Elastica
PHP | 66 lines | 27 code | 9 blank | 30 comment | 4 complexity | 1535a8bda6e7fc417cfb3ae28e5e9602 MD5 | raw file
 1<?php
 2namespace Elastica;
 3
 4use Elastica\Exception\JSONParseException;
 5
 6/**
 7 * Elastica JSON tools.
 8 */
 9class JSON
10{
11    /**
12     * Parse JSON string to an array.
13     *
14     * @link http://php.net/manual/en/function.json-decode.php
15     * @link http://php.net/manual/en/function.json-last-error.php
16     *
17     * @param string $json JSON string to parse
18     *
19     * @return array PHP array representation of JSON string
20     */
21    public static function parse(/* inherit from json_decode */)
22    {
23        // extract arguments
24        $args = func_get_args();
25
26        // default to decoding into an assoc array
27        if (count($args) === 1) {
28            $args[] = true;
29        }
30
31        // run decode
32        $array = call_user_func_array('json_decode', $args);
33
34        // turn errors into exceptions for easier catching
35        $error = json_last_error();
36        if ($error !== JSON_ERROR_NONE) {
37            throw new JSONParseException($error);
38        }
39
40        // output
41        return $array;
42    }
43
44    /**
45     * Convert input to JSON string with standard options.
46     *
47     * @link http://php.net/manual/en/function.json-encode.php
48     *
49     * @param  mixed check args for PHP function json_encode
50     *
51     * @return string Valid JSON representation of $input
52     */
53    public static function stringify(/* inherit from json_encode */)
54    {
55        // extract arguments
56        $args = func_get_args();
57
58        // allow special options value for Elasticsearch compatibility
59        if (count($args) > 1 && $args[1] === 'JSON_ELASTICSEARCH') {
60            $args[1] = JSON_UNESCAPED_UNICODE;
61        }
62
63        // run encode and output
64        return call_user_func_array('json_encode', $args);
65    }
66}