<?php

declare(strict_types=1);

namespace mirzaev\deeproots\models\telegram\traits;

/**
 * Escape
 *
 * @package mirzaev\deeproots\models\telegram\traits
 *
 * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
 * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
 */
trait escape
{
	/**
	 * Markdown
	 *
	 * Escaping for markdown
	 *
	 * @param string $text Text
	 * @param array $exception Symbols excluded from escaping
	 *
	 * @return string Escaped text
	 */
	public static function markdown(string $text, array $exceptions = []): string
	{
		// Initializing the registry of symbols for escaping
		$from = array_diff(
			[
				'#',
				'*',
				'_',
				'=',
				'.',
				'[',
				']',
				'(',
				')',
				'-',
				'>',
				'<',
				'!',
				'`'
			],
			$exceptions
		);

		// Initializing the registry of targets for escaping
		$to = [];
		foreach ($from as $symbol) $to[] = "\\$symbol";

		// Escaping and exit (success)
		return str_replace($from, $to,	$text);
	}
}
