<?php
namespace App\Smarty\Functions;

class CustomerSegmentLabel
{


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

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

		// GET CUSTOMER SEGMENT PROFILE DETAILS -------------------------------------------------------
		// object reference
		if (is_object($params['ref']) and get_class($params['ref'])=='Entity\CustomerSegment') {
			$ent = $params['ref'];
		}
		// just the id
		elseif ( is_numeric($params['ref']) and $params['ref']>0 ) {
			$ent = \Entity\CustomerSegment::select($params['ref']);
			if (!$ent->recordExists()) {
				return $defaultOutput;
			}
		}
		// something invalid
		else {
			return $defaultOutput;
		}

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

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

			return str_replace(array_keys($replaceArray), array_values($replaceArray), $params['format']);
		}

	}

}