1 | <?php |
||
2 | |||
3 | namespace DrMVC\Helpers; |
||
4 | |||
5 | use MongoDB\BSON\ObjectID; |
||
6 | use MongoDB\BSON\UTCDateTime; |
||
7 | |||
8 | /** |
||
9 | * Class Mongo for work with data from mongodb |
||
10 | * @package DrMVC\Helpers |
||
11 | */ |
||
12 | class Mongo |
||
13 | { |
||
14 | /** |
||
15 | * Check if id have a valid length |
||
16 | * @param string $id |
||
17 | * @return bool |
||
18 | */ |
||
19 | public static function checkId(string $id): bool |
||
20 | { |
||
21 | // If strlen is incorrect, then return error |
||
22 | $len = \strlen($id); |
||
23 | return ($len !== 24 || $len !== 12); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * Cleanup result of query |
||
28 | * @param array|object $array |
||
29 | */ |
||
30 | public static function resultFix(&$array) |
||
31 | { |
||
32 | foreach ($array as &$element) { |
||
33 | if ($element instanceof \stdClass) { |
||
34 | self::resultFix($element); |
||
35 | } else { |
||
36 | if (\is_object($element) && $element instanceof ObjectID) { |
||
37 | $element = (string) $element; |
||
38 | } |
||
39 | if (\is_object($element) && $element instanceof UTCDateTime) { |
||
40 | $element = (new UTCDateTime((string) $element))->toDateTime()->format('Y-m-d H:i:s'); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
41 | } |
||
42 | } |
||
43 | } |
||
44 | } |
||
45 | } |
||
46 |