class_mcArray.inc.php 5.19 KB
<?php
/**
 * @todo
 *	könnte in
 *	SEOLinkCache eingesetzt werden
 */

class mcArray {
	
	const ARRAY_MIDDLE = '=';
	const ARRAY_START = '|_';
	const ARRAY_STOP = '_|';
	/**
	 *	transform a array to a string  ... in example for SEOLink
	 * 
	 * @phpunit
	 * 
	 * @param array $param_Array -- array( key1=>var1, key2=>var2, ...)
	 * @param string $param_StartSeperator
	 * @param string $param_MiddleSeperator
	 * @param string $param_StopSeperator
	 * @return string -- $param_StartSeperator
	 *						.key1.$param_MiddleSeperator.var1.$param_MiddleSeperator
	 *						.key2.$param_MiddleSeperator.var2.$param_StopSeperator
	 */
	public static function transformArrayToString(array $param_Array, $param_StartSeperator=SEOLink::ARRAY_START, $param_MiddleSeperator=SEOLink::ARRAY_MIDDLE, $param_StopSeperator=SEOLink::ARRAY_STOP) {
		$return = '';
		if(is_array($param_Array)) {
			$return .= $param_StartSeperator;
			$tmp_notfirst = false;
			foreach($param_Array as $Aindex => $Avalue) {
				if(is_array($Avalue)) {
					D::cf('transformArrayToSEOLink rekursiv fürs Array noch nicht entwickelt');
				} else {
					if($tmp_notfirst) {
						$return .= ''.$param_MiddleSeperator;
					} else {
						$tmp_notfirst = true ;
					}
					$return .= ''.$Aindex.$param_MiddleSeperator.$Avalue;
				}
			}
			$return .= $param_StopSeperator;
		}

				
		return $return;
	}	
	
	
	/**
	 *	check if key path is in array and value is not emtpy array
	 * 
	 * @phpunit
	 * 
	 * @param array $param_Array
	 * @param string $param_key	-- $param_Array[$param_key] 
	 * @param string $param_key2 -- $param_Array[$param_key][$param_key2]
	 * @param string $param_key3 -- $param_Array[$param_key][$param_key2][$param_key3]
	 * @param string $param_key4 -- $param_Array[$param_key][$param_key2][$param_key3][$param_key4] 
	 * @param string $param_key5 -- $param_Array[$param_key][$param_key2][$param_key3][$param_key4][$param_key5] 
	 * @param string $param_key6 -- $param_Array[$param_key][$param_key2][$param_key3][$param_key4][$param_key5][$param_key6]
	 * @return bool 
	 */
	public static function checkKeyHasFullArray($param_Array, $param_key, $param_key2=null, $param_key3=null, $param_key4=null, $param_key5=null, $param_key6=null) {
		if(is_array($param_Array)) {
			if(array_key_exists($param_key, $param_Array) 
					&& is_array($param_Array[$param_key]) 
					&& !empty($param_Array[$param_key])
			) {		
				if(is_null($param_key2)) {
					return true;
				} else {
					return mcArray::checkKeyHasFullArray($param_Array[$param_key], $param_key2, $param_key3, $param_key4, $param_key5);
				}
				
			} else {
				return false;
			}			
		} else {
			return false;
		}

	}
	
	public static function returnValueFromKeyPath(array $param_PathAsArray, array $param_Array) {
		$searcharray = $param_Array;
		foreach($param_PathAsArray as $key) {
			if(is_array($searcharray)) {
				if(array_key_exists($key, $searcharray)) {
					$searcharray = $searcharray[$key];
				}
			} else {
			  return false;
			}
		}
		return $searcharray;
	}

	public static $ARRAY_PATTERN = '_#_';
	/**
	 *	transformiert Array mit speziellen String Key zu einem normalen Key
	 *
	 * @param array param_Array -- Array welches transformiert wird
	 * @param string param_pattern -- Pattern für den Schlüssel (im Beispiel '_#_')
	 *
	 * Array
		(
			[bleibt] =>
			[data_#_1_#_Subject] => kein Betreff
		)
	 *
	 *
	 *	Array
		(
			[bleibt] =>
			[data] => Array
				(
					[1] => Array
						(
							[Subject] => kein Betreff
						)

				)
		)
	 */
	public static function transformArrayWithStringKeyToNormalKey(array &$param_Array, $param_pattern=null) {
//		D::show($param_Array, '$param_Array');
		if(is_null($param_pattern)) {
			$param_pattern = mcArray::$ARRAY_PATTERN;
		}

		$tmp_POST = array();
		$tmp_Array = $param_Array;
		foreach($tmp_Array as $post_key => $post_value) {

			if(FALSE!=strpos($post_key, $param_pattern)) {
				$tmp_POST[$post_key] = array();
				$array_key = split($param_pattern, $post_key);

				$tmp_POST_key = null;

				$tmp_POST[$post_key][0]  = array();

				$count_array_key = count($array_key);
				for($i=0; $i<$count_array_key; $i++) {
					if(is_null($tmp_POST_key)) {
						$tmp_POST_key = $array_key[$i];
					}

					if(!array_key_exists($array_key[$i], $tmp_POST[$post_key][$i])) {
						$tmp_POST[$post_key][$i][$array_key[$i]] = array();
					}

					if($count_array_key == $i+1) {
						$tmp_POST[$post_key][$i][$array_key[$i]]  = $post_value;
					} else {
						$tmp_POST[$post_key][$i+1] =& $tmp_POST[$post_key][$i][$array_key[$i]];
					}
				}

				mcArray::addArrayValueToArray($param_Array, $tmp_POST[$post_key][0]);

				unset($param_Array[$post_key]);
			} else {
				$param_Array[$post_key] = $post_value;
			}
		}
	}

	/**
	 *	fügt Tabelle mit Werten zu einer bestehenden Tabelle
	 *
	 *	siehe phpunit/class_mcArray_PHPTest.inc.php 
	 *
	 * @param array $param_Array
	 * @param <type> $param_ArrayValue
	 */
	public static function addArrayValueToArray(array &$param_Array, $param_ArrayValue) {
		
		$value_key = array_keys($param_ArrayValue);

		foreach ($value_key as $key) {
			if(array_key_exists($key, $param_Array)) {
				mcArray::addArrayValueToArray($param_Array[$key], $param_ArrayValue[$key]);
			} else {
				$param_Array[$key] = $param_ArrayValue[$key];
			}
		}

	}
}
?>