<?php
namespace App\Smarty\Functions;

/**
 * This label works on mvc listing
 *
 * Display usernames, taking into account locale configs in various ways.
 *
 */
class NameFormat
{

	/**
	 * @param Array $params
	 * @return String
	 */
	static public function create($params, $smarty)
	{
		return sprintf("<span>%s</span>", static::renderStr((string)$params['firstName'], (string)$params['lastName'], (string)$params['middleName']));
	}

	/**
	 * Renders the full name based on the configured name format configuration.
	 *
	 * @param string $firstName
	 * @param string $lastName
	 * @param string $middleName (Optional) The middle name
	 * @throws \Exception Unsupported name format configured: {$CFG['localization']['nameFormat']}
	 * @return string
	 */
	static public function renderStr(string $firstName, string $lastName, string $middleName='') : string
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
		switch ($CFG['localization']['nameFormat']) {
			case 'last,first,middle':
				return implode(" ", array_filter([
					$lastName,
					$firstName,
					$middleName,
				]));
			break;

			case 'first,middle,last':
				return implode(" ", array_filter([
					$firstName,
					$middleName,
					$lastName,
				]));
			break;

			default:
				throw new \Exception("Unsupported name format configured: {$CFG['localization']['nameFormat']}");
		}
	}
}