@@ -30,6 +30,12 @@ public function insertOneAction() : Response {
3030 return new JsonResponse (400 , ErrorNormalizer::normalize ($ th , __METHOD__ ));
3131 }
3232
33+ foreach ($ decodedRequestBody ['document ' ] as &$ insertValue ) {
34+ if ( preg_match (MongoDBHelper::ISO_DATE_TIME_REGEX , $ insertValue ) ) {
35+ $ insertValue = new \MongoDB \BSON \UTCDateTime (new \DateTime ($ insertValue ));
36+ }
37+ }
38+
3339 try {
3440
3541 $ collection = MongoDBHelper::getClient ()->selectCollection (
@@ -58,7 +64,7 @@ public function countAction() : Response {
5864 }
5965
6066 if ( isset ($ decodedRequestBody ['filter ' ]['_id ' ])
61- && is_string ( $ decodedRequestBody ['filter ' ]['_id ' ]) ) {
67+ && preg_match (MongoDBHelper:: MDB_OBJECT_ID_REGEX , $ decodedRequestBody ['filter ' ]['_id ' ]) ) {
6268 $ decodedRequestBody ['filter ' ]['_id ' ] =
6369 new \MongoDB \BSON \ObjectId ($ decodedRequestBody ['filter ' ]['_id ' ]);
6470 }
@@ -91,7 +97,7 @@ public function deleteOneAction() : Response {
9197 }
9298
9399 if ( isset ($ decodedRequestBody ['filter ' ]['_id ' ])
94- && is_string ( $ decodedRequestBody ['filter ' ]['_id ' ]) ) {
100+ && preg_match (MongoDBHelper:: MDB_OBJECT_ID_REGEX , $ decodedRequestBody ['filter ' ]['_id ' ]) ) {
95101 $ decodedRequestBody ['filter ' ]['_id ' ] =
96102 new \MongoDB \BSON \ObjectId ($ decodedRequestBody ['filter ' ]['_id ' ]);
97103 }
@@ -124,7 +130,7 @@ public function findAction() : Response {
124130 }
125131
126132 if ( isset ($ decodedRequestBody ['filter ' ]['_id ' ])
127- && is_string ( $ decodedRequestBody ['filter ' ]['_id ' ]) ) {
133+ && preg_match (MongoDBHelper:: MDB_OBJECT_ID_REGEX , $ decodedRequestBody ['filter ' ]['_id ' ]) ) {
128134 $ decodedRequestBody ['filter ' ]['_id ' ] =
129135 new \MongoDB \BSON \ObjectId ($ decodedRequestBody ['filter ' ]['_id ' ]);
130136 }
@@ -143,6 +149,16 @@ public function findAction() : Response {
143149 return new JsonResponse (500 , ErrorNormalizer::normalize ($ th , __METHOD__ ));
144150 }
145151
152+ foreach ($ documents as $ document ) {
153+ foreach ($ document as &$ documentValue ) {
154+ if ( is_a ($ documentValue , '\MongoDB\BSON\ObjectId ' ) ) {
155+ $ documentValue = (string ) $ documentValue ;
156+ } elseif ( is_a ($ documentValue , '\MongoDB\BSON\UTCDatetime ' ) ) {
157+ $ documentValue = $ documentValue ->toDateTime ()->format ('Y-m-d\TH:i:s.v\Z ' );
158+ }
159+ }
160+ }
161+
146162 return new JsonResponse (200 , $ documents );
147163
148164 }
@@ -159,11 +175,17 @@ public function updateOneAction() : Response {
159175 }
160176
161177 if ( isset ($ decodedRequestBody ['filter ' ]['_id ' ])
162- && is_string ( $ decodedRequestBody ['filter ' ]['_id ' ]) ) {
178+ && preg_match (MongoDBHelper:: MDB_OBJECT_ID_REGEX , $ decodedRequestBody ['filter ' ]['_id ' ]) ) {
163179 $ decodedRequestBody ['filter ' ]['_id ' ] =
164180 new \MongoDB \BSON \ObjectId ($ decodedRequestBody ['filter ' ]['_id ' ]);
165181 }
166182
183+ foreach ($ decodedRequestBody ['update ' ]['$set ' ] as &$ updateValue ) {
184+ if ( preg_match (MongoDBHelper::ISO_DATE_TIME_REGEX , $ updateValue ) ) {
185+ $ updateValue = new \MongoDB \BSON \UTCDateTime (new \DateTime ($ updateValue ));
186+ }
187+ }
188+
167189 try {
168190
169191 $ collection = MongoDBHelper::getClient ()->selectCollection (
0 commit comments