Taitava/silverstripe-cloakemail

Refactor CloakingModes

Opened this issue · 0 comments

For now, different cloaking modes are just methods in a class called CloakingModes. While it kind of works as the cloaking modes are not very complex, it prevents developers from creating custom cloaking modes without forking this repository.

This is how the cloaking modes are currently defined:

<?php
namespace Taitava\CloakEmail;
class CloakingModes
{
	public static function none($value)
	{
		return $value;
	}
	
	public static function nojs($value, $options)
	{
		$address = str_replace(array('.', '@'), array($options['dot'], $options['at']), $value);
		if ($options['insert_link']) return "<a href=\"mailto:$address\">$address</a>";
		return $address;
	}
	
	public static function simple($value, $options)
	{
		CloakEmail::RequireJavaScript($options);
		$address = str_replace(array('.', '@'), array($options['dot'], $options['at']), $value);
		if ($options['insert_link']) $address = "<a href=\"mailto:$address\">$address</a>";
		return '<span class="simple-cloak">' . $address . '</span>';
	}
	
	public static function hard($value, $options)
	{
		CloakEmail::RequireJavaScript($options);
		$noscript = '<noscript>' . $options['hard_noscript_error'] . '</noscript>';
		$chars = array();
		for ($i = 0; $i < strlen($value); $i++)
		{
			$char = $value[$i];
			$chars[] = ord($char); //Convert the character to a decimal number
		}
		$insert_link = $options['insert_link'] ? ' insert-link' : '';
		return "$noscript<span class=\"hard-cloak$insert_link\" style=\"display: none;\">" . implode('-', $chars) . "</span>";
	}
}

I'd like to change that so that there would be a CloakModeInterface interface and all modes would be classes that implement that interface. Something like this:

interface CloakModeInterface
{
    public function cloak($value, array $options);
}

class CloakModeNone implements CloakModeInterface {};
class CloakModeNoJS implements CloakModeInterface {};
class CloakModeSimple implements CloakModeInterface {};
class CloakModeHard implements CloakModeInterface {};

Cloak modes are currently referenced as 'none', 'nojs', 'simple' and 'hard' in YAML configuration files. From now on they would be referenced by fully qualified class names instead. For backwards compatibility, there could be an automatic function that would translate i.e. 'simple' to CloakModeSimple::class. But config examples in the README.md file should use the class name style.