@@ -655,7 +655,7 @@ public function insert(string $collection, array $document, array $options = [])
655655 $ docObj ->{$ key } = $ value ;
656656 }
657657
658- if (!isset ($ docObj ->_id ) || $ docObj ->_id === '' || $ docObj -> _id === null ) {
658+ if (!isset ($ docObj ->_id ) || $ docObj ->_id === '' ) {
659659 $ docObj ->_id = $ this ->createUuid ();
660660 }
661661
@@ -729,7 +729,7 @@ public function insertMany(string $collection, array $documents, array $options
729729 $ docObj ->{$ key } = $ value ;
730730 }
731731
732- if (!isset ($ docObj ->_id ) || $ docObj ->_id === '' || $ docObj -> _id === null ) {
732+ if (!isset ($ docObj ->_id ) || $ docObj ->_id === '' ) {
733733 $ docObj ->_id = $ this ->createUuid ();
734734 }
735735
@@ -806,10 +806,10 @@ public function lastDocument(string $collection): array
806806 * - arrayFilters: Array filters for updates
807807 * @param bool $multi Whether to update multiple documents
808808 *
809- * @return Client
809+ * @return int Number of modified documents
810810 * @throws Exception
811811 */
812- public function update (string $ collection , array $ where = [], array $ updates = [], array $ options = [], bool $ multi = false ): self
812+ public function update (string $ collection , array $ where = [], array $ updates = [], array $ options = [], bool $ multi = false ): int
813813 {
814814 // Build command with session and concerns
815815 $ command = [
@@ -843,9 +843,7 @@ public function update(string $collection, array $where = [], array $updates = [
843843 $ otherOptions = array_diff_key ($ options , array_flip (['session ' , 'writeConcern ' , 'readConcern ' , 'upsert ' ]));
844844 $ command = array_merge ($ command , $ otherOptions );
845845
846- $ this ->query ($ command );
847-
848- return $ this ;
846+ return $ this ->query ($ command );
849847 }
850848
851849 /**
@@ -856,10 +854,10 @@ public function update(string $collection, array $where = [], array $updates = [
856854 * @param array $operations Array of operations, each with 'filter' and 'update' keys
857855 * @param array $options
858856 *
859- * @return self
857+ * @return int Number of modified documents
860858 * @throws Exception
861859 */
862- public function upsert (string $ collection , array $ operations , array $ options = []): self
860+ public function upsert (string $ collection , array $ operations , array $ options = []): int
863861 {
864862 $ updates = [];
865863
@@ -874,7 +872,7 @@ public function upsert(string $collection, array $operations, array $options = [
874872 $ updates [] = $ updateOperation ;
875873 }
876874
877- $ this ->query (
875+ return $ this ->query (
878876 array_merge (
879877 [
880878 self ::COMMAND_UPDATE => $ collection ,
@@ -883,7 +881,6 @@ public function upsert(string $collection, array $operations, array $options = [
883881 $ options
884882 )
885883 );
886- return $ this ;
887884 }
888885
889886
@@ -1542,9 +1539,7 @@ public function close(): void
15421539 $ activeSessions [] = ['id ' => $ sessionData ['id ' ], 'sessionId ' => $ sessionId ];
15431540 }
15441541
1545- if (!empty ($ activeSessions )) {
1546- $ this ->endSessions ($ activeSessions );
1547- }
1542+ $ this ->endSessions ($ activeSessions );
15481543 } catch (Exception $ e ) {
15491544 // Silently ignore if connection is already lost during cleanup
15501545 if (!str_contains ($ e ->getMessage (), 'Connection to MongoDB has been lost ' )) {
@@ -1553,7 +1548,7 @@ public function close(): void
15531548 }
15541549 }
15551550
1556- if (isset ( $ this -> client ) && $ this ->client ->isConnected ()) {
1551+ if ($ this ->client ->isConnected ()) {
15571552 $ this ->client ->close ();
15581553 }
15591554
@@ -1871,11 +1866,6 @@ private function validateConnection(): void
18711866 throw new Exception ('Client is not connected to MongoDB ' );
18721867 }
18731868
1874- if (!isset ($ this ->client )) {
1875- $ this ->isConnected = false ;
1876- throw new Exception ('MongoDB client is not initialized ' );
1877- }
1878-
18791869 if (!$ this ->client ->isConnected ()) {
18801870 $ this ->isConnected = false ;
18811871 throw new Exception ('Connection to MongoDB has been lost ' );
@@ -1889,7 +1879,7 @@ private function validateConnection(): void
18891879 * @return array Validated write concern
18901880 * @throws Exception If write concern is invalid
18911881 */
1892- public function createWriteConcern ($ writeConcern ): array
1882+ public function createWriteConcern (array | string | int $ writeConcern ): array
18931883 {
18941884 if (is_string ($ writeConcern )) {
18951885 return ['w ' => $ writeConcern ];
@@ -1902,31 +1892,27 @@ public function createWriteConcern($writeConcern): array
19021892 return ['w ' => $ writeConcern ];
19031893 }
19041894
1905- if (is_array ($ writeConcern )) {
1906- $ concern = [];
1895+ $ concern = [];
19071896
1908- if (isset ($ writeConcern ['w ' ])) {
1909- if (is_int ($ writeConcern ['w ' ]) && $ writeConcern ['w ' ] < 0 ) {
1910- throw new Exception ('Write concern w value must be >= 0 ' );
1911- }
1912- $ concern ['w ' ] = $ writeConcern ['w ' ];
1897+ if (isset ($ writeConcern ['w ' ])) {
1898+ if (is_int ($ writeConcern ['w ' ]) && $ writeConcern ['w ' ] < 0 ) {
1899+ throw new Exception ('Write concern w value must be >= 0 ' );
19131900 }
1901+ $ concern ['w ' ] = $ writeConcern ['w ' ];
1902+ }
19141903
1915- if (isset ($ writeConcern ['j ' ])) {
1916- $ concern ['j ' ] = (bool )$ writeConcern ['j ' ];
1917- }
1904+ if (isset ($ writeConcern ['j ' ])) {
1905+ $ concern ['j ' ] = (bool )$ writeConcern ['j ' ];
1906+ }
19181907
1919- if (isset ($ writeConcern ['wtimeout ' ])) {
1920- if (!is_int ($ writeConcern ['wtimeout ' ]) || $ writeConcern ['wtimeout ' ] < 0 ) {
1921- throw new Exception ('Write concern wtimeout must be a non-negative integer ' );
1922- }
1923- $ concern ['wtimeout ' ] = $ writeConcern ['wtimeout ' ];
1908+ if (isset ($ writeConcern ['wtimeout ' ])) {
1909+ if (!is_int ($ writeConcern ['wtimeout ' ]) || $ writeConcern ['wtimeout ' ] < 0 ) {
1910+ throw new Exception ('Write concern wtimeout must be a non-negative integer ' );
19241911 }
1925-
1926- return $ concern ;
1912+ $ concern ['wtimeout ' ] = $ writeConcern ['wtimeout ' ];
19271913 }
19281914
1929- throw new Exception ( ' Invalid write concern format ' ) ;
1915+ return $ concern ;
19301916 }
19311917
19321918 /**
@@ -1974,7 +1960,7 @@ private function shouldSkipReadConcern(array $options): bool
19741960 * @return array Validated read concern
19751961 * @throws Exception If read concern is invalid
19761962 */
1977- public function createReadConcern ($ readConcern ): array
1963+ public function createReadConcern (array | string $ readConcern ): array
19781964 {
19791965 if (is_string ($ readConcern )) {
19801966 $ validLevels = [
@@ -1992,33 +1978,29 @@ public function createReadConcern($readConcern): array
19921978 return ['level ' => $ readConcern ];
19931979 }
19941980
1995- if (is_array ($ readConcern )) {
1996- $ concern = [];
1981+ $ concern = [];
19971982
1998- if (isset ($ readConcern ['level ' ])) {
1999- $ validLevels = [
2000- self ::READ_CONCERN_LOCAL ,
2001- self ::READ_CONCERN_AVAILABLE ,
2002- self ::READ_CONCERN_MAJORITY ,
2003- self ::READ_CONCERN_LINEARIZABLE ,
2004- self ::READ_CONCERN_SNAPSHOT
2005- ];
2006-
2007- if (!in_array ($ readConcern ['level ' ], $ validLevels )) {
2008- throw new Exception ('Invalid read concern level: ' . $ readConcern ['level ' ]);
2009- }
1983+ if (isset ($ readConcern ['level ' ])) {
1984+ $ validLevels = [
1985+ self ::READ_CONCERN_LOCAL ,
1986+ self ::READ_CONCERN_AVAILABLE ,
1987+ self ::READ_CONCERN_MAJORITY ,
1988+ self ::READ_CONCERN_LINEARIZABLE ,
1989+ self ::READ_CONCERN_SNAPSHOT
1990+ ];
20101991
2011- $ concern ['level ' ] = $ readConcern ['level ' ];
1992+ if (!in_array ($ readConcern ['level ' ], $ validLevels )) {
1993+ throw new Exception ('Invalid read concern level: ' . $ readConcern ['level ' ]);
20121994 }
20131995
2014- if (isset ($ readConcern ['afterClusterTime ' ])) {
2015- $ concern ['afterClusterTime ' ] = $ readConcern ['afterClusterTime ' ];
2016- }
1996+ $ concern ['level ' ] = $ readConcern ['level ' ];
1997+ }
20171998
2018- return $ concern ;
1999+ if (isset ($ readConcern ['afterClusterTime ' ])) {
2000+ $ concern ['afterClusterTime ' ] = $ readConcern ['afterClusterTime ' ];
20192001 }
20202002
2021- throw new Exception ( ' Invalid read concern format ' ) ;
2003+ return $ concern ;
20222004 }
20232005
20242006 /**
0 commit comments