@@ -759,7 +759,7 @@ public function save($row): bool
759759
760760 /**
761761 * This method is called on save to determine if entry have to be updated.
762- * If this method returns false insert operation will be executed
762+ * If this method returns ` false` insert operation will be executed.
763763 *
764764 * @param object|row_array $row
765765 */
@@ -966,11 +966,7 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
966966 */
967967 public function update ($ id = null , $ row = null ): bool
968968 {
969- if (is_bool ($ id )) {
970- throw new InvalidArgumentException ('update(): argument #1 ($id) should not be boolean. ' );
971- }
972-
973- if (is_numeric ($ id ) || is_string ($ id )) {
969+ if ($ this ->hasRationalPrimaryKey ($ id )) {
974970 $ id = [$ id ];
975971 }
976972
@@ -1097,11 +1093,7 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
10971093 */
10981094 public function delete ($ id = null , bool $ purge = false )
10991095 {
1100- if (is_bool ($ id )) {
1101- throw new InvalidArgumentException ('delete(): argument #1 ($id) should not be boolean. ' );
1102- }
1103-
1104- if (! in_array ($ id , [null , 0 , '0 ' ], true ) && (is_numeric ($ id ) || is_string ($ id ))) {
1096+ if ($ this ->hasRationalPrimaryKey ($ id )) {
11051097 $ id = [$ id ];
11061098 }
11071099
@@ -1896,4 +1888,24 @@ protected function convertToReturnType(array $row, string $returnType): array|ob
18961888
18971889 return $ this ->converter ->reconstruct ($ returnType , $ row );
18981890 }
1891+
1892+ /**
1893+ * Checking that the ID has a rational value.
1894+ *
1895+ * Standard type checks in PHP do not fully verify the validity of ID as a primary key.
1896+ * Especially the values 0, '0', '' are rarely used or are invalid.
1897+ * Example, if you add an entry with ID=0, it will be an error for `$this->insertID`.
1898+ *
1899+ * @param array<int|string, int|string>|float|int|string|null $id
1900+ *
1901+ * @throws InvalidArgumentException
1902+ */
1903+ protected function hasRationalPrimaryKey ($ id ): bool
1904+ {
1905+ if (is_bool ($ id )) {
1906+ throw new InvalidArgumentException ('The ID value should not be boolean. ' );
1907+ }
1908+
1909+ return ! in_array ($ id , [0 , '0 ' , '' , 0.0 ], true ) && (is_numeric ($ id ) || is_string ($ id ));
1910+ }
18991911}
0 commit comments