Passed
Branch master (3c33c7)
by Andrey
04:31
created

QueryParamBuilder::isFlat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
namespace Aws\Api\Serializer;
3
4
use Aws\Api\StructureShape;
5
use Aws\Api\ListShape;
6
use Aws\Api\MapShape;
7
use Aws\Api\Shape;
8
use Aws\Api\TimestampShape;
9
10
/**
11
 * @internal
12
 */
13
class QueryParamBuilder
14
{
15
    private $methods;
16
17
    protected function queryName(Shape $shape, $default = null)
18
    {
19
        if (null !== $shape['queryName']) {
20
            return $shape['queryName'];
21
        }
22
23
        if (null !== $shape['locationName']) {
24
            return $shape['locationName'];
25
        }
26
27
        if ($this->isFlat($shape) && !empty($shape['member']['locationName'])) {
28
            return $shape['member']['locationName'];
29
        }
30
31
        return $default;
32
    }
33
34
    protected function isFlat(Shape $shape)
35
    {
36
        return $shape['flattened'] === true;
37
    }
38
39
    public function __invoke(StructureShape $shape, array $params)
40
    {
41
        if (!$this->methods) {
42
            $this->methods = array_fill_keys(get_class_methods($this), true);
43
        }
44
45
        $query = [];
46
        $this->format_structure($shape, $params, '', $query);
47
48
        return $query;
49
    }
50
51
    protected function format(Shape $shape, $value, $prefix, array &$query)
52
    {
53
        $type = 'format_' . $shape['type'];
54
        if (isset($this->methods[$type])) {
55
            $this->{$type}($shape, $value, $prefix, $query);
56
        } else {
57
            $query[$prefix] = (string) $value;
58
        }
59
    }
60
61
    protected function format_structure(
62
        StructureShape $shape,
63
        array $value,
64
        $prefix,
65
        &$query
66
    ) {
67
        if ($prefix) {
68
            $prefix .= '.';
69
        }
70
71
        foreach ($value as $k => $v) {
72
            if ($shape->hasMember($k)) {
73
                $member = $shape->getMember($k);
74
                $this->format(
75
                    $member,
76
                    $v,
77
                    $prefix . $this->queryName($member, $k),
78
                    $query
79
                );
80
            }
81
        }
82
    }
83
84
    protected function format_list(
85
        ListShape $shape,
86
        array $value,
87
        $prefix,
88
        &$query
89
    ) {
90
        // Handle empty list serialization
91
        if (!$value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
92
            $query[$prefix] = '';
93
            return;
94
        }
95
96
        $items = $shape->getMember();
97
98
        if (!$this->isFlat($shape)) {
99
            $locationName = $shape->getMember()['locationName'] ?: 'member';
100
            $prefix .= ".$locationName";
101
        } elseif ($name = $this->queryName($items)) {
102
            $parts = explode('.', $prefix);
103
            $parts[count($parts) - 1] = $name;
104
            $prefix = implode('.', $parts);
105
        }
106
107
        foreach ($value as $k => $v) {
108
            $this->format($items, $v, $prefix . '.' . ($k + 1), $query);
109
        }
110
    }
111
112
    protected function format_map(
113
        MapShape $shape,
114
        array $value,
115
        $prefix,
116
        array &$query
117
    ) {
118
        $vals = $shape->getValue();
119
        $keys = $shape->getKey();
120
121
        if (!$this->isFlat($shape)) {
122
            $prefix .= '.entry';
123
        }
124
125
        $i = 0;
126
        $keyName = '%s.%d.' . $this->queryName($keys, 'key');
127
        $valueName = '%s.%s.' . $this->queryName($vals, 'value');
128
129
        foreach ($value as $k => $v) {
130
            $i++;
131
            $this->format($keys, $k, sprintf($keyName, $prefix, $i), $query);
132
            $this->format($vals, $v, sprintf($valueName, $prefix, $i), $query);
133
        }
134
    }
135
136
    protected function format_blob(Shape $shape, $value, $prefix, array &$query)
137
    {
138
        $query[$prefix] = base64_encode($value);
139
    }
140
141
    protected function format_timestamp(
142
        TimestampShape $shape,
143
        $value,
144
        $prefix,
145
        array &$query
146
    ) {
147
        $query[$prefix] = TimestampShape::format($value, 'iso8601');
148
    }
149
150
    protected function format_boolean(Shape $shape, $value, $prefix, array &$query)
151
    {
152
        $query[$prefix] = ($value) ? 'true' : 'false';
153
    }
154
}
155