FormObjectTransformer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 55
ccs 16
cts 17
cp 0.9412
rs 10
c 1
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A serializeObjects() 0 4 2
A supports() 0 3 2
A transform() 0 13 3
A normalizeObject() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Component\Rest\Transformer\Request;
6
7
use CCT\Component\Rest\Serializer\ContextInterface;
8
9
class FormObjectTransformer extends AbstractSerializerRequestTransformer
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14 4
    public function transform($formData = [], ContextInterface $context = null): array
15
    {
16 4
        if (empty($formData)) {
17
            return [];
18
        }
19
20 4
        if (\is_object($formData)) {
21 1
            return $this->normalizeObject($formData, $context);
22
        }
23
24 3
        array_walk_recursive($formData, [&$this, 'serializeObjects'], $context);
25
26 3
        return $formData;
27
    }
28
29
    /**
30
     * Checks if the formData is supported to execute the transformation.
31
     *
32
     * @param array|object $formData
33
     *
34
     * @return bool
35
     */
36 4
    public function supports($formData): bool
37
    {
38 4
        return \is_object($formData) || \is_array($formData);
39
    }
40
41
    /**
42
     * @param $item
43
     * @param $key
44
     * @param ContextInterface|null $context
45
     */
46 3
    protected function serializeObjects(&$item, $key, ContextInterface $context = null): void
47
    {
48 3
        if (\is_object($item)) {
49 3
            $item = $this->normalizeObject($item, $context);
50
        }
51 3
    }
52
53
    /**
54
     * @param $object
55
     * @param ContextInterface|null $context
56
     *
57
     * @return array
58
     */
59 4
    protected function normalizeObject($object, ContextInterface $context = null): array
60
    {
61 4
        return $this->serializer->toArray(
62 4
            $object,
63 4
            $context ?? $this->context
64
        );
65
    }
66
}
67