1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Minotaur |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2017 Appertly |
19
|
|
|
* @license Apache-2.0 |
20
|
|
|
*/ |
21
|
|
|
namespace Minotaur\Db; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Can be used by any class which accesses MongoDB. |
25
|
|
|
*/ |
26
|
|
|
trait MongoDateConversion |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Tries to convert a `UTCDateTime` into a `DateTime`. |
30
|
|
|
* |
31
|
|
|
* @param mixed $value The value to convert |
32
|
|
|
* @param \DateTimeZone $timeZone Optional. Time Zone to set. |
33
|
|
|
* @return \DateTime|null The date if `$value` is a `UTCDateTime`, `null` otherwise |
34
|
|
|
*/ |
35
|
|
|
protected function toDateTime($value, \DateTimeZone $timeZone = null): ?\DateTime |
36
|
|
|
{ |
37
|
|
|
if ($value instanceof \MongoDB\BSON\UTCDateTime) { |
|
|
|
|
38
|
|
|
$date = $value->toDateTime(); |
39
|
|
|
if ($timeZone !== null) { |
40
|
|
|
$date->setTimezone($timeZone); |
41
|
|
|
} |
42
|
|
|
return $date; |
43
|
|
|
} |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Tries to convert a `UTCDateTime` into a `DateTimeImmutable`. |
49
|
|
|
* |
50
|
|
|
* @param mixed $value The value to convert |
51
|
|
|
* @return \DateTimeImmutable|null The date if `$value` is a `UTCDateTime`, `null` otherwise |
52
|
|
|
*/ |
53
|
|
|
protected function toDateTimeImmutable($value): ?\DateTimeImmutable |
54
|
|
|
{ |
55
|
|
|
return $value instanceof \MongoDB\BSON\UTCDateTime ? |
|
|
|
|
56
|
|
|
new \DateTimeImmutable('@' . $value->toDateTime()->getTimestamp()) : null; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
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.