From 1044f1f786665d5fdf5af1a1ae82d2071b2eca4d Mon Sep 17 00:00:00 2001 From: s1uggard Date: Tue, 12 Sep 2023 21:35:42 +0300 Subject: [PATCH] MongoDb storage driver: not rely on default typeMap --- src/Storage/MongoDb.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Storage/MongoDb.php b/src/Storage/MongoDb.php index eb5723c..a8bc528 100644 --- a/src/Storage/MongoDb.php +++ b/src/Storage/MongoDb.php @@ -4,6 +4,8 @@ use MongoDB\Client; use MongoDB\Collection; +use MongoDB\Model\BSONArray; +use MongoDB\Model\BSONDocument; /** * MongoDb storage implementation @@ -12,6 +14,15 @@ class MongoDb implements I { protected Client $client; + /** + * @see https://www.php.net/manual/en/mongodb-driver-cursor.settypemap.php + */ + private const TYPE_MAP = [ + 'array' => BSONArray::class, + 'document' => BSONDocument::class, + 'root' => BSONDocument::class, + ]; + /** * @var array */ @@ -85,7 +96,7 @@ public function sessionUp(string $sessionId, int $otp, int $sessionExpSecs, int ]; $this->collection($this->config['collection_session']) - ->updateOne(['id' => $sessionId], ['$set' => $sessionSet], ['upsert' => true/*, 'session'=> $session*/]); + ->updateOne(['id' => $sessionId], ['$set' => $sessionSet], ['upsert' => true]); $sessionCounterSetOnInsert = [ // New datetime on creation, no changes on update! @@ -93,9 +104,9 @@ public function sessionUp(string $sessionId, int $otp, int $sessionExpSecs, int ]; $this->collection($this->config['collection_session_counter']) - ->updateOne(['id' => $sessionId], ['$setOnInsert' => $sessionCounterSetOnInsert], ['upsert' => true, /*'session'=> $session*/]); + ->updateOne(['id' => $sessionId], ['$setOnInsert' => $sessionCounterSetOnInsert], ['upsert' => true]); $this->collection($this->config['collection_session_counter']) - ->updateOne(['id' => $sessionId], ['$inc' => ['count' => 1]], [/*'session'=> $session*/]); + ->updateOne(['id' => $sessionId], ['$inc' => ['count' => 1]]); // Indexes if ($this->config['indexes']) { @@ -120,7 +131,7 @@ public function sessionDown(string $sessionId): self public function sessionCounter(string $sessionId): int { $sessionCounter = $this->collection($this->config['collection_session_counter']) - ->findOne(['id' => $sessionId], ['projection' => ['count' => 1]]); + ->findOne(['id' => $sessionId], ['projection' => ['count' => 1], 'typeMap' => self::TYPE_MAP]); return ($sessionCounter and !empty($sessionCounter->count)) ? $sessionCounter->count : 0; } @@ -130,7 +141,7 @@ public function sessionCounter(string $sessionId): int public function otp(string $sessionId): int { $session = $this->collection($this->config['collection_session']) - ->findOne(['id' => $sessionId], ['projection' => ['otp' => 1]]); + ->findOne(['id' => $sessionId], ['projection' => ['otp' => 1], 'typeMap' => self::TYPE_MAP]); return ($session and !empty($session->otp)) ? $session->otp : 0; } @@ -150,7 +161,7 @@ public function otpCheckIncrement(string $sessionId): self public function otpCheckCounter(string $sessionId): int { $session = $this->collection($this->config['collection_session']) - ->findOne(['id' => $sessionId], ['projection' => ['otp_check_count' => 1]]); + ->findOne(['id' => $sessionId], ['projection' => ['otp_check_count' => 1], 'typeMap' => self::TYPE_MAP]); return ($session and !empty($session->otp_check_count)) ? $session->otp_check_count : 0; } }