<?php
/**
 * Clears the given data.
 *
 * @package Ealab\Deployer
 */

namespace EaLab\WP_Deployer\vendor\Ealab\Deployer\Tracking\ItemValueHandler\ValueProcessor\TransmutationHandler;

/**
 * Clears the given data.
 *
 * It checks the original value type and sets a suitable empty value.
 * It's not the same as the "clear_data_on_import" processor, which
 * is used to delete database entries upon import.
 */
class ClearDataTransmutationHandler extends TransmutationHandlerBase {
	/**
	 * {@inheritDoc}
	 */
	public static function getId(): string {
		return 'clear_data';
	}

	/**
	 * {@inheritDoc}
	 */
	static public function getPublicName(): string {
		return _dpgettext( 'deployer-core', 'Transmutation handler public name', 'Clear data' );
	}

	/**
	 * {@inheritDoc}
	 */
	public function toExport( $value, ?array $options = array() ) {
		if ( is_string( $value ) ) {
			if ( filter_var( $value, FILTER_VALIDATE_INT ) ) {
				return '0';
			}

			if ( filter_var( $value, FILTER_VALIDATE_FLOAT ) ) {
				return '0.0';
			}

			return '';
		}

		if ( is_array( $value ) ) {
			return array();
		}

		if ( is_int( $value ) ) {
			return 0;
		}

		if ( is_float( $value ) ) {
			return 0.0;
		}

		if ( is_bool( $value ) ) {
			return false;
		}

		// Case for objects, resources, and null.
		return null;
	}

	/**
	 * {@inheritdoc}
	 */
	public function toImportFinalize( $value, ?array $options = array() ) {
		// The value is supposed to be already cleared.
		return $value;
	}

	/**
	 * {@inheritdoc}
	 */
	public function toImportPresave( $value, ?array $options = array() ) {
		// The value is supposed to be already cleared.
		return $value;
	}
}
