Created
October 12, 2021 15:33
-
-
Save JoshuaDoshua/49fafef2eaf1ac98b1ec8b91e004c398 to your computer and use it in GitHub Desktop.
PHP Helpers
This file contains 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 | |
/** | |
* Pull and remove an element from an array | |
* | |
* @param array $array | |
* @param mixed $key | |
* @param mixed $default | |
* @return mixed | |
*/ | |
if (!function_exists('arrayPull')): | |
function arrayPull(array &$array, $key, $default = null) { | |
$value = $array[$key] ?? $default; | |
unset($array[$key]); | |
return $value; | |
} | |
endif; // arrayPull |
This file contains 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 | |
/** | |
* Quick and dirty html attributes from an array | |
* | |
* @param array $atts att => value or int => att | |
* @return string | |
*/ | |
if (!function_exists('arrayToAtts')): | |
function arrayToAtts(array $atts = []) { | |
$str = ""; | |
foreach ($atts as $key => $val) { | |
if (!is_int($key) && $val) | |
$str .= " {$key}=\"{$val}\""; | |
elseif (is_int($key)) | |
$str .= " {$val}"; | |
} | |
return $str; | |
} | |
endif; // arrayToAtts |
This file contains 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 | |
/** | |
* Convert array of strings to a comma separated string | |
* | |
* @param array $list | |
* @param string $and | |
* @param int $max | |
* @param string $ellipsis | |
* @return string | |
*/ | |
if (!function_exists('arrayToCsv')): | |
function arrayToCsv(array $list, $and = "&", $max = -1, $ellipsis = " ...") { | |
$n = count($list); | |
if ($n === 1) | |
return $list[0]; | |
elseif ($n === 2 && $and) | |
return "{$list[0]} {$and} {$list[1]}"; | |
if ($max > 0 && $n > $max) | |
return implode(", ", array_slice($list, 0, $max)) . $ellipsis; | |
if ($and) | |
$list[$n-1] = " {$and} {$list[$n-1]}"; | |
else | |
$list[$n-1] = $list[$n-1]; | |
return implode(", ", $list); | |
} | |
endif; // arrayToCsv |
This file contains 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 | |
/** | |
* BEM css class generator | |
* | |
* @param string $block | |
* @param string $el | |
* @param mixed $mods string or array | |
* @param bool $string output as string (or array) | |
* @return mixed | |
*/ | |
if (!function_exists('bem')): | |
function bem($block, $el = null, $mods = null, $string = true) { | |
$classes = []; | |
$classes[] = $el ? "{$block}__{$el}" : "{$block}"; | |
if ($mods && is_array($mods)): | |
foreach ($mods as $mod): | |
if (!$mod) continue; | |
$classes[] = $el ? "{$block}__{$el}--{$mod}" : "{$block}--{$mod}"; | |
endforeach; // mods | |
elseif ($mods): | |
$classes[] = $el ? "{$block}__{$el}--{$mods}" : "{$block}--{$mods}"; | |
endif; // is mods array | |
return $string ? implode(" ", $classes) : $classes; | |
} | |
endif; // bem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment