Generics
1 year ago
Props
1 month ago
BlockStyleRender.php
1 year ago
DynamicStyles.php
1 year ago
GlobalStyleRender.php
1 year ago
MainAttribute.php
4 years ago
ParserUtils.php
1 year ago
StyleManager.php
2 years ago
StyleParser.php
1 year ago
StyleRender.php
1 year ago
Utils.php
1 year ago
ParserUtils.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Kubio\Core\StyleManager; |
| 5 | |
| 6 | |
| 7 | use Kubio\Core\LodashBasic; |
| 8 | use IlluminateAgnostic\Arr\Support\Arr; |
| 9 | use function implode; |
| 10 | use function is_array; |
| 11 | use function is_string; |
| 12 | |
| 13 | class ParserUtils { |
| 14 | static function areAllNonEmpty( $values ) { |
| 15 | $found = LodashBasic::find( |
| 16 | $values, |
| 17 | function ( $item ) { |
| 18 | return ! self::isNotEmptyButCanBeZero( $item ); |
| 19 | } |
| 20 | ); |
| 21 | |
| 22 | return ! $found; |
| 23 | } |
| 24 | |
| 25 | public static function isNotEmptyButCanBeZero( $value ) { |
| 26 | return $value !== '' && $value !== null && $value !== 'undefined' && $value !== false; |
| 27 | } |
| 28 | |
| 29 | public static function joinNonEmpty( $values, $join = ' ' ) { |
| 30 | $nonEmpty = LodashBasic::filter( |
| 31 | $values, |
| 32 | function ( $item ) { |
| 33 | return self::isNotEmptyButCanBeZero( $item ); |
| 34 | } |
| 35 | ); |
| 36 | |
| 37 | return implode( $join, $nonEmpty ); |
| 38 | } |
| 39 | |
| 40 | public static function toValueUnitStringFunction( |
| 41 | $functionName, |
| 42 | $valueUnit, |
| 43 | $defaultValue = '', |
| 44 | $defaultUnit = '', |
| 45 | $isUnitLess = false |
| 46 | ) { |
| 47 | $value = self::toValueUnitString( $valueUnit, null, $defaultUnit, $isUnitLess ); |
| 48 | if ( $value ) { |
| 49 | return "$functionName($value)"; |
| 50 | } |
| 51 | |
| 52 | return $defaultValue; |
| 53 | } |
| 54 | |
| 55 | public static function toValueUnitString( |
| 56 | $value_unit, |
| 57 | $defaultValue = '', |
| 58 | $defaultUnit = '', |
| 59 | $isUnitLess = false |
| 60 | ) { |
| 61 | if ( is_string( $value_unit ) || is_numeric( $value_unit ) ) { |
| 62 | |
| 63 | if ( $value_unit && $defaultUnit ) { |
| 64 | return "{$value_unit}{$defaultUnit}"; |
| 65 | } |
| 66 | |
| 67 | return $value_unit; |
| 68 | } |
| 69 | |
| 70 | $value = Arr::get( (array) $value_unit, 'value', $defaultValue ); |
| 71 | $unit = Arr::get( (array) $value_unit, 'unit', $defaultUnit ); |
| 72 | $importantStr = isset( $value_unit['important'] ) && $value_unit['important'] ? ' !important' : ''; |
| 73 | if ( |
| 74 | self::isNotEmptyButCanBeZero( $value ) && |
| 75 | ( $defaultUnit || self::isNotEmptyButCanBeZero( $unit ) || $isUnitLess ) |
| 76 | ) { |
| 77 | |
| 78 | // in colmun spacing custom there seems to be an issue where the unit is sent as array of label, value |
| 79 | if ( is_array( $unit ) && isset( $unit['value'] ) ) { |
| 80 | $unit = $unit['value']; |
| 81 | } |
| 82 | |
| 83 | return "{$value}{$unit}{$importantStr}"; |
| 84 | } |
| 85 | |
| 86 | return $defaultValue; |
| 87 | } |
| 88 | |
| 89 | public static function toJoinedValueUnitString( $values, $glue = ' ' ) { |
| 90 | $vals = array(); |
| 91 | foreach ( $values as $value ) { |
| 92 | $vals[] = self::toValueUnitString( $value ); |
| 93 | } |
| 94 | |
| 95 | return join( $glue, $vals ); |
| 96 | } |
| 97 | |
| 98 | public static function addValueUnitString( &$style, $key, $obj ) { |
| 99 | $value = self::toValueUnitString( $obj ); |
| 100 | if ( $value ) { |
| 101 | $style[ $key ] = $value; |
| 102 | } |
| 103 | |
| 104 | return $style; |
| 105 | } |
| 106 | |
| 107 | public static function addPrimitiveValues( &$style, $value, $propertiesMap, $unitLessProperties = array() ) { |
| 108 | LodashBasic::each( |
| 109 | $propertiesMap, |
| 110 | function ( $cssName, $jsonName ) use ( &$style, $value, $unitLessProperties ) { |
| 111 | if ( isset( $value[ $jsonName ] ) && self::isNotEmptyButCanBeZero( $value[ $jsonName ] ) ) { |
| 112 | $isUnitLess = in_array( $jsonName, $unitLessProperties ); |
| 113 | $propertyValue = self::toPrimitiveValue( $value[ $jsonName ], $isUnitLess ); |
| 114 | if ( self::isNotEmptyButCanBeZero( $propertyValue ) ) { |
| 115 | $style[ $cssName ] = $propertyValue; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | ); |
| 120 | |
| 121 | return $style; |
| 122 | } |
| 123 | |
| 124 | public static function toPrimitiveValue( $value, $isUnitLess ) { |
| 125 | if ( is_array( $value ) ) { |
| 126 | if ( |
| 127 | isset( $value['value'] ) || |
| 128 | isset( $value['unit'] ) |
| 129 | ) { |
| 130 | return self::toValueUnitString( $value, '', '', $isUnitLess ); |
| 131 | } |
| 132 | } else { |
| 133 | return $value; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 |