Utils.php
109 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Kubio\Core\Styles; |
| 5 | |
| 6 | use Kubio\Core\LodashBasic; |
| 7 | use function implode; |
| 8 | |
| 9 | use Kubio\Config; |
| 10 | use Kubio\Core\Utils as CoreUtils; |
| 11 | |
| 12 | class Utils { |
| 13 | |
| 14 | public static function composeClassesByMedia( $valuesByMedia, $prefix, $allow_empty = false ) { |
| 15 | $classes = array(); |
| 16 | foreach ( $valuesByMedia as $media => $value ) { |
| 17 | if ( $value !== null || $allow_empty ) { |
| 18 | $classes[] = self::composeClassForMedia( $media, $value, $prefix, $allow_empty ); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | return $classes; |
| 23 | } |
| 24 | |
| 25 | public static function composeClassForMedia( $media, $value, $prefix, $allow_empty = false ) { |
| 26 | if ( ! $allow_empty ) { |
| 27 | $isEmptyString = is_string( $value ) && strlen( $value ) === 0; |
| 28 | if ( $isEmptyString ) { |
| 29 | return ''; |
| 30 | } |
| 31 | } |
| 32 | $mediaPrefix = self::getMediaPrefix( $media ); |
| 33 | $values = LodashBasic::compactWithExceptions( array( $prefix, $mediaPrefix, $value ), array( '0', 0 ) ); |
| 34 | $prefixedClass = implode( '-', $values ); |
| 35 | |
| 36 | return $prefixedClass; |
| 37 | } |
| 38 | |
| 39 | public static function getMediaPrefix( $media ) { |
| 40 | return LodashBasic::get( Config::mediasById(), $media . '.' . 'gridPrefix', false ); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | public static function getPrefixedCss( $css, $prefix ) { |
| 45 | # Wipe all block comments |
| 46 | $css = preg_replace( '!/\*.*?\*/!s', '', $css ); |
| 47 | |
| 48 | $parts = explode( '}', $css ); |
| 49 | $keyframe_started = false; |
| 50 | $media_query_started = false; |
| 51 | |
| 52 | foreach ( $parts as &$part ) { |
| 53 | $part = trim( $part ); # Wht not trim immediately .. ? |
| 54 | if ( empty( $part ) ) { |
| 55 | $keyframe_started = false; |
| 56 | continue; |
| 57 | } else # This else is also required |
| 58 | { |
| 59 | $part_details = explode( '{', $part ); |
| 60 | |
| 61 | if ( strpos( $part, 'keyframes' ) !== false ) { |
| 62 | $keyframe_started = true; |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | if ( $keyframe_started ) { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | if ( substr_count( $part, '{' ) === 2 ) { |
| 71 | $mediaQuery = $part_details[0] . '{'; |
| 72 | $part_details[0] = $part_details[1]; |
| 73 | $media_query_started = true; |
| 74 | } |
| 75 | |
| 76 | $sub_parts = explode( ',', $part_details[0] ); |
| 77 | foreach ( $sub_parts as &$subPart ) { |
| 78 | if ( trim( $subPart ) === '@font-face' ) { |
| 79 | continue; |
| 80 | } else { |
| 81 | $subPart = $prefix . ' ' . trim( $subPart ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if ( substr_count( $part, '{' ) == 2 ) { |
| 86 | $part = $mediaQuery . "\n" . implode( ', ', $sub_parts ) . '{' . $part_details[2]; |
| 87 | } elseif ( empty( $part[0] ) && $media_query_started ) { |
| 88 | $media_query_started = false; |
| 89 | $part = implode( ', ', $sub_parts ) . '{' . $part_details[2] . "}\n"; //finish media query |
| 90 | } else { |
| 91 | if ( isset( $part_details[1] ) ) { # Sometimes, without this check, |
| 92 | # there is an error-notice, we don't need that.. |
| 93 | $part = implode( ', ', $sub_parts ) . '{' . $part_details[1]; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | unset( $part_details, $mediaQuery, $sub_parts ); # Kill those three .. |
| 98 | } unset( $part ); # Kill this one as well |
| 99 | } |
| 100 | |
| 101 | if ( CoreUtils::isDebug() ) { |
| 102 | return implode( "}\n", $parts ); |
| 103 | } |
| 104 | |
| 105 | # Finish with the whole new prefixed string/file in one line |
| 106 | return( preg_replace( '/\s+/', ' ', implode( '} ', $parts ) ) ); |
| 107 | } |
| 108 | } |
| 109 |