1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Deserialization\Transformer; |
6
|
|
|
|
7
|
|
|
final class UrlEncodedTransformer implements TransformerInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
private $numericPrefix; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $argSeperator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $numericPrefix |
21
|
|
|
* @param string $argSeperator |
22
|
|
|
*/ |
23
|
4 |
|
public function __construct(string $numericPrefix = '', string $argSeperator = '&') |
24
|
|
|
{ |
25
|
4 |
|
$this->numericPrefix = $numericPrefix; |
26
|
4 |
|
$this->argSeperator = $argSeperator; |
27
|
4 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
1 |
|
public function getContentType(): string |
33
|
|
|
{ |
34
|
1 |
|
return 'application/x-www-form-urlencoded'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $string |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
3 |
|
public function transform(string $string): array |
43
|
|
|
{ |
44
|
3 |
|
$string = str_replace($this->argSeperator, '&', $string); |
45
|
|
|
|
46
|
3 |
|
$rawData = []; |
47
|
3 |
|
parse_str($string, $rawData); |
48
|
|
|
|
49
|
3 |
|
if ('' !== $string && [] === $rawData) { |
50
|
1 |
|
throw TransformerException::create('UrlEncoded not parsable'); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
return $this->cleanRawData($rawData, strlen($this->numericPrefix)); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $rawData |
58
|
|
|
* @param int $numericPrefixLength |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
2 |
|
private function cleanRawData(array $rawData, int $numericPrefixLength): array |
63
|
|
|
{ |
64
|
2 |
|
$data = []; |
65
|
2 |
|
foreach ($rawData as $rawKey => $value) { |
66
|
2 |
|
if (0 !== $numericPrefixLength && 0 === strpos($rawKey, $this->numericPrefix)) { |
67
|
1 |
|
$rawSubKey = substr($rawKey, $numericPrefixLength); |
68
|
1 |
|
if (is_numeric($rawSubKey)) { |
69
|
1 |
|
$rawKey = $rawSubKey; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
$key = is_numeric($rawKey) ? (int) $rawKey : $rawKey; |
74
|
|
|
|
75
|
2 |
|
if (is_array($value)) { |
76
|
2 |
|
$data[$key] = $this->cleanRawData($value, $numericPrefixLength); |
77
|
|
|
} else { |
78
|
2 |
|
if (is_numeric($value)) { |
79
|
1 |
|
if ((string) (int) $value === $value) { |
80
|
1 |
|
$value = (int) $value; |
81
|
|
|
} else { |
82
|
1 |
|
$value = (float) $value; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
$data[$key] = $value; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
return $data; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.