<?php

class CompositeTranslator implements ITranslator {



	protected $translators;



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



	public function add($field, ITranslator $t = null) {
		$this->translators[$field] = $t;
	}



	public function vtom($value) {
		if ($value instanceof IteratorAggregate) {
			$iterator = $value->getIterator();
		} elseif ($value instanceof Iterator) {
			$iterator = $value;
		} else {
			throw new Exception(get_class($this) . ' expects Iterator or IteratorAggregate (e.g. FormContext)');
		}
		$out = new ArrayObject();
		foreach ($iterator as $field => $v) {
			if (array_key_exists($field, $this->translators)) {
				if ($this->translators[$field]) {
					$out[$field] = $this->translators[$field]->vtom($v);
				}
			} else {
				$out[$field] = $v;
			}
		}
		return $out;
	}



	public function mtov($value) {
		if ($value instanceof IteratorAggregate) {
			$iterator = $value->getIterator();
		} elseif ($value instanceof Iterator) {
			$iterator = $value;
		} else {
			throw new Exception(get_class($this) . ' expects Iterator or IteratorAggregate');
		}
		$out = new ArrayObject();
		foreach ($iterator as $field => $value) {
			if (array_key_exists($field, $this->translators)) {
				if ($this->translators[$field]) {
					$out[$field] = $this->translators[$field]->mtov($value);
				}
			} else {
				$out[$field] = $value;
			}
		}
		return $out;
	}



} // class CompositeTranslator

?>