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 https://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
|
4 |
|
public function setExtRefConverter(ExtReferenceConverterInterface $converter) |
38
|
|
|
{ |
39
|
4 |
|
self::$extRefConverter = $converter; |
40
|
4 |
|
} |
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
|
8 |
|
if (is_array($value)) { |
64
|
4 |
|
$dbValue = (object) $value; |
65
|
8 |
|
} elseif ($value instanceof \ArrayObject) { |
66
|
8 |
|
$dbValue = (object) $value->getArrayCopy(); |
67
|
4 |
|
} elseif (is_object($value)) { |
68
|
|
|
$dbValue = (object) get_object_vars($value); |
69
|
|
|
} |
70
|
|
|
|
71
|
8 |
|
if (!is_null($dbValue)) { |
72
|
8 |
|
$dbValue = (object) self::processDynamicParts($dbValue); |
73
|
|
|
} |
74
|
|
|
|
75
|
8 |
|
return $dbValue; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* loops our structure recursively to |
80
|
|
|
* - find all $ref objects that need to be converted either from that or to that.. |
81
|
|
|
* - empty objects that need to be marked accordingly |
82
|
|
|
* |
83
|
|
|
* @param mixed $input input structure |
84
|
|
|
* |
85
|
|
|
* @return array altered structure with replaced $ref objects |
86
|
|
|
*/ |
87
|
16 |
|
public static function processDynamicParts($input) |
88
|
|
|
{ |
89
|
16 |
|
if ($input instanceof \stdClass) { |
90
|
8 |
|
if (!empty(get_object_vars($input))) { |
91
|
8 |
|
$input = self::processDynamicParts(get_object_vars($input)); |
92
|
|
|
} |
93
|
8 |
|
return $input; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// extrefs |
97
|
16 |
|
$externalRefFieldName = '$ref'; |
98
|
16 |
|
$internalRefFieldName = 'ref'; |
99
|
|
|
|
100
|
|
|
// empty objects |
101
|
16 |
|
$emptyObjectToPhpValue = '_____EMPTY_PHP_OBJECT_____'; |
102
|
|
|
|
103
|
16 |
|
if (is_array($input)) { |
104
|
16 |
|
foreach ($input as $key => $value) { |
105
|
16 |
|
if ($key === $internalRefFieldName) { |
106
|
|
|
if (is_array($value) && isset($value['$ref']) && isset($value['$id'])) { |
107
|
|
|
$extRef = ExtReference::create($value['$ref'], $value['$id']); |
108
|
|
|
$input[$externalRefFieldName] = self::$extRefConverter->getUrl($extRef); |
109
|
|
|
unset($input[$internalRefFieldName]); |
110
|
|
|
} |
111
|
16 |
|
} elseif ($key === $externalRefFieldName) { |
112
|
|
|
$extRef = self::$extRefConverter->getExtReference($value); |
113
|
|
|
$input[$internalRefFieldName] = $extRef->jsonSerialize(); |
114
|
|
|
unset($input[$externalRefFieldName]); |
115
|
16 |
|
} elseif ($value === $emptyObjectToPhpValue) { |
116
|
|
|
$input[$key] = new \stdClass(); |
117
|
16 |
|
} elseif (is_object($value) && empty((array) $value)) { |
118
|
|
|
$input[$key] = $emptyObjectToPhpValue; |
119
|
|
|
} else { |
120
|
16 |
|
if (is_array($value) || is_object($value)) { |
121
|
|
|
$value = self::processDynamicParts($value); |
122
|
|
|
} |
123
|
16 |
|
$input[$key] = $value; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
16 |
|
return $input; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Convert to PHP value |
133
|
|
|
* |
134
|
|
|
* @param mixed $value Db value |
135
|
|
|
* @return Hash|null |
136
|
|
|
*/ |
137
|
2 |
|
public function convertToPHPValue($value) |
138
|
|
|
{ |
139
|
2 |
|
return static::convertToPhp($value); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Closure to convert to PHP value |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
4 |
|
public function closureToPHP() |
148
|
|
|
{ |
149
|
4 |
|
return '$return = \\'.static::class.'::convertToPhp($value);'; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Convert to DB value |
154
|
|
|
* |
155
|
|
|
* @param mixed $value PHP value |
156
|
|
|
* @return object|null |
157
|
|
|
*/ |
158
|
2 |
|
public function convertToDatabaseValue($value) |
159
|
|
|
{ |
160
|
2 |
|
return static::convertToDb($value); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Closure to convert to DB value |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
2 |
|
public function closureToMongo() |
169
|
|
|
{ |
170
|
2 |
|
return '$return = \\'.static::class.'::convertToDb($value);'; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|