Skip to content

Commit e4d576e

Browse files
committed
Split casting on its own method
1 parent f223e05 commit e4d576e

File tree

1 file changed

+48
-37
lines changed

1 file changed

+48
-37
lines changed

src/Schema/Traits/Custom_Table_Query_Methods.php

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -773,45 +773,56 @@ private static function amend_value_types( array $data ): array {
773773

774774
$column_php_type = $column_object->get_php_type();
775775

776-
switch ( $column_php_type ) {
777-
case Column::PHP_TYPE_INT:
778-
$data[ $column ] = (int) $value;
779-
break;
780-
case Column::PHP_TYPE_STRING:
781-
$data[ $column ] = (string) $value;
782-
break;
783-
case Column::PHP_TYPE_FLOAT:
784-
$data[ $column ] = (float) $value;
785-
break;
786-
case Column::PHP_TYPE_BOOL:
787-
$data[ $column ] = (bool) $value;
788-
break;
789-
case Column::PHP_TYPE_JSON:
790-
$data[ $column ] = (array) json_decode( $value, true );
791-
break;
792-
case Column::PHP_TYPE_DATETIME:
793-
try {
794-
$instance = Config::get_container()->get( DateTimeInterface::class );
795-
} catch ( Exception $e ) {
796-
$instance = DateTime::class;
797-
}
798-
799-
$data[ $column ] = $instance::createFromFormat( 'Y-m-d H:i:s', $value );
800-
801-
if ( ! $data[ $column ] instanceof DateTimeInterface ) {
802-
$data[ $column ] = $instance::createFromFormat( 'Y-m-d', $value );
803-
}
804-
805-
if ( ! $data[ $column ] instanceof DateTimeInterface ) {
806-
throw new InvalidArgumentException( "Invalid datetime value format: {$value}." );
807-
}
808-
809-
break;
810-
default:
811-
throw new InvalidArgumentException( "Unsupported column type: {$column_php_type}." );
812-
}
776+
$data[ $column ] = static::cast_value_based_on_type( $column_php_type, $value );
813777
}
814778

815779
return $data;
816780
}
781+
782+
/**
783+
* Casts a value based on the type.
784+
*
785+
* @since TBD
786+
*
787+
* @param string $type The type to cast the value to.
788+
* @param mixed $value The value to cast.
789+
*
790+
* @return mixed The cast value.
791+
*/
792+
public static function cast_value_based_on_type( string $type, $value ) {
793+
switch ( $type ) {
794+
case Column::PHP_TYPE_INT:
795+
return (int) $value;
796+
case Column::PHP_TYPE_STRING:
797+
return (string) $value;
798+
case Column::PHP_TYPE_FLOAT:
799+
return (float) $value;
800+
case Column::PHP_TYPE_BOOL:
801+
return (bool) $value;
802+
case Column::PHP_TYPE_JSON:
803+
return (array) json_decode( $value, true );
804+
case Column::PHP_TYPE_DATETIME:
805+
try {
806+
$instance = Config::get_container()->get( DateTimeInterface::class );
807+
} catch ( Exception $e ) {
808+
$instance = DateTime::class;
809+
}
810+
811+
$value = $instance::createFromFormat( 'Y-m-d H:i:s', $value );
812+
813+
if ( $value instanceof DateTimeInterface ) {
814+
return $value;
815+
}
816+
817+
$value = $instance::createFromFormat( 'Y-m-d', $value );
818+
819+
if ( ! $value instanceof DateTimeInterface ) {
820+
throw new InvalidArgumentException( "Invalid datetime value format: {$value}." );
821+
}
822+
823+
return $value;
824+
default:
825+
throw new InvalidArgumentException( "Unsupported column type: {$type}." );
826+
}
827+
}
817828
}

0 commit comments

Comments
 (0)