|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Facile\MongoDbBundle\DataCollector; |
|
4
|
|
|
|
|
5
|
|
|
use Facile\MongoDbBundle\Models\Query; |
|
6
|
|
|
use MongoDB\BSON\Serializable; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class MongoQuerySerializer |
|
10
|
|
|
* @internal |
|
11
|
|
|
*/ |
|
12
|
|
|
final class MongoQuerySerializer |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param Query $query |
|
16
|
|
|
*/ |
|
17
|
5 |
|
public static function serialize(Query $query) |
|
18
|
|
|
{ |
|
19
|
5 |
|
$query->setFilters(self::prepareUnserializableData($query->getFilters())); |
|
20
|
5 |
|
$query->setData(self::prepareUnserializableData($query->getData())); |
|
21
|
5 |
|
$query->setOptions(self::prepareUnserializableData($query->getOptions())); |
|
22
|
5 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param array|object $data |
|
26
|
|
|
* |
|
27
|
|
|
* @return array|object |
|
28
|
|
|
*/ |
|
29
|
6 |
|
private static function prepareUnserializableData($data) |
|
30
|
|
|
{ |
|
31
|
6 |
|
if ($data instanceof Serializable) { |
|
|
|
|
|
|
32
|
1 |
|
$data = $data->bsonSerialize(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
6 |
|
$newData = []; |
|
36
|
6 |
|
foreach ($data as $key => $item) { |
|
37
|
6 |
|
$newData[$key] = self::prepareItemData($item); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
6 |
|
return $newData; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param mixed $item |
|
45
|
|
|
* |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
6 |
|
public static function prepareItemData($item) |
|
49
|
|
|
{ |
|
50
|
6 |
|
if (method_exists($item, 'getArrayCopy')) { |
|
51
|
2 |
|
return self::prepareUnserializableData($item->getArrayCopy()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
5 |
|
if (method_exists($item, 'toDateTime')) { |
|
55
|
1 |
|
return 'ISODate("'.$item->toDateTime()->format('c').'")'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
4 |
|
if (method_exists($item, '__toString')) { |
|
59
|
|
|
return $item->__toString(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
4 |
|
if ($item instanceof Serializable) { |
|
|
|
|
|
|
63
|
|
|
return $item->bsonSerialize(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
4 |
|
if (is_array($item) || is_object($item)) { |
|
67
|
2 |
|
return self::prepareUnserializableData((array) $item); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
3 |
|
return $item; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto 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
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.