summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/src/Common/JsonPath.php
diff options
context:
space:
mode:
authormanzerbredes <loic.guegan_secondary@yahoo.fr>2016-03-15 16:17:39 +0100
committermanzerbredes <loic.guegan_secondary@yahoo.fr>2016-03-15 16:17:39 +0100
commit26d10bc0fa4befbac54453228ae1ce89021bdec2 (patch)
tree029d7240ecf7416205e5f76cf9107a6b5bdf8ca3 /server/vendor/php-opencloud/common/src/Common/JsonPath.php
parent8ad216dedf017f3d6de047a25d08db3b98e16361 (diff)
parent03ef74d0cfe675a6e18a91f039182ca1b248d8f5 (diff)
Merge branch 'develop' into loic
Diffstat (limited to 'server/vendor/php-opencloud/common/src/Common/JsonPath.php')
-rw-r--r--server/vendor/php-opencloud/common/src/Common/JsonPath.php119
1 files changed, 119 insertions, 0 deletions
diff --git a/server/vendor/php-opencloud/common/src/Common/JsonPath.php b/server/vendor/php-opencloud/common/src/Common/JsonPath.php
new file mode 100644
index 0000000..0a6372e
--- /dev/null
+++ b/server/vendor/php-opencloud/common/src/Common/JsonPath.php
@@ -0,0 +1,119 @@
+<?php
+
+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($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($path, $value, $json)
+ {
+ $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($path)
+ {
+ return $this->getPath($path, $this->jsonStructure);
+ }
+
+ /**
+ * Internal method for recursion.
+ *
+ * @param $path
+ * @param $json
+ * @return null
+ */
+ private function getPath($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]);
+ }
+ }
+}