<?php
namespace App\Smarty\Functions;

class TrustLabel
{

	static public function create($params, $smarty)
	{

		// CHECK INPUT --------------------------------------------------------------------------------
		$defaultOutput = '';
		if ($params['default']) {
			$defaultOutput = $params['default'];
		}

		// GET TRUST DETAILS --------------------------------------------------------------------------
		// Trust object reference
		if (is_object($params['ref']) and get_class($params['ref'])=='Entity\Trust') {
			$trust = $params['ref'];
		}
		// trust id
		elseif ( is_numeric($params['ref']) and $params['ref']>0 ) {
			$trust = \Entity\Trust::select($params['ref']);
			if (!$trust->recordExists()) {
				return $defaultOutput;
			}
		}
		// something invalid
		else {
			return $defaultOutput;
		}

		// STORE TRUST DETAILS IN AN OUTPUT PARAMETER ($params['assign']) -----------------------------
		if ($params['assign']) {
			$smarty->assign($params['assign'], $trust);
			return '';
		}

		// CHECK OUTPUT FORMAT ------------------------------------------------------------------------
		if (!$params['format']) {
			return $trust->name ? $trust->name : $defaultOutput;
		}
		else {
			$replaceArray = [];
			$replaceArray['%name%'] = $trust->name;
			return str_replace(array_keys($replaceArray), array_values($replaceArray), $params['format']);
		}
	}

}