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
|
6 |
|
public static function serialize(Query $query) |
18
|
|
|
{ |
19
|
6 |
|
$query->setFilters(self::prepareUnserializableData($query->getFilters())); |
20
|
6 |
|
$query->setData(self::prepareUnserializableData($query->getData())); |
21
|
6 |
|
$query->setOptions(self::prepareUnserializableData($query->getOptions())); |
22
|
6 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array|object $data |
26
|
|
|
* |
27
|
|
|
* @return array|object |
28
|
|
|
*/ |
29
|
7 |
|
private static function prepareUnserializableData($data) |
30
|
|
|
{ |
31
|
7 |
|
if ($data instanceof Serializable) { |
|
|
|
|
32
|
1 |
|
$data = $data->bsonSerialize(); |
33
|
|
|
} |
34
|
|
|
|
35
|
7 |
|
$newData = []; |
36
|
7 |
|
foreach ($data as $key => $item) { |
37
|
7 |
|
$newData[$key] = self::prepareItemData($item); |
38
|
|
|
} |
39
|
|
|
|
40
|
7 |
|
return $newData; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param mixed $item |
45
|
|
|
* |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
7 |
|
public static function prepareItemData($item) |
49
|
|
|
{ |
50
|
7 |
|
if (\is_string($item)) { |
51
|
4 |
|
return $item; |
52
|
|
|
} |
53
|
|
|
|
54
|
6 |
|
if (method_exists($item, 'getArrayCopy')) { |
55
|
3 |
|
return self::prepareUnserializableData($item->getArrayCopy()); |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
if (method_exists($item, 'toDateTime')) { |
59
|
1 |
|
return 'ISODate("'.$item->toDateTime()->format('c').'")'; |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
if (method_exists($item, '__toString')) { |
63
|
|
|
return $item->__toString(); |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
if ($item instanceof Serializable) { |
|
|
|
|
67
|
|
|
return $item->bsonSerialize(); |
68
|
|
|
} |
69
|
|
|
|
70
|
3 |
|
if (is_array($item) || is_object($item)) { |
71
|
2 |
|
return self::prepareUnserializableData((array) $item); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
return $item; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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.