<?php
/**
 * @package    VP Email Manager
 * @copyright  Copyright (C) 2012-2021 VirtuePlanet Services LLP. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 * @author     Abhishek Das <info@virtueplanet.com>
 * @link       https://www.virtueplanet.com
 */

defined('_JEXEC') or die;

/**
* Do helper class to handle custom Mustache helper callbacks.
*/
class VpemHelperMustacheDo
{
	public function escape()
	{
		return array($this, '_escape');
	}
	
	public function strip_tags()
	{
		return array($this, '_strip_tags');
	}
	
	public function link()
	{
		return array($this, '_link');
	}
	
	public function date()
	{
		return array($this, '_date');
	}
	
	public function time()
	{
		return array($this, '_time');
	}
	
	public function _escape($string, \Mustache_LambdaHelper $helper)
	{
		$string = $helper->render($string);
		
		return htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
	}
	
	public function _strip_tags($string, \Mustache_LambdaHelper $helper)
	{
		$string = $helper->render($string);
		
		return strip_tags($string);
	}
	
	public function _link($string, \Mustache_LambdaHelper $helper)
	{
		$args = explode(',', $string);
		$args = array_map('trim', $args);
		
		foreach ($args as &$arg)
		{
			$arg = $helper->render($arg);
		}
		
		return call_user_func_array(['Joomla\CMS\HTML\HTMLHelper', 'link'], $args);
	}
	
	public function _date($string, \Mustache_LambdaHelper $helper)
	{
		$args = explode(',', $string);
		$args = array_map('trim', $args);
		
		foreach ($args as &$arg)
		{
			$arg = $helper->render($arg);
		}
		
		if (isset($args[0]) && $args[0] <= 0)
		{
			return '';
		}
		
		if (!isset($args[1]))
		{
			$args[1] = null;
		}
		
		if (!isset($args[2]))
		{
			$args[2] = null;
		}
		
		return call_user_func_array(['Joomla\CMS\HTML\HTMLHelper', 'date'], $args);
	}
	
	public function _time($string, \Mustache_LambdaHelper $helper)
	{
		$string = $helper->render($string);
		
		if (empty($string))
		{
			return '';
		}
		
		if (strpos($string, ':') !== false)
		{
			return $string;
		}
		
		$time = '';
		
		if (isset($string[0]) && isset($string[1]))
		{
			$time .= $string[0] . $string[1];
		}
		
		if (isset($string[2]) && isset($string[3]))
		{
			$time .= ':' . $string[2] . $string[3];
		}
		
		if (isset($string[4]) && isset($string[5]))
		{
			$time .= ':' . $string[4] . $string[5];
		}
		
		return $time;
	}
}
