PluginProbe
Media Cloud Sync / 1.4.1
Media Cloud Sync v1.4.1
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.4.1, at includes/sdk/s3/Aws/Api/Serializer/QueryParamBuilder.php

114 lines 3.9 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 // flattened lists can also model a `locationName`
75 } elseif ($name = $shape['locationName'] ?? $this->queryName($items)) {
76 $parts = \explode('.', $prefix);
77 $parts[\count($parts) - 1] = $name;
78 $prefix = \implode('.', $parts);
79 }
80 foreach ($value as $k => $v) {
81 $this->format($items, $v, $prefix . '.' . ($k + 1), $query);
82 }
83 }
84 protected function format_map(MapShape $shape, array $value, $prefix, array &$query)
85 {
86 $vals = $shape->getValue();
87 $keys = $shape->getKey();
88 if (!$this->isFlat($shape)) {
89 $prefix .= '.entry';
90 }
91 $i = 0;
92 $keyName = '%s.%d.' . $this->queryName($keys, 'key');
93 $valueName = '%s.%s.' . $this->queryName($vals, 'value');
94 foreach ($value as $k => $v) {
95 $i++;
96 $this->format($keys, $k, \sprintf($keyName, $prefix, $i), $query);
97 $this->format($vals, $v, \sprintf($valueName, $prefix, $i), $query);
98 }
99 }
100 protected function format_blob(Shape $shape, $value, $prefix, array &$query)
101 {
102 $query[$prefix] = \base64_encode($value);
103 }
104 protected function format_timestamp(TimestampShape $shape, $value, $prefix, array &$query)
105 {
106 $timestampFormat = !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : 'iso8601';
107 $query[$prefix] = TimestampShape::format($value, $timestampFormat);
108 }
109 protected function format_boolean(Shape $shape, $value, $prefix, array &$query)
110 {
111 $query[$prefix] = $value ? 'true' : 'false';
112 }
113 }
114