Skip to content

Commit ac0bd8d

Browse files
committed
Purpose MongoUtils Helper
1 parent 354e016 commit ac0bd8d

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/Helper/Mongo.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon Framework.
5+
*
6+
* (c) Phalcon Team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.txt
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Incubator\MongoDB\Helper;
15+
16+
use DateTimeInterface;
17+
use MongoDB\BSON\ObjectID;
18+
use MongoDB\BSON\UTCDateTime;
19+
20+
/**
21+
* Class Mongo
22+
*
23+
* @package Phalcon\Incubator\MongoDB\Helper
24+
*/
25+
class Mongo
26+
{
27+
/**
28+
* Check if id parameter is a valid ObjectID
29+
*
30+
* @param mixed $id
31+
* @return bool
32+
* @noinspection MissingParameterTypeDeclarationInspection
33+
*/
34+
final public static function isValidObjectId($id): bool
35+
{
36+
return $id instanceof ObjectID || preg_match('/^[a-f\d]{24}$/i', $id);
37+
}
38+
39+
/**
40+
* Convert a DateTime object to UTCDateTime from MongoDB
41+
*
42+
* @param DateTimeInterface $dateTime
43+
* @return UTCDateTime
44+
*/
45+
final public static function convertDatetime(DateTimeInterface $dateTime): UTCDateTime
46+
{
47+
return new UTCDateTime($dateTime->getTimestamp() * 1000);
48+
}
49+
}

0 commit comments

Comments
 (0)