class_names('hero flex align-center', [
'full-bleed' => false,
'full' => false
], 'full-bleed no-selection');
// outputs: hero flex align-center full-bleed no-selection
class_names('hero flex align-center', [
'full-bleed' => false,
'full' => true
], 'full-bleed no-selection')
// outputs: hero flex align-center full
class_names([
'button-button button-shadowed button-shadowed-light' => false,
'button-button button-shadowed button-shadowed-dark' => false,
'button-button button-plain-dark' => false,
'button-button button-plain-light' => false,
'button-with-icon' => true
], 'button-button button-plain-dark')
// outputs: button-with-icon
class_names('hero flex align-center', [
'full-bleed' => false,
'full full full full' => true
], 'full-bleed no-selection')
// outputs: hero flex align-center full
Last active
September 24, 2018 13:00
-
-
Save kevindees/b9155199b1aa8f80f64841378509b894 to your computer and use it in GitHub Desktop.
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 | |
function array_reduce_allowed_str($array) { | |
$reduced = ''; | |
array_walk($array, function($val, $key) use(&$reduced) { | |
$reduced .= $val ? " $key" : ''; | |
}); | |
$cleaned = implode(' ', array_unique(array_map('trim', explode(' ', trim($reduced))))); | |
return $cleaned; | |
} | |
function class_names($defaults, $classes = null, $failed = '') { | |
if(!$result = array_reduce_allowed_str(is_array($defaults) ? $defaults : $classes)) { | |
$result = !is_array($classes) ? $classes : $failed; | |
} | |
$defaults = !is_array($defaults) ? $defaults : ''; | |
return $defaults . ' ' . $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment