summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/src/Common/Transport/RequestSerializer.php
diff options
context:
space:
mode:
authorEoleDev <root@serverpc.home>2016-03-09 15:36:02 +0100
committerEoleDev <root@serverpc.home>2016-03-09 15:36:02 +0100
commitc479658f0bb953ded4b29ca573404a318f1e798f (patch)
treef4c4fa4b7e245abd462794df5b6525ed41306e77 /server/vendor/php-opencloud/common/src/Common/Transport/RequestSerializer.php
parent5c6f6c97b7b906476ad5b9ed193e0140dd66f977 (diff)
New Library
Diffstat (limited to 'server/vendor/php-opencloud/common/src/Common/Transport/RequestSerializer.php')
-rw-r--r--server/vendor/php-opencloud/common/src/Common/Transport/RequestSerializer.php85
1 files changed, 85 insertions, 0 deletions
diff --git a/server/vendor/php-opencloud/common/src/Common/Transport/RequestSerializer.php b/server/vendor/php-opencloud/common/src/Common/Transport/RequestSerializer.php
new file mode 100644
index 0000000..30f04af
--- /dev/null
+++ b/server/vendor/php-opencloud/common/src/Common/Transport/RequestSerializer.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace OpenCloud\Common\Transport;
+
+use function GuzzleHttp\uri_template;
+use function GuzzleHttp\Psr7\build_query;
+use function GuzzleHttp\Psr7\modify_request;
+use OpenCloud\Common\Api\Operation;
+use OpenCloud\Common\Api\Parameter;
+
+class RequestSerializer
+{
+ private $jsonSerializer;
+
+ public function __construct(JsonSerializer $jsonSerializer = null)
+ {
+ $this->jsonSerializer = $jsonSerializer ?: new JsonSerializer();
+ }
+
+ public function serializeOptions(Operation $operation, array $userValues = [])
+ {
+ $options = ['headers' => []];
+
+ foreach ($userValues as $paramName => $paramValue) {
+ if (null === ($schema = $operation->getParam($paramName))) {
+ continue;
+ }
+
+ $method = sprintf('stock%s', ucfirst($schema->getLocation()));
+ $this->$method($schema, $paramValue, $options);
+ }
+
+ if (!empty($options['json'])) {
+ if ($key = $operation->getJsonKey()) {
+ $options['json'] = [$key => $options['json']];
+ }
+ if (strpos(json_encode($options['json']), '\/') !== false) {
+ $options['body'] = json_encode($options['json'], JSON_UNESCAPED_SLASHES);
+ $options['headers']['Content-Type'] = 'application/json';
+ unset($options['json']);
+ }
+ }
+
+ return $options;
+ }
+
+ private function stockUrl()
+ {
+ }
+
+ private function stockQuery(Parameter $schema, $paramValue, array &$options)
+ {
+ $options['query'][$schema->getName()] = $paramValue;
+ }
+
+ private function stockHeader(Parameter $schema, $paramValue, array &$options)
+ {
+ $paramName = $schema->getName();
+
+ if (stripos($paramName, 'metadata') !== false) {
+ return $this->stockMetadataHeader($schema, $paramValue, $options);
+ }
+
+ $options['headers'] += is_scalar($paramValue) ? [$schema->getPrefixedName() => $paramValue] : [];
+ }
+
+ private function stockMetadataHeader(Parameter $schema, $paramValue, array &$options)
+ {
+ foreach ($paramValue as $key => $keyVal) {
+ $schema = $schema->getItemSchema() ?: new Parameter(['prefix' => $schema->getPrefix(), 'name' => $key]);
+ $this->stockHeader($schema, $keyVal, $options);
+ }
+ }
+
+ private function stockJson(Parameter $schema, $paramValue, array &$options)
+ {
+ $json = isset($options['json']) ? $options['json'] : [];
+ $options['json'] = $this->jsonSerializer->stockJson($schema, $paramValue, $json);
+ }
+
+ private function stockRaw(Parameter $schema, $paramValue, array &$options)
+ {
+ $options['body'] = $paramValue;
+ }
+}