<?php
/*--------------------------------------------------------- 
 * File:conver.php
 * Author:Tomix
 * Description: 
 * Created:2009
 * -------------------------------------------------------
 * Modified history:2023/08/22
 * Copyright © 2023 蘋果世界股份有限公司 版權所有
 * -------------------------------------------------------
 */
class conver {
	// 將位結構的數組轉化為整型值。
	static public function arrayToInt(array $value): int {
		if (empty($value)) return 0;
		
		$nReturn = 0;
		foreach ($value as $item) {
			if (($nReturn & $item) <= 0) $nReturn += $item;
		}
		return $nReturn;
	}
	
	// 將位結構的整數轉化為位數組。
	static public function intToArray(int $value): array {
		if ($value <= 0) {
			return [0];
		} else {
			$arr = [];
			$nLeave = $value;
			$nT = 0x01;
			
			while ($nLeave > 0) {
				if (($nLeave & 1) == 1) $arr[] = $nT;
				$nLeave = ($nLeave >> 1);
				
				if ($nLeave > 0) $nT = ($nT << 1);
			}
			return $arr;
		}
	}
	
	// 數據庫sql語句過濾函數
	static public function toDbString(string $str): string {
		$str = str_replace("'", "&#039;", $str); // 替換單引號
		return $str;
	}
}
?>
