Completed
Push — feature/EVO-11896-empty-object... ( 4869c6 )
by Narcotic
20:12
created

HashType::convertToPhp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * HashType class file
4
 */
5
6
namespace Graviton\DocumentBundle\Types;
7
8
use Doctrine\ODM\MongoDB\Types\Type;
9
use Graviton\DocumentBundle\Entity\ExtReference;
10
use Graviton\DocumentBundle\Entity\Hash;
11
use Graviton\DocumentBundle\Service\ExtReferenceConverterInterface;
12
13
/**
14
 * Hash type
15
 *
16
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license  http://opensource.org/licenses/MIT MIT License
18
 * @link     http://swisscom.ch
19
 */
20
class HashType extends Type
21
{
22
23
    /**
24
     * extref converter
25
     *
26
     * @var ExtReferenceConverterInterface
27
     */
28
    private static $extRefConverter;
29
30
    /**
31
     * sets the converter
32
     *
33
     * @param ExtReferenceConverterInterface $converter converter
34
     *
35
     * @return void
36
     */
37 18
    public function setExtRefConverter(ExtReferenceConverterInterface $converter)
38
    {
39 18
        self::$extRefConverter = $converter;
40 18
    }
41
42
    /**
43
     * Convert DB value to PHP representation
44
     *
45
     * @param mixed $value Value to convert
46
     * @return Hash|null
47
     */
48 8
    public static function convertToPhp($value)
49
    {
50 8
        return is_array($value) ? new Hash(self::processDynamicParts($value)) : null;
51
    }
52
53
    /**
54
     * Convert PHP value to MongoDb representation
55
     *
56
     * @param mixed $value Value to convert
57
     * @return object|null
58
     */
59 8
    public static function convertToDb($value)
60
    {
61 8
        $dbValue = null;
62
63
64 8
        if (is_array($value)) {
65 4
            $dbValue = (object) $value;
66 8
        } elseif ($value instanceof \ArrayObject) {
67 8
            $dbValue = (object) $value->getArrayCopy();
68 4
        } elseif (is_object($value)) {
69
            $dbValue = (object) get_object_vars($value);
70
        }
71
72 8
        var_dump($dbValue);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($dbValue); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
73
74 8
        if (!is_null($dbValue)) {
75 8
            $dbValue = (object) self::processDynamicParts($dbValue);
76
        }
77
78 8
        return $dbValue;
79
    }
80
81
    /**
82
     * loops our structure recursively to
83
     * - find all $ref objects that need to be converted either from that or to that..
84
     * - empty objects that need to be marked accordingly
85
     *
86
     * @param mixed $input input structure
87
     *
88
     * @return array altered structure with replaced $ref objects
89
     */
90 16
    public static function processDynamicParts($input)
91
    {
92 16
        if ($input instanceof \stdClass) {
93 8
            if (!empty(get_object_vars($input))) {
94 8
                $input = self::processDynamicParts(get_object_vars($input));
95
            }
96 8
            return $input;
97
        }
98
99
        // extrefs
100 16
        $externalRefFieldName = '$ref';
101 16
        $internalRefFieldName = 'ref';
102
103
        // empty objects
104 16
        $emptyObjectToPhpValue = '_____EMPTY_PHP_OBJECT_____';
105
106 16
        if (is_array($input)) {
107 16
            foreach ($input as $key => $value) {
108 16
                if ($key === $internalRefFieldName) {
109
                    if (is_array($value) && isset($value['$ref']) && isset($value['$id'])) {
110
                        $extRef = ExtReference::create($value['$ref'], $value['$id']);
111
                        $input[$externalRefFieldName] = self::$extRefConverter->getUrl($extRef);
112
                        unset($input[$internalRefFieldName]);
113
                    }
114 16
                } elseif ($key === $externalRefFieldName) {
115
                    $extRef = self::$extRefConverter->getExtReference($value);
116
                    $input[$internalRefFieldName] = $extRef->jsonSerialize();
117
                    unset($input[$externalRefFieldName]);
118 16
                } elseif ($value === $emptyObjectToPhpValue) {
119
                    $input[$key] = new \stdClass();
120 16
                } elseif (is_object($value) && empty((array)$value)) {
121
                    $input[$key] = $emptyObjectToPhpValue;
122
                } else {
123 16
                    if (is_array($value)) {
124
                        $value = self::processDynamicParts($value);
125
                    }
126 16
                    $input[$key] = $value;
127
                }
128
            }
129
        }
130
131 16
        return $input;
132
    }
133
134
    /**
135
     * Convert to PHP value
136
     *
137
     * @param mixed $value Db value
138
     * @return Hash|null
139
     */
140 2
    public function convertToPHPValue($value)
141
    {
142 2
        return static::convertToPhp($value);
143
    }
144
145
    /**
146
     * Closure to convert to PHP value
147
     *
148
     * @return string
149
     */
150 4
    public function closureToPHP()
151
    {
152 4
        return '$return = \\'.static::class.'::convertToPhp($value);';
153
    }
154
155
    /**
156
     * Convert to DB value
157
     *
158
     * @param mixed $value PHP value
159
     * @return object|null
160
     */
161 2
    public function convertToDatabaseValue($value)
162
    {
163 2
        return static::convertToDb($value);
164
    }
165
166
    /**
167
     * Closure to convert to DB value
168
     *
169
     * @return string
170
     */
171 2
    public function closureToMongo()
172
    {
173 2
        return '$return = \\'.static::class.'::convertToDb($value);';
174
    }
175
}
176