<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
//Convert number to words

function __get_word($number, $dictionary){
		//$number = number_format($number,2, '.', '');
		$hyphen      = $dictionary['seperator']['hyphen'];
		$conjunction = $dictionary['seperator']['and'];
		$cent_word =  $dictionary['seperator']['cent'];
		$separator   = $dictionary['seperator']['separator'];
		$negative    = $dictionary['seperator']['negative'];
		$decimal     = $dictionary['seperator']['decimal'];
		$only     = $dictionary['seperator']['only'];
		
		if (!is_numeric($number)) {
			return false;
		}
		
		if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
			// overflow
			trigger_error(
				'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
				E_USER_WARNING
			);
			return false;
		}
	
		if ($number < 0) {
			return $negative . __get_word(abs($number),$dictionary);
		}
		
		$string = $fraction = null;
		
		
		
		switch (true) {
			case $number < 21:
				$string = $dictionary[$number];
				break;
			case $number < 100:
				$tens   = ((int) ($number / 10)) * 10;
				$units  = $number % 10;
				$string = $dictionary[$tens];
				if ($units) {
					$string .= $hyphen . $dictionary[$units];
				}
				break;
			case $number < 1000:
				$hundreds  = $number / 100;
				$remainder = $number % 100;
				$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
				if ($remainder) {
					$string .= $conjunction . __get_word($remainder,$dictionary);
				}
				break;
			default:
				$baseUnit = pow(1000, floor(log($number, 1000)));
				$numBaseUnits = (int) ($number / $baseUnit);
				$remainder = $number % $baseUnit;
				$string = __get_word($numBaseUnits,$dictionary) . ' ' . $dictionary[$baseUnit];
				if ($remainder) {
					$string .= $remainder < 100 ? $conjunction : $separator;
					$string .= __get_word($remainder,$dictionary);
				}
				break;
		}
		return $string;
}


if ( ! function_exists('num_to_word')){
	
	// function num_to_word($number,$dictionary) {		
		
	// 	$hyphen      = $dictionary['seperator']['hyphen'];
	// 	$conjunction = $dictionary['seperator']['and'];
	// 	$separator   = $dictionary['seperator']['separator'];
	// 	$negative    = $dictionary['seperator']['negative'];
	// 	$decimal     = $dictionary['seperator']['decimal'];
		
	// 	if (!is_numeric($number)) {
	// 		return false;
	// 	}
		
	// 	if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
	// 		// overflow
	// 		trigger_error(
	// 			'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
	// 			E_USER_WARNING
	// 		);
	// 		return false;
	// 	}
	
	// 	if ($number < 0) {
	// 		return $negative . num_to_word(abs($number),$dictionary);
	// 	}
		
	// 	$string = $fraction = null;
		
	// 	if (strpos($number, '.') !== false) {
	// 		list($number, $fraction) = explode('.', $number);
	// 	}
		
	// 	switch (true) {
	// 		case $number < 21:
	// 			$string = $dictionary[$number];
	// 			break;
	// 		case $number < 100:
	// 			$tens   = ((int) ($number / 10)) * 10;
	// 			$units  = $number % 10;
	// 			$string = $dictionary[$tens];
	// 			if ($units) {
	// 				$string .= $hyphen . $dictionary[$units];
	// 			}
	// 			break;
	// 		case $number < 1000:
	// 			$hundreds  = $number / 100;
	// 			$remainder = $number % 100;
	// 			$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
	// 			if ($remainder) {
	// 				$string .= $conjunction . num_to_word($remainder,$dictionary);
	// 			}
	// 			break;
	// 		default:
	// 			$baseUnit = pow(1000, floor(log($number, 1000)));
	// 			$numBaseUnits = (int) ($number / $baseUnit);
	// 			$remainder = $number % $baseUnit;
	// 			$string = num_to_word($numBaseUnits,$dictionary) . ' ' . $dictionary[$baseUnit];
	// 			if ($remainder) {
	// 				$string .= $remainder < 100 ? $conjunction : $separator;
	// 				$string .= num_to_word($remainder,$dictionary);
	// 			}
	// 			break;
	// 	}
		
	// 	if (null !== $fraction && is_numeric($fraction)) {
	// 		$string .= $decimal;
	// 		$words = array();
	// 		foreach (str_split((string) $fraction) as $number) {
	// 			$words[] = $dictionary[$number];
	// 		}
	// 		$string .= implode(' ', $words);
	// 	}
		
	// 	return $string;
	// }

	
	function num_to_word($number,$dictionary) {	
		$number = number_format($number,2, '.', '');
		$hyphen      = $dictionary['seperator']['hyphen'];
		$conjunction = $dictionary['seperator']['and'];
		$cent_word =  $dictionary['seperator']['cent'];
		$separator   = $dictionary['seperator']['separator'];
		$negative    = $dictionary['seperator']['negative'];
		$decimal     = $dictionary['seperator']['decimal'];
		$only     = $dictionary['seperator']['only'];

		$fraction = null;	
		$return  ="";

		if (strpos($number, '.') == false) {
			//list($number, $fraction) = explode('.', $number);
			$return = __get_word($number,$dictionary)." Ringgit ".$only; //ringgint to be in lang later
		}else{
			list($number, $fraction) = explode('.', $number);
			$return = __get_word($number,$dictionary)." Ringgit ";
			if( !is_null($fraction) && $fraction  > 0 ){

				$return .= __get_word((int)$fraction,$dictionary).$cent_word;
			}
			$return.=$only;
		}
		return $return;
	}

}
