<?php
namespace App\GenDocument\TextProcessor;

class DefaultFunctions extends \Engine\GenDocument\TextProcessor\DefaultFunctions
{
	/**
	 * Convert a number to its literal representation
	 * return string
	 * */
	public function toLetters($lang='', $currency='', $separator='', $currencySubdivision='')
	{

		if (!$lang){
			$outputLang = 'EN';
		} else {
			$outputLang = $lang;
		}

		$class = sprintf('\App\Utils\DigitsToLetters\%s', $outputLang);
		if (!class_exists($class)) {
			$class = '\App\Utils\DigitsToLetters\EN';
		}

		// auto-detect subdivision
		if ($currencySubdivision) {
			$finalSubdivision = $currencySubdivision;
		} elseif ($currency) {
			$currDet = \App\Config\Currencies::getInstance()->get($currency);
			$finalSubdivision = $currDet['subdivisionName'];
		} else {
			$finalSubdivision = '';
		}


		$text = $class::create($this->value, $currency, $finalSubdivision, $separator)->getText();

		return new self((string)$text);
	}

	/**
	 * Instantiate an entity of type $objectType, and id $this->value
	 * return \App\Entity\GeneralAbstract
	 * */
	public function instantiateEntity($objectType){

		$className='\Entity\\'.$objectType;
		if (class_exists($className) && is_subclass_of($className, "\App\Entity\SelectAbstract")) {
			return new static($className::select($this->value));
		}

		throw new \Exception("Class '{$objectType}' doesn't exist!");
	}

	/**
	 * Convert a number to a specific format (ex: eastern arabic digits)
	 * @param string $format
	 * @return string
	 */
	public function numeralsConvert($format)
	{
		return \App\Utils\NumeralsConvert::convert($this->value, $format);
	}
}
