summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/src/Common/Transport/JsonSerializer.php
blob: 11b31d3ca00706d74563261b57d9d279e6cbe791 (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
<?php

namespace OpenCloud\Common\Transport;

use OpenCloud\Common\Api\Parameter;
use OpenCloud\Common\JsonPath;

/**
 * Class responsible for populating the JSON body of a {@see GuzzleHttp\Message\Request} object.
 *
 * @package OpenCloud\Common\Transport
 */
class JsonSerializer
{
    /**
     * Populates the actual value into a JSON field, i.e. it has reached the end of the line and no
     * further nesting is required.
     *
     * @param Parameter $param     The schema that defines how the JSON field is being populated
     * @param mixed     $userValue The user value that is populating a JSON field
     * @param array     $json      The existing JSON structure that will be populated
     *
     * @return array|mixed
     */
    private function stockValue(Parameter $param, $userValue, $json)
    {
        $name = $param->getName();
        if ($path = $param->getPath()) {
            $jsonPath = new JsonPath($json);
            $jsonPath->set(sprintf("%s.%s", $path, $name), $userValue);
            $json = $jsonPath->getStructure();
        } elseif ($name) {
            $json[$name] = $userValue;
        } else {
            $json[] = $userValue;
        }

        return $json;
    }

    /**
     * Populates a value into an array-like structure.
     *
     * @param Parameter $param     The schema that defines how the JSON field is being populated
     * @param mixed     $userValue The user value that is populating a JSON field
     *
     * @return array|mixed
     */
    private function stockArrayJson(Parameter $param, $userValue)
    {
        $elems = [];
        foreach ($userValue as $item) {
            $elems = $this->stockJson($param->getItemSchema(), $item, $elems);
        }
        return $elems;
    }

    /**
     * Populates a value into an object-like structure.
     *
     * @param Parameter $param     The schema that defines how the JSON field is being populated
     * @param mixed     $userValue The user value that is populating a JSON field
     *
     * @return array
     */
    private function stockObjectJson(Parameter $param, $userValue)
    {
        $object = [];
        foreach ($userValue as $key => $val) {
            $object = $this->stockJson($param->getProperty($key), $val, $object);
        }
        return $object;
    }

    /**
     * A generic method that will populate a JSON structure with a value according to a schema. It
     * supports multiple types and will delegate accordingly.
     *
     * @param Parameter $param     The schema that defines how the JSON field is being populated
     * @param mixed     $userValue The user value that is populating a JSON field
     * @param array     $json      The existing JSON structure that will be populated
     *
     * @return array
     */
    public function stockJson(Parameter $param, $userValue, $json)
    {
        if ($param->isArray()) {
            $userValue = $this->stockArrayJson($param, $userValue);
        } elseif ($param->isObject()) {
            $userValue = $this->stockObjectJson($param, $userValue);
        }
        // Populate the final value
        return $this->stockValue($param, $userValue, $json);
    }
}