@@ -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
@@ -1539,9 +1539,7 @@ public function close(): void
15391539 $ activeSessions [] = ['id ' => $ sessionData ['id ' ], 'sessionId ' => $ sessionId ];
15401540 }
15411541
1542- if (!empty ($ activeSessions )) {
1543- $ this ->endSessions ($ activeSessions );
1544- }
1542+ $ this ->endSessions ($ activeSessions );
15451543 } catch (Exception $ e ) {
15461544 // Silently ignore if connection is already lost during cleanup
15471545 if (!str_contains ($ e ->getMessage (), 'Connection to MongoDB has been lost ' )) {
@@ -1550,7 +1548,7 @@ public function close(): void
15501548 }
15511549 }
15521550
1553- if (isset ( $ this -> client ) && $ this ->client ->isConnected ()) {
1551+ if ($ this ->client ->isConnected ()) {
15541552 $ this ->client ->close ();
15551553 }
15561554
@@ -1868,11 +1866,6 @@ private function validateConnection(): void
18681866 throw new Exception ('Client is not connected to MongoDB ' );
18691867 }
18701868
1871- if (!isset ($ this ->client )) {
1872- $ this ->isConnected = false ;
1873- throw new Exception ('MongoDB client is not initialized ' );
1874- }
1875-
18761869 if (!$ this ->client ->isConnected ()) {
18771870 $ this ->isConnected = false ;
18781871 throw new Exception ('Connection to MongoDB has been lost ' );
@@ -1886,7 +1879,7 @@ private function validateConnection(): void
18861879 * @return array Validated write concern
18871880 * @throws Exception If write concern is invalid
18881881 */
1889- public function createWriteConcern ($ writeConcern ): array
1882+ public function createWriteConcern (array | string | int $ writeConcern ): array
18901883 {
18911884 if (is_string ($ writeConcern )) {
18921885 return ['w ' => $ writeConcern ];
@@ -1899,31 +1892,27 @@ public function createWriteConcern($writeConcern): array
18991892 return ['w ' => $ writeConcern ];
19001893 }
19011894
1902- if (is_array ($ writeConcern )) {
1903- $ concern = [];
1895+ $ concern = [];
19041896
1905- if (isset ($ writeConcern ['w ' ])) {
1906- if (is_int ($ writeConcern ['w ' ]) && $ writeConcern ['w ' ] < 0 ) {
1907- throw new Exception ('Write concern w value must be >= 0 ' );
1908- }
1909- $ 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 ' );
19101900 }
1901+ $ concern ['w ' ] = $ writeConcern ['w ' ];
1902+ }
19111903
1912- if (isset ($ writeConcern ['j ' ])) {
1913- $ concern ['j ' ] = (bool )$ writeConcern ['j ' ];
1914- }
1904+ if (isset ($ writeConcern ['j ' ])) {
1905+ $ concern ['j ' ] = (bool )$ writeConcern ['j ' ];
1906+ }
19151907
1916- if (isset ($ writeConcern ['wtimeout ' ])) {
1917- if (!is_int ($ writeConcern ['wtimeout ' ]) || $ writeConcern ['wtimeout ' ] < 0 ) {
1918- throw new Exception ('Write concern wtimeout must be a non-negative integer ' );
1919- }
1920- $ 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 ' );
19211911 }
1922-
1923- return $ concern ;
1912+ $ concern ['wtimeout ' ] = $ writeConcern ['wtimeout ' ];
19241913 }
19251914
1926- throw new Exception ( ' Invalid write concern format ' ) ;
1915+ return $ concern ;
19271916 }
19281917
19291918 /**
@@ -1971,7 +1960,7 @@ private function shouldSkipReadConcern(array $options): bool
19711960 * @return array Validated read concern
19721961 * @throws Exception If read concern is invalid
19731962 */
1974- public function createReadConcern ($ readConcern ): array
1963+ public function createReadConcern (array | string $ readConcern ): array
19751964 {
19761965 if (is_string ($ readConcern )) {
19771966 $ validLevels = [
@@ -1989,33 +1978,29 @@ public function createReadConcern($readConcern): array
19891978 return ['level ' => $ readConcern ];
19901979 }
19911980
1992- if (is_array ($ readConcern )) {
1993- $ concern = [];
1981+ $ concern = [];
19941982
1995- if (isset ($ readConcern ['level ' ])) {
1996- $ validLevels = [
1997- self ::READ_CONCERN_LOCAL ,
1998- self ::READ_CONCERN_AVAILABLE ,
1999- self ::READ_CONCERN_MAJORITY ,
2000- self ::READ_CONCERN_LINEARIZABLE ,
2001- self ::READ_CONCERN_SNAPSHOT
2002- ];
2003-
2004- if (!in_array ($ readConcern ['level ' ], $ validLevels )) {
2005- throw new Exception ('Invalid read concern level: ' . $ readConcern ['level ' ]);
2006- }
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+ ];
20071991
2008- $ concern ['level ' ] = $ readConcern ['level ' ];
1992+ if (!in_array ($ readConcern ['level ' ], $ validLevels )) {
1993+ throw new Exception ('Invalid read concern level: ' . $ readConcern ['level ' ]);
20091994 }
20101995
2011- if (isset ($ readConcern ['afterClusterTime ' ])) {
2012- $ concern ['afterClusterTime ' ] = $ readConcern ['afterClusterTime ' ];
2013- }
1996+ $ concern ['level ' ] = $ readConcern ['level ' ];
1997+ }
20141998
2015- return $ concern ;
1999+ if (isset ($ readConcern ['afterClusterTime ' ])) {
2000+ $ concern ['afterClusterTime ' ] = $ readConcern ['afterClusterTime ' ];
20162001 }
20172002
2018- throw new Exception ( ' Invalid read concern format ' ) ;
2003+ return $ concern ;
20192004 }
20202005
20212006 /**
0 commit comments