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.

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

Хуков нет.

Возвращает

float|int|Разное|Строку|\WP_Error.

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

// private - только в коде основоного (родительского) класса
$result = $this->validate_data( $value, $type );
$value(разное) (обязательный)
Actual data value.
$type(строка) (обязательный)
Type of data, could be decimal, int, date, string.

Код MetaToCustomTableMigrator::validate_data() 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;
}