<?php

	/**
	 * Static Helper Class to perform array to PHP code conversion
	 *
	 */
	class PhpArrayFile
	{
		/**
		 * @var string Comment included with all generated Files
		 * @todo Make an accessible property
		 */
		const HEADER_TRANSLATION_COMMENT = '
	/**
	 *
	 * Translation file
	 *
	 **/
	 ';
		const HEADER_CONFIGS_COMMENT = '
	/**
	 *
	 * Do not change something here !!!
	 *
	 **/
	 ';

		/**
		 * @var int Internal Recursion Counter for recurse()-Method
		 */
		protected static $recursionLevel = 0;

		/**
		 * @param array $array The array to convert to PHP code. CAUTION: The array may contain any nested combination keys, values and arrays, BUT IT MUST NOT contain a value with the integer index 0 (String Value '0' and unindexed arrays are allowed).
		 * @return string
		 */
		public static function getFromArray(array $array, $encodeHtml = true, $header = self::HEADER_TRANSLATION_COMMENT)
		{	
			$returnString = "<?php\n"
				. str_replace('{CREATION_DATE}', date('c'), $header). "\n\treturn array(\n";
			//self::$recursionLevel = 0;	// initialise recursion level value
			$returnString .= self::recurse($array, $encodeHtml);
			$returnString .= "\t);";
			return $returnString;
		}

		/**
		 * Recursive Converter Function
		 * @param array $array
		 * @return string The PHP Code representation, in a nicely formatted way
		 */
		protected static function recurse(array $array, $encodeHtml)
		{
			$returnString = "";
			self::$recursionLevel++;
			foreach ($array as $key => $value)
			{
				$returnString .= self::getCurrentIndentation ();
				/** Evaluate KEY **/
				if (is_string($key))
					$returnString .= "'$key' => ";
				elseif($key === 0) {	// Start of an unindexed array
					$returnString .= self::implodeUnindexed($array) . "\n";
					break;
				} else
					$returnString .= $key . ' => ';

				/** Evaluate VALUE **/
				if ($value == null) {
					$value = 'null';
					$returnString .= $value;
				}else if (is_string($value))
				{
					if ($encodeHtml) {
						$value = str_replace("'", "\'", $value);
						$value = str_replace("\r\n", "<br />", $value);
						$value = str_replace("\n", "<br />", $value);
					} else {
                                                $value = str_replace("'", "\'", $value);
					}
					$returnString .= "'$value'";
				}
				elseif (is_array($value)) {
					$returnString .= "array(\n";
					$returnString .= self::recurse($value, $encodeHtml);
					$returnString .= self::getCurrentIndentation() . ")";
				}
				elseif (is_bool($value))
					$returnString .= $value ? 'true' : 'false';
				else
					$returnString .= $value;

				$returnString .= ",\n";
				//dump($returnString);
			}
			self::$recursionLevel--;
			return $returnString;
		}

		/**
		 * Get Indentation (currently harcoded to Tabs) for current recursion level
		 * @return string A string with the indentation characters
		 */
		protected static function getCurrentIndentation()
		{
			$returnString = "\t";
			for($i=0; $i<self::$recursionLevel; ++$i)
				$returnString .= "\t";
			return $returnString;
		}

		/**
		 * An equivalent of PHP's implode function for arrays - this array-safe
		 * concatenation of strings (require quoting) vs. simple type values
		 * @param array $values
		 * @return string
		 */
		protected static function implodeUnindexed(array $values)
		{
			$returnString = '';
			foreach ($values as $key => $value)
			{
				if ($key !== 0)
					$returnString .= ', ';
				if (is_string($value))
					$returnString .= "'$value'";
				else
					$returnString .= $value;
			}
			return $returnString;
		}

	}
