forked from FriendsOfSymfony/FOSRest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonToFormDecoder.php
More file actions
59 lines (51 loc) · 1.49 KB
/
JsonToFormDecoder.php
File metadata and controls
59 lines (51 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/*
* This file is part of the FOSRest package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\Rest\Decoder;
use FOS\Rest\Decoder\DecoderInterface;
/**
* Decodes JSON data and make it compliant with application/x-www-form-encoded style
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class JsonToFormDecoder implements DecoderInterface
{
/**
* Makes data decoded from JSON application/x-www-form-encoded compliant
*
* @param array $data
*/
private function xWwwFormEncodedLike(&$data)
{
foreach ($data as $key => &$value) {
if (is_array($value)) {
// Encode recursively
$this->xWwwFormEncodedLike($value);
} elseif (false === $value) {
// Checkbox-like behavior: remove false data
unset($data[$key]);
} elseif (!is_string($value)) {
// Convert everyting to string
// true values will be converted to '1', this is the default checkbox behavior
$value = strval($value);
}
}
}
/**
* {@inheritdoc}
*/
public function decode($data)
{
$decodedData = @json_decode($data, true);
if ($decodedData) {
$this->xWwwFormEncodedLike($decodedData);
}
return $decodedData;
}
}