Location: PHPKode > projects > PWF > PWF_0.3.0/Data/Assigner.php
<?php
/**
 * Assigners, assigns values to objects properties or to list of objects.
 * @copyright	Copyright (c) 2012, PWF
 * @license		http://phpwebframework.com/License 
 * @version PWF_0.3.0
 */
class _Data_Assigner
{

	/**
	 * @var _Core_ControllerAbstract
	 */
	public $c;

	/**
	 * Properties files that have been used in order to map an array to an object
	 * @var array
	 */
	private $mappers;

	/**
	 * The Controller that instantiate the assigner
	 * @param _Core_ControllerAbstract $controller
	 */
	public function __construct(_Core_ControllerAbstract $controller)
	{
		$this->c = $controller;
	}


	/**
	 * Assigns values to properties of an object from an associative array.
	 * @param array $array . The array to be mapped. If $mapper is not set then native behavior will
	 * be used(e.g. array key value $r["CATEGORY_ID"] will mapped to object's property $categoryId) .
	 * @param _Type_Object $object The object that values will map to.
	 * @param string $mapper . If passed it will use this file name in order to instantiate a
	 * _Core_Properties and use it to map the array to object (e.g. CATID:categoryId will
	 * map array key value $r["CATID"] to object's property $categoryId)
	 */
	public function map($array,_Type_Object $object,$mapper = null)
	{
		if($mapper == null)
		{
			foreach($array as $key=>$value)
			{
				$methodName = "set";
				$methodParts = explode("_",$key);
				for($i=0;$i<count($methodParts); $i++)
				{
				  
					$methodName .= _String::capitalizeFirstLetter($methodParts[$i]);
				}
				if($object->methodExist($methodName))
				{
					$object->$methodName($value);
				}
			}
		}
		else
		{
			if(!isset($this->mappers[$mapper]))
			{
				$this->mappers[$mapper] = new _Properties($mapper);
			}
			foreach($array as $key=>$value)
			{
				if($this->mappers[$mapper]->containsKey($key))
				{
					$methodName = "set"._String::capitalizeFirstLetter($this->mappers[$mapper]->get($key));
					if($object->methodExist($methodName))
					{
						$object->$methodName($value);
					}
				}
			}
		}
	}
}
?>
Return current item: PWF