Created
March 28, 2021 11:40
-
-
Save semihkeskindev/4db31ceae0215f7fafb6632c0d0b816f to your computer and use it in GitHub Desktop.
HTML Add Style Link tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Html | |
{ | |
const REL_STYLESHEET = 'stylesheet'; | |
const REL_PRELOAD = 'preload'; | |
/** | |
* style css link tagı return eder. | |
*/ | |
public static function addStyle(string $path, string $rel = 'stylesheet', array $extraAttributes = []): string | |
{ | |
$formattedPath = addslashes($path); | |
$html = "<link rel=\"{$rel}\" href=\"{$formattedPath}\""; | |
if (!empty($extraAttributes)) { | |
foreach ($extraAttributes as $key => $extraAttribute) { | |
if (!is_string($key) || blank($key)) { | |
[$attributeKey, $attributeValue] = [$extraAttribute, null]; | |
} else { | |
[$attributeKey, $attributeValue] = [$key, $extraAttribute]; | |
} | |
$html .= " {$attributeKey}"; | |
if (!is_null($attributeValue)) { | |
$html .= "=\"{$attributeValue}\""; | |
} | |
} | |
} | |
$html .= '>'; | |
return $html; | |
} | |
/** | |
* Browser uyumluluğuna göre preload style tagı döner. | |
*/ | |
public static function addPreloadStyle(string $path, array $extraAttributes = []): string | |
{ | |
$rel = self::REL_PRELOAD; | |
if ('Firefox' === \Agent::browser()) { | |
$rel = self::REL_STYLESHEET; | |
} else { | |
$extraAttributes['onload'] = 'this.onload=null;this.rel=\'stylesheet\''; | |
} | |
return self::addStyle($path, $rel, $extraAttributes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment