42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
namespace ContingentParser\Http;
|
|
|
|
final class UrlBuilder
|
|
{
|
|
public function __construct() {}
|
|
/**
|
|
* Строит валидный URL сайта
|
|
* @param string $url Изначальный URL
|
|
* @return string
|
|
*/
|
|
public function build(string $url): string
|
|
{
|
|
// Строит -> https://<base_uri>
|
|
$url = trim(strtolower($url));
|
|
$url = preg_replace('/\s+/', '', $url);
|
|
$url = str_replace("www/", "www.", $url);
|
|
$url = str_replace("http:\\\\", "", $url);
|
|
if (!preg_match('#^https?://#', $url)) {
|
|
$url = "https://$url";
|
|
}
|
|
$url = str_replace("http://", "https://", $url);
|
|
$arr = parse_url($url);
|
|
$url = $arr['scheme'] . '://' . $arr['host'] . '/';
|
|
$url = str_replace("www.", "", $url);
|
|
$url = str_replace("_", "/", $url);
|
|
|
|
return trim($url);
|
|
}
|
|
|
|
public function checkUri(string $uri): bool
|
|
{
|
|
if (str_ends_with($uri, ".pdf")
|
|
|| str_ends_with($uri, ".docx")
|
|
|| str_ends_with($uri, ".doc")
|
|
|| str_starts_with($uri, "javascript")
|
|
) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
} |