Skip to content

Instantly share code, notes, and snippets.

@kevindees
Last active September 24, 2018 13:00
Show Gist options
  • Save kevindees/b9155199b1aa8f80f64841378509b894 to your computer and use it in GitHub Desktop.
Save kevindees/b9155199b1aa8f80f64841378509b894 to your computer and use it in GitHub Desktop.
<?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;
}
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment