<?php

/**
 * Context object for form
 *
 * This object holds the data sent in HTTP request using
 * Form valueReader.
 *
 * @version DR1.4.2
 */
class FormContext implements IteratorAggregate  {



	private $_formData;



	/**
	 * Constructor
	 */
	public function __construct(ArrayObject $formData) {
			$this->_formData = $formData;
	}



	/**
	 * Form fields accessor
	 *
	 * @return mixed, context data for given field or null when data missing
	 */
	public function __get($field) {
		return isset($this->_formData[$field]) ? $this->_formData[$field] : null;
	}



	/**
	 * Returns raw array of context data
	 *
	 * @return ArrayObject
	 */
	public function getData() {
		return $this->_formData;
	}



	/**
	 * Returns context data array iterator
	 *
	 * @return ArrayIterator
	 */
	public function getIterator() {
		return new ArrayIterator($this->_formData);
	}



	/**
	 * Serializes context data as string
	 *
	 * String format is comma separated list of <code>field: "value"</code> pairs.
	 *
	 * @return string
	 */
	public function __toString() {
		$out = array();
		foreach ($this as $field => $value) {
			$out[] = "$field: \"$value\"";
		}
		return implode(', ', $out);
	}



	/**
	 * Returns JSON string for context data
	 *
	 * @return string, JSON
	 */
	public function valueForwarder() {
		return '{'.$this->__toString().'}';
	}



} // class FormContext

?>