| @@ -45,4 +45,42 @@ | ||
| 45 | 45 | */ |
| 46 | 46 | function clamp($value, $min, $max) { |
| 47 | 47 | return max($min, min($value, $max)); |
| 48 | 48 | } |
| 49 | + | |
| 50 | +/** | |
| 51 | + * Check whether a value is undefined. | |
| 52 | + * | |
| 53 | + * @param mixed $value Value to check. | |
| 54 | + * @return bool | |
| 55 | + */ | |
| 56 | +function is_undefined($value) { | |
| 57 | + return null === $value || !isset($value); | |
| 58 | +} | |
| 59 | + | |
| 60 | +/** | |
| 61 | + * Generate CSS string from styles array. | |
| 62 | + * | |
| 63 | + * @param array $styles Styles array. | |
| 64 | + * @return string | |
| 65 | + */ | |
| 66 | +function generate_css_string($styles) { | |
| 67 | + $css_string = ''; | |
| 68 | + | |
| 69 | + foreach ($styles as $key => $value) { | |
| 70 | + if ( | |
| 71 | + !is_undefined($value) && | |
| 72 | + false !== $value && | |
| 73 | + ( | |
| 74 | + !is_string($value) || | |
| 75 | + ( | |
| 76 | + trim($value) !== '' && | |
| 77 | + trim($value) !== 'undefined undefined undefined' | |
| 78 | + ) | |
| 79 | + ) | |
| 80 | + ) { | |
| 81 | + $css_string .= $key . ': ' . $value . '; '; | |
| 82 | + } | |
| 83 | + } | |
| 84 | + | |
| 85 | + return $css_string; | |
| 86 | +} | |