Automattic\WooCommerce\Internal\Utilities

DatabaseUtil::format_object_value_for_db()publicWC 1.0

Formats an object value of type $type for inclusion in the database.

Метод класса: DatabaseUtil{}

Хуков нет.

Возвращает

Разное.

Использование

$DatabaseUtil = new DatabaseUtil();
$DatabaseUtil->format_object_value_for_db( $value, $type );
$value(разное) (обязательный)
Raw value.
$type(строка) (обязательный)
Data type.

Код DatabaseUtil::format_object_value_for_db() WC 9.1.2

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;
}