class_mcArray.inc.php 9.37 KB
<?php
/** 
 * @copyright (c) 2014, netz.coop eG
 */
/**
 * @todo
 *	könnte in
 *	SEOLinkCache eingesetzt werden
 */

class mcArray {
	
	const ARRAY_MIDDLE = '1M1';
	const ARRAY_START = '1S1';
	const ARRAY_STOP = '1E1';
	
	

	
	
	/**
	 *	transform a array to a string  ... in example for SEOLink
	 * 
	 * @todo evt. serialize()/unserialize()+str_replace Lösungsweg versuchen, weil vllt. schneller
	 * 
	 * @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;
	}	
	
	
	public static function transformStringToArray($param_string, $param_StartSeperator=SEOLink::ARRAY_START, $param_MiddleSeperator=SEOLink::ARRAY_MIDDLE, $param_StopSeperator=SEOLink::ARRAY_STOP) {
//D::cf($param_string)		;
		$pos_APS = strpos($param_string, $param_StartSeperator);
		$param_string = substr($param_string, $pos_APS+  strlen($param_StartSeperator));
//D::cf($param_string)		;
		$param_string = substr($param_string, 0, strpos($param_string, $param_StopSeperator));
//D::cf($param_string)		;
		$AddonArray_Before = explode($param_MiddleSeperator, $param_string);
//D::show($AddonArray_Before, '$AddonArray_Before');		
		$AddonArray = array();
		$AddonArray_Before_count = count($AddonArray_Before);
		for($i=0; $i<$AddonArray_Before_count; $i++) {
			if ($i%2 === 0) {
				$j = $i+1;
				$AddonArray[$AddonArray_Before[$i]] = $AddonArray_Before[$j];
			}
		}		
//D::show($AddonArray, '$AddonArray');
		return $AddonArray;
	}
	
	
	/**
	 *	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( (is_string($param_key) || is_integer($param_key))
					&& 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;
		}

	}
	
	
	/**
	 * 
	 * 
	 * 
	 * @param array $param_PathAsArray => example: array(key1, key2, key3, key4, value)
	 * @param array $param_ResultArray => example: array[key1][key2][key3][key4] = value -- 
	 *				Achtung da die Funktion array_merge_recursive genutzt wird, ergeben sich komische Effekte bein integer keys
	 *				array_merge_recursive wird nur genutzt beim $param_ResultArray - im Zweifel mcArray::putIntKeyPathValueToArray nutzen
	 * 
	 * @return array zusammengeführtes Arry vom neuen generierten Array aus $param_PathAsArray und  param_ResultArray
		Array(
			[key1] => Array
				(
					[key2] => Array
						(
							[keyA] => Array
								(
									[keyB] => value
								)
							[key3] => Array
								(
									[key4] => value
								)
						)
				)
		)
	 * 
	 * 
	 */
	public static function putStringKeyPathValueToArray(array $param_PathAsArray, $param_ResultArray=array()) {
		$new_array = array();
		$firstkey = array_shift($param_PathAsArray);
//		$firstkey = (string)'_'.$firstkey."_";
		$firstkey = (string) ''.$firstkey."";
		$new_array[$firstkey] = array();

		if(count($param_PathAsArray) == 1) {
			$new_array[$firstkey] = array_shift($param_PathAsArray);
		} else {
			$new_array[$firstkey] = mcArray::putStringKeyPathValueToArray($param_PathAsArray);
		}

		if(!empty($param_ResultArray)) {
			$result_array = array_merge_recursive($param_ResultArray, $new_array);
		} else {
			$result_array = $new_array;
		}
		
		return $result_array;
	}	
	/**
	 * analog zu mcArray::putStringKeyPathValueToArray nur auch mit Int Key möglich
	 * 
	 * @param array $param_PathAsArray
	 * @param type $param_ResultArray
	 * @return type
	 */
	public static function putIntKeyPathValueToArray(array $param_PathAsArray, $param_ResultArray=array()) {
//		$new_array = array();
		$firstkey = array_shift($param_PathAsArray);
		$firstkey = (string)''.$firstkey."";
		
		if(!array_key_exists($firstkey, $param_ResultArray)) {
			$param_ResultArray[$firstkey] = array();
		}

		if(count($param_PathAsArray) == 1) {
			$param_ResultArray[$firstkey] = array_shift($param_PathAsArray);
		} else {
			$param_ResultArray[$firstkey] = mcArray::putIntKeyPathValueToArray($param_PathAsArray, $param_ResultArray[$firstkey]);
		}		

		return $param_ResultArray;
	}	
	
	/**
	 * Gegenstück zu self::putKeyPathValueToArray()
	 * 
	 * @param array $param_PathAsArray => example: array(key1, key2, key3, key4)
	 * @param array $param_Array => example: self::putKeyPathValueToArray()
	 * @return boolean
	 */
	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;
				}
			} 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 '_#_') (darf nicht zB -+- sein)
	 *
	 * 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]];
					}
				}
//D::show($tmp_POST[$post_key][0], '$tmp_POST['.$post_key.'][0]');
				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];
			}
		}

	}

	
	/**
	 *	vergleicht 2 arrays und gibt true zurück wenn positiv
	 * 
	 * @param array $param_array1
	 * @param array $param_array2
	 * @return bool 
	 */
	public static function compare(array $param_array1, array $param_array2) {
		$serialize1 = serialize($param_array1);
		$serialize2 = serialize($param_array2);
		if(strcmp($serialize1, $serialize2) === 0) {
//			D::cf($serialize2);
			return true;
		} else {
//			D::cf($serialize1.' != '.$serialize2);
			return false;
		}
	}
	
}
?>