PluginProbe
Media Cloud Sync / 1.2.11
Media Cloud Sync v1.2.11
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / Aws / Api / Serializer / QueryParamBuilder.php

QueryParamBuilder.php in Media Cloud Sync 1.2.11, at includes/sdk/s3/Aws/Api/Serializer/QueryParamBuilder.php

113 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\Aws\Api\Serializer;
4
5 use Dudlewebs\WPMCS\s3\Aws\Api\StructureShape;
6 use Dudlewebs\WPMCS\s3\Aws\Api\ListShape;
7 use Dudlewebs\WPMCS\s3\Aws\Api\MapShape;
8 use Dudlewebs\WPMCS\s3\Aws\Api\Shape;
9 use Dudlewebs\WPMCS\s3\Aws\Api\TimestampShape;
10 /**
11 * @internal
12 */
13 class QueryParamBuilder
14 {
15 private $methods;
16 protected function queryName(Shape $shape, $default = null)
17 {
18 if (null !== $shape['queryName']) {
19 return $shape['queryName'];
20 }
21 if (null !== $shape['locationName']) {
22 return $shape['locationName'];
23 }
24 if ($this->isFlat($shape) && !empty($shape['member']['locationName'])) {
25 return $shape['member']['locationName'];
26 }
27 return $default;
28 }
29 protected function isFlat(Shape $shape)
30 {
31 return $shape['flattened'] === \true;
32 }
33 public function __invoke(StructureShape $shape, array $params)
34 {
35 if (!$this->methods) {
36 $this->methods = \array_fill_keys(\get_class_methods($this), \true);
37 }
38 $query = [];
39 $this->format_structure($shape, $params, '', $query);
40 return $query;
41 }
42 protected function format(Shape $shape, $value, $prefix, array &$query)
43 {
44 $type = 'format_' . $shape['type'];
45 if (isset($this->methods[$type])) {
46 $this->{$type}($shape, $value, $prefix, $query);
47 } else {
48 $query[$prefix] = (string) $value;
49 }
50 }
51 protected function format_structure(StructureShape $shape, array $value, $prefix, &$query)
52 {
53 if ($prefix) {
54 $prefix .= '.';
55 }
56 foreach ($value as $k => $v) {
57 if ($shape->hasMember($k)) {
58 $member = $shape->getMember($k);
59 $this->format($member, $v, $prefix . $this->queryName($member, $k), $query);
60 }
61 }
62 }
63 protected function format_list(ListShape $shape, array $value, $prefix, &$query)
64 {
65 // Handle empty list serialization
66 if (!$value) {
67 $query[$prefix] = '';
68 return;
69 }
70 $items = $shape->getMember();
71 if (!$this->isFlat($shape)) {
72 $locationName = $shape->getMember()['locationName'] ?: 'member';
73 $prefix .= ".{$locationName}";
74 } elseif ($name = $this->queryName($items)) {
75 $parts = \explode('.', $prefix);
76 $parts[\count($parts) - 1] = $name;
77 $prefix = \implode('.', $parts);
78 }
79 foreach ($value as $k => $v) {
80 $this->format($items, $v, $prefix . '.' . ($k + 1), $query);
81 }
82 }
83 protected function format_map(MapShape $shape, array $value, $prefix, array &$query)
84 {
85 $vals = $shape->getValue();
86 $keys = $shape->getKey();
87 if (!$this->isFlat($shape)) {
88 $prefix .= '.entry';
89 }
90 $i = 0;
91 $keyName = '%s.%d.' . $this->queryName($keys, 'key');
92 $valueName = '%s.%s.' . $this->queryName($vals, 'value');
93 foreach ($value as $k => $v) {
94 $i++;
95 $this->format($keys, $k, \sprintf($keyName, $prefix, $i), $query);
96 $this->format($vals, $v, \sprintf($valueName, $prefix, $i), $query);
97 }
98 }
99 protected function format_blob(Shape $shape, $value, $prefix, array &$query)
100 {
101 $query[$prefix] = \base64_encode($value);
102 }
103 protected function format_timestamp(TimestampShape $shape, $value, $prefix, array &$query)
104 {
105 $timestampFormat = !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : 'iso8601';
106 $query[$prefix] = TimestampShape::format($value, $timestampFormat);
107 }
108 protected function format_boolean(Shape $shape, $value, $prefix, array &$query)
109 {
110 $query[$prefix] = $value ? 'true' : 'false';
111 }
112 }
113