Completed
Pull Request — 6.1-dev (#889)
by
unknown
01:14
created

Converter::convertDocumentToArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchBundle\Mapping;
13
14
use ONGR\ElasticsearchBundle\Result\ObjectIterator;
15
16
/**
17
 * This class converts array to document object.
18
 */
19
class Converter
20
{
21
    private $propertyMetadata = [];
22
23
    public function addClassMetadata(string $class, array $metadata): void
24
    {
25
        $this->propertyMetadata[$class] = $metadata;
26
    }
27
28
    public function convertArrayToDocument(string $namespace, array $raw)
29
    {
30
        if (!isset($this->propertyMetadata[$namespace])) {
31
            echo json_encode($raw, JSON_PRETTY_PRINT);
32
33
            return [];
34
        }
35
36
        return $this->denormalize($raw, $namespace);
37
    }
38
39
    public function convertDocumentToArray($document): array
40
    {
41
        $class = get_class($document);
42
43
        if (!isset($this->propertyMetadata[$class])) {
44
            throw new \Exception("Cannot convert object of class `$class` to array.");
45
        }
46
47
        return $this->normalize($document);
48
    }
49
50
    protected function normalize($document, $metadata = null)
51
    {
52
        $metadata = $metadata ?? $this->propertyMetadata[get_class($document)];
53
        $result = [];
54
55
        foreach ($metadata as $field => $fieldMeta) {
56
            $getter = $fieldMeta['getter'];
57
            $value = $fieldMeta['public'] ? $document->{$fieldMeta['name']} : $document->$getter();
58
59
            if ($fieldMeta['embeded']) {
60
                if (is_iterable($value)) {
61
                    foreach ($value as $item) {
62
                        $result[$field][] = $this->normalize($item, $fieldMeta['sub_properties']);
63
                    }
64
                } else {
65
                    $result[$field] = $this->normalize($item, $fieldMeta['sub_properties']);
0 ignored issues
show
Bug introduced by
The variable $item seems to be defined by a foreach iteration on line 61. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
66
                }
67
            } else {
68
                $result[$field] = $value;
69
            }
70
        }
71
72
        return $result;
73
    }
74
75
    protected function denormalize(array $raw, string $namespace)
76
    {
77
        $metadata = $this->propertyMetadata[$namespace];
78
        $object = new $namespace();
79
80
        foreach ($raw as $field => $value) {
81
            $fieldMeta = $metadata[$field];
82
            $setter = $fieldMeta['setter'];
83
84
            if ($fieldMeta['embeded']) {
85
                $this->addClassMetadata($fieldMeta['class'], $fieldMeta['sub_properties']);
86
                $object->$setter(new ObjectIterator($fieldMeta['class'], $value, $this));
87
            } else {
88
                if ($fieldMeta['public']) {
89
                    $object->{$fieldMeta['name']} = $value;
90
                } else {
91
                    if ($fieldMeta['identifier']) {
92
                        $setter = function ($field, $value) {
93
                            $this->$field = $value;
94
                        };
95
96
                        $setter = \Closure::bind($setter, $object, $object);
97
                        $setter($fieldMeta['name'], $value);
98
                    } else {
99
                        $object->$setter($value);
100
                    }
101
                }
102
            }
103
        }
104
105
        return $object;
106
    }
107
}
108