|
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
|
8 |
|
public function __construct(array $mapping) |
|
26
|
|
|
{ |
|
27
|
8 |
|
$this->mapping = $mapping; |
|
28
|
8 |
|
} |
|
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
|
8 |
|
public function convertToFormData($request, $className) |
|
38
|
|
|
{ |
|
39
|
8 |
|
$document = json_decode($request); |
|
40
|
8 |
|
if (!is_object($document)) { |
|
41
|
|
|
return []; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
8 |
|
if (isset($this->mapping[$className])) { |
|
45
|
8 |
|
foreach ($this->mapping[$className] as $path => $name) { |
|
46
|
3 |
|
$this->mapField($document, $path, $name); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
8 |
|
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
|
3 |
|
private function mapField($item, $path, $name) |
|
61
|
|
|
{ |
|
62
|
3 |
|
if ($path === $name) { |
|
63
|
|
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
if (is_array($item)) { |
|
67
|
3 |
|
if (strpos($path, '0.') === 0) { |
|
68
|
3 |
|
$subField = substr($path, 2); |
|
69
|
|
|
|
|
70
|
3 |
|
array_map( |
|
71
|
3 |
|
function ($subItem) use ($subField, $name) { |
|
72
|
3 |
|
$this->mapField($subItem, $subField, $name); |
|
73
|
3 |
|
}, |
|
74
|
|
|
$item |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
3 |
|
} elseif (is_object($item)) { |
|
78
|
3 |
|
if (($pos = strpos($path, '.')) !== false) { |
|
79
|
3 |
|
$topLevel = substr($path, 0, $pos); |
|
80
|
3 |
|
$subField = substr($path, $pos + 1); |
|
81
|
|
|
|
|
82
|
3 |
|
if (isset($item->$topLevel)) { |
|
83
|
3 |
|
$this->mapField($item->$topLevel, $subField, $name); |
|
84
|
|
|
} |
|
85
|
3 |
|
} elseif (isset($item->$path) || property_exists($item, $path)) { |
|
86
|
3 |
|
$item->$name = $item->$path; |
|
87
|
3 |
|
unset($item->$path); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
3 |
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|