<?php
/**
* 汎用的な関数を集めたクラス
*
* PHP version 5
*
* @package    core
* @author     stk2k <stk2k@sazysoft.com>
* @copyright  2008 stk2k, sazysoft
*/

class System
{
	/**
	 *	呼び出し箇所
	 *
	 *	@return list($file,$line) ファイル名、行番号の配列
	 */
	public static function caller( $back = 0 )
	{
		$bt = debug_backtrace();
		$trace = $bt[1 + $back];

		return array( $trace['file'], $trace['line'] );
	}

	/*
	 *    現在時間を取得
	 */
	public static function now(){ 
		list($msec,$sec) = explode(' ',microtime()); 
		return ( (float)$msec + (float)$sec );
	}

	/**
	 *	２つの日付の差分を日単位で求める
	 */
	public static function dateDiff( $date1, $date2 = NULL ){
		if ( $date2 == NULL ){
			// 今日の日付
			$date2 = date("Y/m/d");
		}
		$result = strtotime($date2) - strtotime($date1);
		$result = intval( $result / (24 * 60 * 60));
		return $result;
	}

	/**
	 *	２つの日付を比較
	 */
	public static function compareDate( $date1, $date2 = NULL ){
		if ( $date2 == NULL ){
			// 今日の日付
			$date2 = date("Y/m/d");
		}
		$result = strtotime($date1) - strtotime($date2);
		return $result;
	}

	/**
	 *    変数をダンプする
	 *
	 */
	public static function dump( $var, $max_string_length = 40 )
	{
		self::_dump( $var, 0, $max_string_length );
	}

	public static function _dump( $var, $depth, $max_string_length )
	{
		$type = gettype($var);
		switch( $type ){
		case 'string':
		case 'integer':
		case 'double':
		case 'boolean':
		case 'NULL':
		case 'unknown type':
			{
				$str = strval($var);
				if ( strlen($str) > $max_string_length ){
					$str = substr( $str, 0, $max_string_length ) . '...';
				}
				$str = htmlspecialchars( $str, ENT_QUOTES );
				echo str_repeat( '-', $depth * 4 ) . "[$type]$str<br>";
			}
			break;
		case 'array':
			{
				echo str_repeat( '-', $depth * 4 ) . "[array]<br>";
				foreach( $var as $key => $value ){
					echo str_repeat( '-', ($depth + 1)*4 ) . "[$key]==&gt;<br>";
					System::_dump( $value, $depth + 2, $max_string_length );
				}
			}
			break;
		case 'object':
			{
				if ( $var instanceof Iterator ){
					$clazz = get_class( $var );
					echo str_repeat( '-', $depth * 4 ) . "[object(Iterator)]$clazz<br>";
					foreach( $var as $key => $value ){
						echo str_repeat( '-', ($depth + 1)*4 ) . "[$key]==&gt;<br>";
						System::_dump( $value, $depth + 2, $max_string_length );
					}
				}
				else{
					$obj_vars = get_object_vars( $var );
					$clazz = get_class( $var );
					echo str_repeat( '-', $depth * 4 ) . "[object]$clazz<br>";
					foreach( $obj_vars as $key => $value ){
						echo str_repeat( '-', ($depth + 1)*4 ) . "[$key]==&gt;<br>";
						System::_dump( $value, $depth + 2, $max_string_length );
					}
				}
			}
			break;
		}
	}

	/** 
	 *	文字列のエンコーディング判定
	 */
	public static function detectEncoding( $str, $detect_order = "EUC-JP, SJIS, JIS, UTF-8" ){

		return mb_detect_encoding( $str, $detect_order, TRUE );

	}

	/**
	 *	エンコード変換
	 */
	public static function convertEncoding( $str, $to_encoding = NULL, $from_encoding = NULL )
	{
		if ( is_string($str) && $to_encoding ){
			// エンコードあり
			return mb_convert_encoding($str,$to_encoding, $from_encoding);
		}
		// エンコード無し
		return $str;
	}

	/**
	 *	再帰エンコード変換
	 */
	public static function convertEncodingRecursive( $var, $to_encoding = NULL, $from_encoding = NULL )
	{
		$type = gettype($var);
		switch( $type ){
		case 'string':
			{
				return mb_convert_encoding($var,$to_encoding, $from_encoding);
			}
			break;
		case 'integer':
		case 'double':
		case 'boolean':
		case 'NULL':
		case 'unknown type':
			break;
		case 'array':
			{
				$newArray = array();
				foreach( $var as $key => $value ){
					$newArray[ $key ] = System::convertEncodingRecursive( $value, $to_encoding, $from_encoding );
				}
				return $newArray;
			}
			break;
		case 'object':
			{
				$newObject = clone $var;
				if ( $var instanceof Iterator ){
					foreach( $var as $key => $value ){
						$newObject->$key = System::convertEncodingRecursive( $value, $to_encoding, $from_encoding );
					}
					return $newObject;
				}
				else{
					$obj_vars = get_object_vars( $var );
					foreach( $obj_vars as $key => $value ){
						$newObject->$key = System::convertEncodingRecursive( $value, $to_encoding, $from_encoding );
					}
					return $newObject;
				}
			}
			break;
		}

		return $var;
	}

}

return __FILE__;
