<?php

/**
 * HTML/Javascript events serializer
 *
 * Produces HTML/Javascript code for defined events. Querries Listeners what event to generate
 * and outputs serialized action for each event. Returns serialized output for one or all events.
 *
 * @version DR1.4.2
 */
class EventSerializer {



	protected $listeners;



	public function __construct() {
		$this->listeners = new ArrayObject();
	}



	public function addListener(Listener $listener) {
		$this->listeners[] = $listener;
	}



	protected function serialize(Iterator $listeners) {
		$out = '';
		$e = array();
		foreach ($listeners as $l) {
			$jsEvent = $l->getJavascriptEvent();
			$e[$jsEvent][] = $l->getSerializedAction();
		}
		foreach ($e as $event => $a) {
			$js = implode('; ', $a);
			$out .= "$event=\"$js\" ";
		}
		return $out;
	}



	public function getOne($event) {
		$event = strtolower($event);
		$a = new ArrayObject();
		foreach ($this->listeners as $l) {
			if (strtolower($l->getEvent()) == $event)
				$a[] = $l;
		}
		return $this->serialize($a->getIterator());
	}



	public function getAll() {
		return $this->serialize($this->listeners->getIterator());
	}



	public function isDefined($event) {
		$event = strtolower($event);
		foreach ($this->listeners as $l) {
			if (strtolower($l->getEvent()) == $event)
				return true;
		}
		return false;
	}



} // class EventSerializer

?>