Automattic\WooCommerce\Internal\Utilities
DatabaseUtil::format_object_value_for_db
Formats an object value of type $type for inclusion in the database.
Method of the class: DatabaseUtil{}
No Hooks.
Returns
Mixed.
Usage
$DatabaseUtil = new DatabaseUtil(); $DatabaseUtil->format_object_value_for_db( $value, $type );
- $value(mixed) (required)
- Raw value.
- $type(string) (required)
- Data type.
DatabaseUtil::format_object_value_for_db() DatabaseUtil::format object value for db code WC 10.7.0
public function format_object_value_for_db( $value, string $type ) {
switch ( $type ) {
case 'decimal':
$value = wc_format_decimal( $value, false, true );
break;
case 'int':
$value = (int) $value;
break;
case 'bool':
$value = wc_string_to_bool( $value );
break;
case 'string':
$value = strval( $value );
break;
case 'date':
// Date properties are converted to the WP timezone (see WC_Data::set_date_prop() method), however
// for our own tables we persist dates in GMT.
$value = $value ? ( new DateTime( $value ) )->setTimezone( new DateTimeZone( '+00:00' ) )->format( 'Y-m-d H:i:s' ) : null;
break;
case 'date_epoch':
$value = $value ? ( new DateTime( "@{$value}" ) )->format( 'Y-m-d H:i:s' ) : null;
break;
default:
throw new \Exception( esc_html( 'Invalid type received: ' . $type ) );
}
return $value;
}