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
|
|
|
use MongoDB\BSON\ObjectID; |
24
|
|
|
use MongoDB\BSON\UTCDateTime; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Can be used by any class which accesses MongoDB. |
28
|
|
|
*/ |
29
|
|
|
trait MongoHelper |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Transforms a literal into a MongoDB ObjectId. |
33
|
|
|
* |
34
|
|
|
* @param $id - If it's an `ObjectID`, returns that, otherwise creates a new |
35
|
|
|
* `ObjectID`. |
36
|
|
|
* @return - The ObjectID |
|
|
|
|
37
|
|
|
*/ |
38
|
1 |
|
protected function toId($id): ObjectID |
39
|
|
|
{ |
40
|
1 |
|
return $id instanceof ObjectID ? $id : new ObjectID((string) $id); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Transforms literals into MongoDB ObjectIds. |
45
|
|
|
* |
46
|
|
|
* @param iterable<mixed> $ids Goes through each entry, converts to `ObjectID` |
|
|
|
|
47
|
|
|
* @return array<\MongoDB\BSON\ObjectID> The ObjectIDs |
48
|
|
|
*/ |
49
|
|
|
protected function toIds(iterable $ids): array |
50
|
|
|
{ |
51
|
|
|
$mids = []; |
52
|
|
|
foreach ($ids as $a) { |
53
|
|
|
$mids[] = $a instanceof ObjectID ? $a : new ObjectID((string) $a); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
return $mids; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Gets the current time. |
60
|
|
|
* |
61
|
|
|
* @return - The current time |
|
|
|
|
62
|
|
|
* @deprecated 0.7.0:1.0.0 UTCDateTime now doesn't need an argument. |
63
|
|
|
*/ |
64
|
|
|
protected function now(): \MongoDB\BSON\UTCDateTime |
65
|
|
|
{ |
66
|
|
|
$parts = explode(' ', microtime()); |
67
|
|
|
return new \MongoDB\BSON\UTCDateTime(sprintf('%d%03d', $parts[1], $parts[0] * 1000)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Tries to parse a date. |
72
|
|
|
* |
73
|
|
|
* @param $date - The possible string date value, a string, a |
74
|
|
|
* `\DateTimeInterface`, or a `\MongoDB\BSON\UTCDateTime` |
75
|
|
|
* @return - The MongoDB datetime or null |
|
|
|
|
76
|
|
|
*/ |
77
|
|
|
protected function toDate($date): ?UTCDateTime |
78
|
|
|
{ |
79
|
|
|
if ($date instanceof UTCDateTime) { |
|
|
|
|
80
|
|
|
return $date; |
81
|
|
|
} elseif ($date instanceof \DateTimeInterface) { |
82
|
|
|
return new UTCDateTime($date); |
83
|
|
|
} else { |
84
|
|
|
$date = trim((string)$date); |
85
|
|
|
return strlen($date) > 0 ? new UTCDateTime(strtotime($date) * 1000) : null; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Makes sure a document isn't null. |
91
|
|
|
* |
92
|
|
|
* @param mixed $id The document identifier, either a `\MongoDB\BSON\ObjectID` or string |
93
|
|
|
* @param mixed $document The document to check |
94
|
|
|
* @return mixed Returns `$document` |
95
|
|
|
* @throws \Caridea\Dao\Exception\Unretrievable if the document is null |
96
|
|
|
*/ |
97
|
|
|
protected function ensure($id, $document) |
98
|
|
|
{ |
99
|
|
|
if ($document === null) { |
100
|
|
|
throw new \Caridea\Dao\Exception\Unretrievable("Could not find document with ID $id"); |
101
|
|
|
} |
102
|
|
|
return $document; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.