Skip to content

Instantly share code, notes, and snippets.

@semihkeskindev
Created March 28, 2021 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save semihkeskindev/4db31ceae0215f7fafb6632c0d0b816f to your computer and use it in GitHub Desktop.
Save semihkeskindev/4db31ceae0215f7fafb6632c0d0b816f to your computer and use it in GitHub Desktop.
HTML Add Style Link tag
<?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