<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package fusionLib
 * @copyright Copyright (c) 2008 - 2016 fusionLib. All rights reserved.
 * @link http://fusionlib.com
 */

/**
 * flEvent Class
 *
 * Base class for holding event data.
 *
 * @package fusionLib
 */
class flEvent {
	/**
	 * The name of the event.
	 *
	 * @var string
	 */
	protected $name = '';

	/**
	 * Parameters to pass with the event.
	 *
	 * @var array
	 */
	protected $params;

	/**
	 * Flags if event propagation stopped.
	 *
	 * @var bool
	 */
	protected $propagationStopped = false;

	/**
	 * Array of parameters to pass along with the event.
	 *
	 * @param string $name The event name.
	 * @param array $params Parameters.
	 */
	function __construct($name, $params = array()) {
		$this->name = $name;
		$this->params = $params;
	}

	/**
	 * Get the name of the event.
	 *
	 * @return string The event name.
	 */
	function getName() {
		return $this->name;
	}

	/**
	 * Stop propagation of this event to other listeners.
	 */
	function stopPropagation() {
		$this->propagationStopped = true;
	}

	/**
	 * True if event propagation has been stopped.
	 *
	 * @return bool true if stopped; else false.
	 */
	function isPropagationStopped() {
		return $this->propagationStopped;
	}

	/**
	 * Get the array of parameters for the event.
	 *
	 * @return array Event parameters.
	 */
	function getParams() {
		return $this->params;
	}

	/**
	 * Get the value of a parameter.
	 *
	 * @param string $name The name of the parameter to get.
	 * @param string $default The default value to return.
	 * @return mixed The parameter value or default if the parameter is not available.
	 */
	function getParam($name, $default = null) {
		return isset($this->params[$name]) ? $this->params[$name] : $default;
	}

	/**
	 * Set a new value for a parameter.
	 *
	 * @param string $name The name of the parameter to set.
	 * @param mixed $value The value to set for the parameter.
	 */
	function setParam($name, $value) {
		$this->params[$name] = $value;
	}

	/**
	 * Trigger this event within the system
	 */
	function trigger() {
		flApp::getEventManager()->notify($this);
	}
}
