Automattic\WooCommerce\Database\Migrations

MetaToCustomTableMigrator::validate_data()privateWC 1.0

Validate and transform data so that we catch as many errors as possible before inserting.

Method of the class: MetaToCustomTableMigrator{}

No Hooks.

Return

float|Int|Mixed|String|\WP_Error.

Usage

// private - for code of main (parent) class only
$result = $this->validate_data( $value, $type );
$value(mixed) (required)
Actual data value.
$type(string) (required)
Type of data, could be decimal, int, date, string.

MetaToCustomTableMigrator::validate_data() code WC 8.7.0

private function validate_data( $value, string $type ) {
	switch ( $type ) {
		case 'decimal':
			$value = wc_format_decimal( floatval( $value ), false, true );
			break;
		case 'int':
			$value = (int) $value;
			break;
		case 'bool':
			$value = wc_string_to_bool( $value );
			break;
		case 'date':
			try {
				if ( '' === $value ) {
					$value = null;
				} else {
					$value = ( new \DateTime( $value ) )->format( 'Y-m-d H:i:s' );
				}
			} catch ( \Exception $e ) {
				return new \WP_Error( $e->getMessage() );
			}
			break;
		case 'date_epoch':
			try {
				if ( '' === $value ) {
					$value = null;
				} else {
					$value = ( new \DateTime( "@$value" ) )->format( 'Y-m-d H:i:s' );
				}
			} catch ( \Exception $e ) {
				return new \WP_Error( $e->getMessage() );
			}
			break;
	}

	return $value;
}