Location: PHPKode > scripts > PleaseMap utilities > pleasemap-utilities/PleaseMap.php
<?php
// vim:ts=3:sw=3:sts=3:ft=php:
class PleaseMap {
	static function Array2Array (&$destination_array,$mapper,$source_array='_POST') {
		if (is_string($source_array) ) {
			// if the source_array is unspecified or a string, we assume it is
			// a global array,like _POST or _GET or such, otherwise it is an array that
			// the caller has passed to us explicitly
			global ${$source_array};
			$source_array = &${$source_array};
		}
		//  loop through specification array grabbing the proper source array variables
		//  placing them in the destination
		foreach ($mapper as $dest=>$src ) {
			echo("Mapping $dest and $src\n");
			$destination_array[$dest] = $source_array[$src];
		}
		return @$destination_array;
	}

	static function Object2Array (&$destination_array,$mapper=null, $source_class) {
		if (!$mapper) $mapper = 'pleaseMap';
		if (is_string($mapper) ) {
			// if the source specification map is not explicitly passed
			// to us, look for a 'pleaseMap' array in the source class
			//
			// if the source specification map IS explicitly passed
			// as a string, we take it as an alternat map name in the source class
			//
			// The source class will usually be the caller's class
			$mapper = &$source_class->$mapper;
		} // otherwise mapper is an array map passed to us 

		//  loop through the mapper array grabbing the proper source class variables
		//  placing them in the destination
		//  either by setting the value directly or calling the correct 'set' method
		foreach ($mapper as $dest=>$srcloc ) {
			// is there a getvalue method in the source?
			if (method_exists($source_class,$srcloc)) {
				$val = $source_class->$srcloc();
			} else $val = $source_class->$srcloc;

			$destination_array[$dest]= $val;
		}
		return @$destination_array;
	}


	static function Object2Object (&$destination_class,$mapper=null, $source_class) {
		if (!$mapper) $mapper = 'pleaseMap';
		if (is_string($mapper) ) {
			// if the source specification map is not explicitly passed
			// to us, look for a 'pleaseMap' array in the destination class
			//
			// if the source specification map IS explicitly passed
			// as a string, we take it as an alternat map name in the destination class
			//
			// The destination class will usually be the caller's class
			$mapper = &$destination_class->$mapper;
		} // otherwise mapper is an array map passed to us 

		//  loop through the mapper array grabbing the proper source class variables
		//  placing them in the destination
		//  either by setting the value directly or calling the correct 'set' method
		foreach ($mapper as $destloc=>$srcvar ) {
			// is there a getvalue method in the source to get the srcvar?
			if (method_exists($source_class,$srcvar)) {
				$val = $source_class->$srcvar( $destloc);
			} else $val = $source_class->$srcvar;

			if (method_exists($destination_class,$destloc)) {
				$destination_class->$destloc( $val,$key);
			} else $destination_class->$destloc = $val;
		}
		return @$destination_class;
	}


	static function Array2Object (&$destination_class,$mapper=null, $source_array='_POST') {
		if (!$mapper) $mapper = 'pleaseMap';
		if (!$source_array) $source_array = '_POST';
		if (is_string($source_array) ) {
			// if the source_array is unspecified or a string, we assume it is
			// a global array,like _POST or _GET or such, otherwise it is an array that
			// the caller has passed to us explicitly
			global ${$source_array};
			$source_array = &${$source_array};
		}
		if (is_string($mapper) ) {
			// if the source specification map is not explicitly passed
			// to us, look for a 'pleaseMap' array in the destination class
			//
			// if the source specification map IS explicitly passed
			// as a string, we take it as an alternat map name in the destination class
			//
			// The destination class will usually be the caller's class
			$mapper = &$destination_class->$mapper;
			//  and place them in the destination
		} 	//otherwise the mapper is an array passed by the caller

			//  if the source specification map is passed explicitly as an array, we
			//  loop through the map grabbing the proper source array variables
			//  placing them in the destination
			//  either by setting the value directly or calling the correct 'set' method
		foreach ($mapper as $dest=>$var ) {
			if (method_exists($destination_class,$dest)) {
				$destination_class->$dest( $source_array[$var],$var);
			} else $destination_class->$dest = $source_array[$var];
		}
		return @$destination_class;
	}

}
if (false) { //set true for test
class test {
	var $myName;
	// always remember array keys are destination variable names, values are the source field names
	var $pleaseMap = array ('var1' => 'var4', 'var2' => 'nonAvalue' , 'var3'=>'var5' );
	var $otherMap = array ('setmyvalue' => 'grabValue', 'var4' => 'grabValue' , 'var5'=>'var1' );

	var $var1="bob";
	var $var2="sam";
	var $var3="hal";
	var $var4="ellen";
	var $var5="candice";
	var $myvalue = 5;
	function setmyvalue ($v) {
		$this->myvalue=$v;
	}
	function grabvalue() {
		return "value from method";
	}
	function show($name) {
		$v = $this -> $name;
		echo (" class " . $this->myName . " variable $name is $v\n" );
		return $v;
	}
	function debug () {
		$debugMap = array ('var1' => 'show', 'var2' => 'show', 'var3' => 'show', 'var4' => 'show', 'var5' =>'show', 'myvalue'=> 'show');
		//  this prints all my variables
		pleaseMap::Object2Object($this,$debugMap,$this);
	}
	function clearAll() {
		$this->var1 = "------";
		$this->var2 = "------";
		$this->var3 = "------";
		$this->var4 = "------";
		$this->var5 = "------";
		$this->myvalue = "------";
	}

}

$c1= new test;
$c2= new test;
$c1 -> myName = "destination";
$c2 -> myName = "source";
$c2->debug();
$c1->clearAll();

echo("Object to Object test:");
echo("\nNow mapping source using default 'pleaseMap' specification arrray\n");
echo("the map of destination class elements from source values for array ('var1' => 'var4', 'var2' => 'nonAvalue' , 'var3'=>'var5' )");
echo("\n expected results ellen in var1,  empty  in  var2, and candice in var3\n");
pleaseMap::Object2Object($c1,null,$c2);
$c1->debug();
$c1->clearAll();

echo("\nNow mapping source using 'otherMap' specification arrray\n");
echo ("the map of destination class elements from source values for array ('setmyvalue' => 'grabValue', 'var4' => 'grabValue' , 'var5'=>'var1' )");
echo("\n expected results 'value from method' in myvalue,  'value from method in  var4, and bob in var5\n");
pleaseMap::Object2Object($c1,'otherMap',$c2);
$c1->debug();
$c1->clearAll();

echo("\n\nArray to Object test:");
echo("\nNow mapping source using 'otherMap' specification arrray from an array rather than a class\n");
echo("\n expected results 'just a constant' in myvalue,  'just a constant'  in  var4, and 1000 in var5\n");
pleaseMap::Array2Object($c1,'otherMap',array('grabValue'=> 'just a constant', 'var1' =>1000));
$c1->debug();

echo("\n\nObject to Array test:");
$destArray=array('key1'=>'some value', 'var4' => 'previous value');
$c2->debug();
print_r($destArray);

echo("\nNow mapping source using 'otherMap' specification arrray from an array rather than a class\n");
echo ("the map of destination array elements from source values for array ('setmyvalue' => 'grabValue', 'var4' => 'grabValue' , 'var5'=>'var1' )");
echo("\n expected results 'value from method' in setmyvalue,  'value from method'  in  var4, and bob in var5, while preserving 'key1' as 'some value'\n");
$destArray=pleaseMap::Object2Array($destArray,$c2->otherMap, $c2);
print_r($destArray);

echo("\n\nArray to Array test:");
$destArray=array('key1'=>'some value', 'var4' => 'previous value');
echo("\nNow mapping source using 'otherMap' specification arrray from an array rather than a class\n");
echo ("the map of destination class elements from source values for array ('myvalue'=>'thiskey', 'var4' => 'var4' )");
echo("\n expected results NULL in myvalue,  'source value'  in  var4,  while preserving 'key1' as 'some value'\n");
$destArray=array('key1'=>'some value', 'var4' => 'previous value');
$srcArray=array('key1'=>'some value', 'var4' => 'source value','thiskey'=>'will be ignored');
$destArray=pleaseMap::Array2Array($destArray,array('myvalue'=>'nonexisting', 'var4'=>'var4'), $srcArray);
print_r($destArray);

}

?>
Return current item: PleaseMap utilities