<?php
namespace Engine\Logger\Context;

abstract class GeneralAbstract
{
	protected $value = null;

	public function __construct($value)
	{
		$this->value = $value;
	}

	/**
	 * Get the value.
	 *
	 * @return mixed The value.
	 */
	public function getValue()
	{
		return $this->value;
	}

	abstract public function render($logRow=null, $key=null);


	/**
	 * Check if the variable contains binary content
	 * @return boolean
	 */
	protected function isBinary($value)
	{
		// @see https://stackoverflow.com/questions/25343508/detect-if-string-is-binary

		// v1: this works pretty well, ,but it will mark utf8 strings as binary
		// if (preg_match('/([\x00-\x20\x80-\xff])/', $value)) {
		// 	return true;
		// }
		// return false;

		// v2: this is a much better solution
		return !mb_check_encoding($value, 'UTF-8');
	}

	/**
	 * Generate a download link for a file
	 * @param string $content 
	 * @param string $fileName 
	 * @param string $fileExtension 
	 * @return string 
	 */
	protected function generateDownloadLink($content, $fileName, $fileExtension)
	{
		if (strlen($content)>(4*1024*1024)) {	// gzip the content if it's too big
			$content = gzencode($content, 9);
			$fileExtension = $fileExtension.'.gz';
		}

		$downloadName = basename($fileName) . '.' . $fileExtension;
		return '<a href="data:application/octet-stream;base64,' . base64_encode($content) . '" download="' . $downloadName . '" title="' . $downloadName . '">⬇️ download ' . $downloadName . '</a>';
	}

}
