<?php
/*
 * Software Name: Eorisis Setup
 * Software URI: https://eorisis.com/shop/software/joomla/setup
 * Author: Eorisis
 * Author URI: https://eorisis.com
 * Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
 * License: GNU General Public License version 2; See https://eorisis.com/gpl
 * Terms of Service: https://eorisis.com/tos
 */
namespace eorisis_setup\framework\lib\core\model;
\defined('_JEXEC') or die;

class clean
{
	public function __construct() {}

	// --------------------------------------------------

	public static function string($string, bool $trim = true) : string
	{
		if (!$string)
		{
			return '';
		}

		$string = \preg_replace('/<\?(php)?|\?>/i', '', (string)$string);
		$string = \strip_tags($string);

		if ($trim)
		{
			$string = \trim($string);
		}

		$string = \htmlspecialchars($string, ENT_COMPAT | ENT_HTML5, 'UTF-8');

		// Allowed
		$replace = [
			'&amp;' // &
		];
		$with = [
			'&'
		];
		return \str_replace($replace, $with, $string);
	}

	// --------------------------------------------------

	public static function uri($string, bool $utf8_encode = false) : string
	{
		$string = \str_replace(["\t",' '], '', (string)$string);
		return self::sanitize_uri($string, $utf8_encode);
	}

	// --------------------------------------------------

	public static function sanitize_uri($uri, bool $utf8_encode = false) : string
	{
		$uri = \filter_var((string)$uri, FILTER_SANITIZE_URL);

		if (!$utf8_encode)
		{
			return $uri;
		}

		return \htmlspecialchars($uri, ENT_COMPAT | ENT_HTML5, 'UTF-8');
	}

	// --------------------------------------------------

	public static function auth_code($string) : string
	{
		return \preg_replace('/[^a-zA-Z0-9]+/', '', (string)$string);
	}
}
