summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/src/Common/JsonPath.php
blob: b1a7b349b62ea1cf465150d1f3396f92e40eedd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php declare(strict_types = 1);

namespace OpenCloud\Common;

/**
 * This class allows arbitrary data structures to be inserted into, and extracted from, deep arrays
 * and JSON-serialized strings. Say, for example, that you have this array as an input:
 *
 * <pre><code>['foo' => ['bar' => ['baz' => 'some_value']]]</code></pre>
 *
 * and you wanted to insert or extract an element. Usually, you would use:
 *
 * <pre><code>$array['foo']['bar']['baz'] = 'new_value';</code></pre>
 *
 * but sometimes you do not have access to the variable - so a string representation is needed. Using
 * XPath-like syntax, this class allows you to do this:
 *
 * <pre><code>$jsonPath = new JsonPath($array);
 * $jsonPath->set('foo.bar.baz', 'new_value');
 * $val = $jsonPath->get('foo.bar.baz');
 * </code></pre>
 *
 * @package OpenCloud\Common
 */
class JsonPath
{
    /** @var array */
    private $jsonStructure;

    /**
     * @param $structure The initial data structure to extract from and insert into. Typically this will be a
     *                   multidimensional associative array; but well-formed JSON strings are also acceptable.
     */
    public function __construct($structure)
    {
        $this->jsonStructure = is_string($structure) ? json_decode($structure, true) : $structure;
    }

    /**
     * Set a node in the structure
     *
     * @param $path  The XPath to use
     * @param $value The new value of the node
     */
    public function set(string $path, $value)
    {
        $this->jsonStructure = $this->setPath($path, $value, $this->jsonStructure);
    }

    /**
     * Internal method for recursive calls.
     *
     * @param $path
     * @param $value
     * @param $json
     *
     * @return mixed
     */
    private function setPath(string $path, $value, array $json): array
    {
        $nodes = explode('.', $path);
        $point = array_shift($nodes);

        if (!isset($json[$point])) {
            $json[$point] = [];
        }

        if (!empty($nodes)) {
            $json[$point] = $this->setPath(implode('.', $nodes), $value, $json[$point]);
        } else {
            $json[$point] = $value;
        }

        return $json;
    }

    /**
     * Return the updated structure.
     *
     * @return mixed
     */
    public function getStructure()
    {
        return $this->jsonStructure;
    }

    /**
     * Get a path's value. If no path can be matched, NULL is returned.
     *
     * @param $path
     *
     * @return mixed|null
     */
    public function get(string $path)
    {
        return $this->getPath($path, $this->jsonStructure);
    }

    /**
     * Internal method for recursion.
     *
     * @param $path
     * @param $json
     *
     * @return null
     */
    private function getPath(string $path, $json)
    {
        $nodes = explode('.', $path);
        $point = array_shift($nodes);

        if (!isset($json[$point])) {
            return null;
        }

        if (empty($nodes)) {
            return $json[$point];
        } else {
            return $this->getPath(implode('.', $nodes), $json[$point]);
        }
    }
}