Completed
Push — master ( 11b317...37df4d )
by Lucas
09:27
created

FormDataMapper::mapField()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 9.0051

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 24
cts 25
cp 0.96
rs 4.909
c 0
b 0
f 0
cc 9
eloc 19
nc 8
nop 3
crap 9.0051
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
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

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[] or array<String>.

Loading history...
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