Просмотр исходного кода

Fix converting DateTimeInterface to BSON in php 7.4 (#281)

Co-authored-by: al.tarasov <al.tarasov@corp.mail.ru>
Aleksei Tarasov 4 лет назад
Родитель
Сommit
e9f2cb6ab4

+ 2 - 0
lib/Alcaeus/MongoDbAdapter/TypeConverter.php

@@ -43,6 +43,8 @@ class TypeConverter
                 return $value->toBSONType();
             case $value instanceof BSON\Type:
                 return $value;
+            case $value instanceof \DateTimeInterface:
+                return self::fromLegacy((array) $value);
             case is_array($value):
             case is_object($value):
                 $result = [];

+ 8 - 0
tests/Alcaeus/MongoDbAdapter/TypeConverterTest.php

@@ -37,6 +37,14 @@ class TypeConverterTest extends TestCase
             'nestedArrays' => [
                 [['foo' => 'bar']], [new BSONDocument(['foo' => 'bar'])]
             ],
+            'dateTime'            => [
+                \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2021-06-30T12:34:56-7'),
+                new BSONDocument([
+                    'date'          => '2021-06-30 12:34:56.000000',
+                    'timezone_type' => 1,
+                    'timezone'      => '-07:00',
+                ]),
+            ],
         ];
     }