<?php
/**
 * This file is part of the set of generic classes available for implementations
 * of the DDR Simple API to speed up implementation. It contains a generic
 * implementation of the {@link PropertyValues} interface.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package AskPythia
 * @subpackage Implementation
 * @version $Revision: 1.11 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 */

/**
 * Include the {@link PropertyValues} interface description.
 */
require_once(dirname(__FILE__).'/../../interface/propertyValues.php');
/**
 * Include the {@link PropertyRef} interface description.
 */
require_once(dirname(__FILE__).'/../../interface/propertyRef.php');
/**
 * Include the {@link PropertyValue} interface description.
 */
require_once(dirname(__FILE__).'/../../interface/propertyValue.php');
/**
 * Include the {@link NameException} class definition as
 * {@link getValue()} raises this exception when the argument is invalid.
 */
require_once(dirname(__FILE__)."/../../interface/nameException.php");
/**
 * Include the {@link SystemException} class definition as
 * {@link getValue()} and {@link add()} raise this exception when the argument
 * is invalid.
 */
require_once(dirname(__FILE__)."/../../interface/systemException.php");

/**
 * A basic {@link PropertyValues} holds a list of {@link PropertyValue}
 * instances.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package AskPythia
 * @subpackage Implementation
 * @link http://www.w3.org/TR/DDR-Simple-API/ Device Description Repository Simple API
 * @version $Revision: 1.11 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 */
class BasicPropertyValues implements PropertyValues{
	/**
	 * @var array(PropertyValue) The list of property values.
	 * @access private
	 */
	private $properties;
	
	/**
	 * Creates an instance of the BasicPropertyValues class.
	 *  
	 * @return BasicPropertyValues The created instance.
	 */
	public function __construct(){
		$this->properties = array();
	}
	
	/**
	 * Adds the given property value to the list.
	 * 
	 * @param PropertyValue $propertyValue property value that needs to be added to the list.
	 * @exception SystemException The property valus is not valid.
	 * @access public
	 */
	public function add($propertyValue){
		if(!isset($propertyValue)){
			throw new SystemException(
				'Property value cannot be NULL.',
				SystemException::$ILLEGAL_ARGUMENT);
		}
		if(!($propertyValue instanceof PropertyValue)){
			throw new SystemException(
				'The property value does not implement the PropertyValue interface.',
				SystemException::$ILLEGAL_ARGUMENT);
		}
		$this->properties[] = $propertyValue;
	}
		
	public function getAll(){
		return $this->properties;
	}
	
	public function getValue($propertyRef){
		if(!isset($propertyRef)){
			throw new SystemException(
				'Property reference cannot be NULL.',
				SystemException::$ILLEGAL_ARGUMENT);
		}
		if(!($propertyRef instanceof PropertyRef)){
			throw new SystemException(
				'The property reference does not implement the PropertyRef interface.',
				SystemException::$ILLEGAL_ARGUMENT);
		}
		
		foreach($this->properties as $prop){
			$ref = $prop->getPropertyRef();
		 
			if (($ref->getLocalPropertyName() == $propertyRef->getLocalPropertyName())
				&& ($ref->getNamespace() == $propertyRef->getNamespace())
				&& ($ref->getAspectName() == $propertyRef->getAspectName())){
				return $prop;
			}
		}
		
		// TODO: The NameException should include a more specific code that
		// tells which part of the property reference is wrong. That's mostly
		// impossible to do in a generic way, as it depends on the list of
		// supported namespaces, aspects and property names of the implementation.
		throw new NameException(
			"No property value found in the list for the requested property referenced.");  
	}
}
?>