<?php
/**
 * State data container.
 *
 * Used to store data that can be passed around.
 *
 * @link       https://wp-deployer.io
 * @package    WP_Deployer
 * @subpackage WP_Deployer/includes
 * @author     EaLab
 */

/**
 * State data container.
 */
class Deploy_Sync_Content__State {
	/**
	 * Contains the current values of the state.
	 *
	 * @var array
	 */
	public array $data = array();

	/**
	 * Creates a state instance.
	 *
	 * @param mixed $default_value Either a single value, or an array of keyed values.
	 */
	public function __construct( $default_value = null ) {
		if ( is_array( $default_value ) ) {
			$this->data = $default_value;

			return;
		}

		$this->data = array( 'value' => $default_value );
	}

	/**
	 * Gets a single value from the state.
	 *
	 * @param string $name The name of the value to get.
	 *
	 * @return mixed The value. null if not set.
	 */
	public function __get( string $name ) {
		return $this->data[ $name ] ?? null;
	}

	/**
	 * Checks if a value is set.
	 *
	 * @param string $name The name of the value to check.
	 *
	 * @return bool Whether the value is set.
	 */
	public function __isset( string $name ): bool {
		return isset( $this->data[ $name ] );
	}

	/**
	 * Sets a value in the state.
	 *
	 * @param string $name The name of the value to set.
	 * @param mixed  $value The value to set.
	 */
	public function __set( string $name, $value ): void {
		$this->data[ $name ] = $value;
	}

	/**
	 * Unsets a value in the state.
	 *
	 * @param string $name The name of the value to unset.
	 */
	public function __unset( string $name ): void {
		unset( $this->data[ $name ] );
	}
}