1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FormDataMapper class file |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\DocumentBundle\Service; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
10
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
11
|
|
|
* @link http://swisscom.ch |
12
|
|
|
*/ |
13
|
|
|
class FormDataMapper implements FormDataMapperInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $mapping; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constructor |
22
|
|
|
* |
23
|
|
|
* @param array $mapping Field mapping |
24
|
|
|
*/ |
25
|
4 |
|
public function __construct(array $mapping) |
26
|
|
|
{ |
27
|
4 |
|
$this->mapping = $mapping; |
28
|
4 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Convert request to form data |
32
|
|
|
* |
33
|
|
|
* @param string $request Request data |
34
|
|
|
* @param string $className Document class |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
4 |
|
public function convertToFormData($request, $className) |
38
|
|
|
{ |
39
|
4 |
|
$document = json_decode($request); |
40
|
4 |
|
if (!is_object($document)) { |
41
|
|
|
return []; |
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
if (isset($this->mapping[$className])) { |
45
|
4 |
|
foreach ($this->mapping[$className] as $path => $name) { |
46
|
4 |
|
$this->mapField($document, $path, $name); |
47
|
2 |
|
} |
48
|
2 |
|
} |
49
|
4 |
|
return json_decode(json_encode($document), true); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Recursive mapper to rename fields for form |
54
|
|
|
* |
55
|
|
|
* @param mixed $item Item to map |
56
|
|
|
* @param string $path Field path |
57
|
|
|
* @param string $name rename field to ... |
58
|
|
|
* @return array |
|
|
|
|
59
|
|
|
*/ |
60
|
4 |
|
private function mapField($item, $path, $name) |
61
|
|
|
{ |
62
|
4 |
|
if ($path === $name) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
if (is_array($item)) { |
67
|
4 |
|
if (strpos($path, '0.') === 0) { |
68
|
4 |
|
$subField = substr($path, 2); |
69
|
|
|
|
70
|
4 |
|
array_map( |
71
|
4 |
|
function ($subItem) use ($subField, $name) { |
72
|
4 |
|
$this->mapField($subItem, $subField, $name); |
73
|
4 |
|
}, |
74
|
|
|
$item |
75
|
2 |
|
); |
76
|
2 |
|
} |
77
|
4 |
|
} elseif (is_object($item)) { |
78
|
4 |
|
if (($pos = strpos($path, '.')) !== false) { |
79
|
4 |
|
$topLevel = substr($path, 0, $pos); |
80
|
4 |
|
$subField = substr($path, $pos + 1); |
81
|
|
|
|
82
|
4 |
|
if (isset($item->$topLevel)) { |
83
|
4 |
|
$this->mapField($item->$topLevel, $subField, $name); |
84
|
2 |
|
} |
85
|
4 |
|
} elseif (isset($item->$path) || property_exists($item, $path)) { |
86
|
4 |
|
$item->$name = $item->$path; |
87
|
4 |
|
unset($item->$path); |
88
|
2 |
|
} |
89
|
2 |
|
} |
90
|
4 |
|
} |
91
|
|
|
} |
92
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.