From b7a2b4a2362e244310a1063a0cf80db48a76f900 Mon Sep 17 00:00:00 2001 From: Eddie Date: Fri, 30 Aug 2024 15:04:46 +0100 Subject: [PATCH 1/6] Adding float as valid type. --- src/Models/Model.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Models/Model.php b/src/Models/Model.php index 5ddc042..1fb0285 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -328,6 +328,8 @@ public function isPropertyTypeValid( string $key, $value ) : bool { return is_bool( $value ); case 'array': return is_array( $value ); + case 'float': + return is_float( $value ); default: return $value instanceof $type; } From 0247d8a8ec52652905bbbae04e3546997d073b49 Mon Sep 17 00:00:00 2001 From: Eddie Date: Fri, 6 Sep 2024 16:20:28 +0100 Subject: [PATCH 2/6] Adding float test. --- tests/_support/Helper/MockModel.php | 9 +++++---- tests/wpunit/ModelTest.php | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/_support/Helper/MockModel.php b/tests/_support/Helper/MockModel.php index 334706b..37db8e4 100644 --- a/tests/_support/Helper/MockModel.php +++ b/tests/_support/Helper/MockModel.php @@ -6,9 +6,10 @@ class MockModel extends Model { protected $properties = [ - 'id' => 'int', - 'firstName' => [ 'string', 'Michael' ], - 'lastName' => 'string', - 'emails' => [ 'array', [] ], + 'id' => 'int', + 'firstName' => [ 'string', 'Michael' ], + 'lastName' => 'string', + 'emails' => [ 'array', [] ], + 'microseconds' => 'float', ]; } diff --git a/tests/wpunit/ModelTest.php b/tests/wpunit/ModelTest.php index 1adf2e4..e6a5973 100644 --- a/tests/wpunit/ModelTest.php +++ b/tests/wpunit/ModelTest.php @@ -306,6 +306,7 @@ public function invalidTypeProvider() { [ 'id', 'Not an integer' ], [ 'firstName', 100 ], [ 'emails', 'Not an array' ], + [ 'microseconds', 'Not a float' ] ]; } } From 8cf8beba644c09a6b0710c15efd6856968c93258 Mon Sep 17 00:00:00 2001 From: Eddie Date: Fri, 6 Sep 2024 16:44:08 +0100 Subject: [PATCH 3/6] Adding numeric type. --- src/Models/Model.php | 2 ++ tests/_support/Helper/MockModel.php | 1 + tests/wpunit/ModelTest.php | 3 ++- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Models/Model.php b/src/Models/Model.php index 1fb0285..f978d23 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -330,6 +330,8 @@ public function isPropertyTypeValid( string $key, $value ) : bool { return is_array( $value ); case 'float': return is_float( $value ); + case 'numeric': + return is_numeric( $value ); default: return $value instanceof $type; } diff --git a/tests/_support/Helper/MockModel.php b/tests/_support/Helper/MockModel.php index 37db8e4..e65234e 100644 --- a/tests/_support/Helper/MockModel.php +++ b/tests/_support/Helper/MockModel.php @@ -11,5 +11,6 @@ class MockModel extends Model { 'lastName' => 'string', 'emails' => [ 'array', [] ], 'microseconds' => 'float', + 'number' => 'numeric', ]; } diff --git a/tests/wpunit/ModelTest.php b/tests/wpunit/ModelTest.php index e6a5973..ba2ed62 100644 --- a/tests/wpunit/ModelTest.php +++ b/tests/wpunit/ModelTest.php @@ -306,7 +306,8 @@ public function invalidTypeProvider() { [ 'id', 'Not an integer' ], [ 'firstName', 100 ], [ 'emails', 'Not an array' ], - [ 'microseconds', 'Not a float' ] + [ 'microseconds', 'Not a float' ], + [ 'number', 'Not a number' ] ]; } } From db4ed70263c13b69f163cba5c088903d380117db Mon Sep 17 00:00:00 2001 From: Eddie Daniels Date: Fri, 6 Sep 2024 09:18:34 -0800 Subject: [PATCH 4/6] Update src/Models/Model.php Co-authored-by: Jason Adams --- src/Models/Model.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Models/Model.php b/src/Models/Model.php index f978d23..d3ba731 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -330,8 +330,8 @@ public function isPropertyTypeValid( string $key, $value ) : bool { return is_array( $value ); case 'float': return is_float( $value ); - case 'numeric': - return is_numeric( $value ); + case 'number': + return is_int( $value ) || is_float( $value ); default: return $value instanceof $type; } From c945452b7c4f93860958120366f4685325f193d6 Mon Sep 17 00:00:00 2001 From: Eddie Date: Fri, 6 Sep 2024 18:20:57 +0100 Subject: [PATCH 5/6] Adding number type to test (instead of numeric) --- tests/_support/Helper/MockModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/_support/Helper/MockModel.php b/tests/_support/Helper/MockModel.php index e65234e..8508191 100644 --- a/tests/_support/Helper/MockModel.php +++ b/tests/_support/Helper/MockModel.php @@ -11,6 +11,6 @@ class MockModel extends Model { 'lastName' => 'string', 'emails' => [ 'array', [] ], 'microseconds' => 'float', - 'number' => 'numeric', + 'number' => 'number', ]; } From 478549e0a3e0f56d91a697cf92cb423d951f8961 Mon Sep 17 00:00:00 2001 From: Eddie Daniels Date: Mon, 9 Sep 2024 07:26:33 -0800 Subject: [PATCH 6/6] Update tests/wpunit/ModelTest.php Co-authored-by: Jason Adams --- tests/wpunit/ModelTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/wpunit/ModelTest.php b/tests/wpunit/ModelTest.php index ba2ed62..aab6a85 100644 --- a/tests/wpunit/ModelTest.php +++ b/tests/wpunit/ModelTest.php @@ -307,7 +307,7 @@ public function invalidTypeProvider() { [ 'firstName', 100 ], [ 'emails', 'Not an array' ], [ 'microseconds', 'Not a float' ], - [ 'number', 'Not a number' ] + [ 'number', '12' ] // numeric strings do not work; must be int or float ]; } }