<?php

/**
 * Composite validator
 * 
 * This is a special validator whose role is to aggregate 
 * multiple validators into a single one.
 * 
 * It validates each user input and is valid only if each
 * and every input is valid.
 * 
 * @version DR1.6
 */
class CompositeValidator implements IValidator {



	protected $validators;



	/**
	 * Constructs a CompositeValidator instance
	 */
	public function __construct() {
		$this->validators = new ArrayObject();
	}



	/**
	 * Adds a Validator to this CompositeValidator
	 * 
	 * @param Validator to be added
	 */
	public function add(IValidator $validator) {
		$this->validators[] = $validator;
	}



	/**
	 * Validates user input of every validator it contains.
	 * 
	 * @return true if all validation rules are valid or no rule found
	 */
	public function valid() {
		if (!$this->validators->count()) {
			return true;
		}
		$valid = true;
		// walk through validators, do not break, so all all error messages are shown at once
		foreach ($this->validators as $validator) {
			if (!$validator->valid()) {
				$valid = false;
			}
		}
		return $valid;
	}



} // CompositeValidator

?>