1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* a small extension to the Zumba\Util\JsonSerializer to allow deserializing mongodb legacy driver objects |
4
|
|
|
* |
5
|
|
|
* NOTE: this is only needed as we widely use the "mongodb legacy" driver (pecl/mongo) instead of the newer one |
6
|
|
|
* (pecl/mongodb) - if we once switch to the new driver, we should be able to ditch this. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Graviton\ImportExport\Util; |
10
|
|
|
|
11
|
|
|
use Zumba\JsonSerializer\Exception\JsonSerializerException; |
12
|
|
|
use Zumba\JsonSerializer\JsonSerializer as BaseSerializer; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author List of contributors <https://github.com/libgraviton/import-export/graphs/contributors> |
16
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
17
|
|
|
* @link http://swisscom.ch |
18
|
|
|
*/ |
19
|
|
|
class JsonSerializer extends BaseSerializer |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Convert the serialized array into an object |
23
|
|
|
* |
24
|
|
|
* @param mixed $value the value |
25
|
|
|
* @return object |
26
|
|
|
* @throws JsonSerializerException |
27
|
|
|
*/ |
28
|
2 |
|
protected function unserializeObject($value) |
29
|
|
|
{ |
30
|
2 |
|
$className = $value[static::CLASS_IDENTIFIER_KEY]; |
31
|
|
|
|
32
|
2 |
|
$obj = false; |
33
|
2 |
|
if ($className == 'MongoDate') { |
34
|
2 |
|
if (isset($value['usec'])) { |
35
|
2 |
|
$obj = new \MongoDate($value['sec'], $value['usec']); |
36
|
2 |
|
} else { |
37
|
|
|
$obj = new \MongoDate($value['sec']); |
38
|
|
|
} |
39
|
2 |
|
} elseif ($className == 'MongoId') { |
40
|
1 |
|
$thisId = null; |
41
|
1 |
|
if (isset($value['$id'])) { |
42
|
1 |
|
$thisId = $value['$id']; |
43
|
1 |
|
} |
44
|
1 |
|
if (isset($value['objectID']['oid'])) { |
45
|
|
|
$thisId = $value['objectID']['oid']; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
if (is_null($thisId)) { |
49
|
|
|
throw new \LogicException('Could not deserialize MongoID instance, could not find $id!'); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
$obj = new \MongoId($thisId); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
2 |
|
if ($obj !== false) { |
56
|
2 |
|
$this->objectMapping[$this->objectMappingIndex++] = $obj; |
57
|
2 |
|
return $obj; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return parent::unserializeObject($value); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Extract the object data |
65
|
|
|
* |
66
|
|
|
* @param object $value obj |
67
|
|
|
* @param ReflectionClass $ref ref |
68
|
|
|
* @param array $properties props |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
2 |
|
protected function extractObjectData($value, $ref, $properties) |
73
|
|
|
{ |
74
|
2 |
|
$data = array(); |
75
|
2 |
|
foreach ($properties as $property) { |
76
|
|
|
try { |
77
|
2 |
|
if (class_exists('\MongoDB\BSON\ObjectId') && $value instanceof \MongoDB\BSON\ObjectId) { |
|
|
|
|
78
|
|
|
$data['oid'] = $value->__toString(); |
79
|
|
|
} else { |
80
|
2 |
|
$propRef = $ref->getProperty($property); |
81
|
2 |
|
$propRef->setAccessible(true); |
82
|
2 |
|
$data[$property] = $propRef->getValue($value); |
83
|
|
|
} |
84
|
2 |
|
} catch (\ReflectionException $e) { |
85
|
1 |
|
$data[$property] = $value->$property; |
86
|
|
|
} |
87
|
2 |
|
} |
88
|
2 |
|
return $data; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.