Generics
1 year ago
Props
1 year 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
StyleParser.php
148 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core\StyleManager; |
| 4 | |
| 5 | use IlluminateAgnostic\Arr\Support\Arr; |
| 6 | use Kubio\Core\LodashBasic; |
| 7 | use Kubio\Core\StyleManager\Props\Animation; |
| 8 | use Kubio\Core\StyleManager\Props\Background; |
| 9 | use Kubio\Core\StyleManager\Props\Border; |
| 10 | use Kubio\Core\StyleManager\Props\BoxShadow; |
| 11 | use Kubio\Core\StyleManager\Props\ColumnWidth; |
| 12 | use Kubio\Core\StyleManager\Props\CustomHeight; |
| 13 | use Kubio\Core\StyleManager\Props\Height; |
| 14 | use Kubio\Core\StyleManager\Props\MultipleImage; |
| 15 | use Kubio\Core\StyleManager\Props\Opacity; |
| 16 | use Kubio\Core\StyleManager\Props\Size; |
| 17 | use Kubio\Core\StyleManager\Props\Stroke; |
| 18 | use Kubio\Core\StyleManager\Props\TBLR; |
| 19 | use Kubio\Core\StyleManager\Props\TextShadow; |
| 20 | use Kubio\Core\StyleManager\Props\Transform; |
| 21 | use Kubio\Core\StyleManager\Props\Transition; |
| 22 | use Kubio\Core\StyleManager\Props\Typography; |
| 23 | use Kubio\Core\StyleManager\Props\Width; |
| 24 | use Kubio\Core\StyleManager\Props\Gap; |
| 25 | use Kubio\Core\StyleManager\Props\JustifyContent; |
| 26 | use Kubio\Core\StyleManager\Props\UnitValuePercentage; |
| 27 | use Kubio\Core\StyleManager\Props\UnitValuePx; |
| 28 | use Kubio\Core\StyleManager\Props\ObjectCss; |
| 29 | use Kubio\Core\StyleManager\Props\MaxWidth; |
| 30 | |
| 31 | class StyleParser { |
| 32 | |
| 33 | public $groups; |
| 34 | public static $instance = null; |
| 35 | |
| 36 | protected function __construct() { |
| 37 | $properties = array( |
| 38 | new Background( 'background' ), |
| 39 | new TBLR( 'padding' ), |
| 40 | new TBLR( 'margin' ), |
| 41 | new ColumnWidth( 'columnWidth' ), |
| 42 | new CustomHeight( 'customHeight' ), |
| 43 | new BoxShadow( 'boxShadow' ), |
| 44 | new TextShadow( 'textShadow' ), |
| 45 | new Border( 'border' ), |
| 46 | new Typography( 'typography' ), |
| 47 | new Size( 'size' ), |
| 48 | new Transform( 'transform' ), |
| 49 | new Opacity( 'opacity' ), |
| 50 | new Gap( 'gap' ), |
| 51 | new JustifyContent( 'justifyContent' ), |
| 52 | new Width( 'width' ), |
| 53 | new Height( 'height' ), |
| 54 | new Stroke( 'stroke' ), |
| 55 | new MultipleImage( 'multipleImage' ), |
| 56 | new UnitValuePercentage( 'top' ), |
| 57 | new UnitValuePercentage( 'right' ), |
| 58 | new UnitValuePercentage( 'bottom' ), |
| 59 | new UnitValuePercentage( 'left' ), |
| 60 | new MaxWidth( 'maxWidth' ), |
| 61 | new UnitValuePx( 'maxHeight' ), |
| 62 | new UnitValuePx( 'minHeight' ), |
| 63 | new ObjectCss( 'object' ), |
| 64 | new Animation( 'animation' ), |
| 65 | new Transition( 'transition' ), |
| 66 | ); |
| 67 | |
| 68 | $this->groups = array(); |
| 69 | foreach ( $properties as $property ) { |
| 70 | $this->addProperty( $property->name, $property ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public static function getInstance() { |
| 75 | if ( ! self::$instance ) { |
| 76 | self::$instance = new StyleParser(); |
| 77 | } |
| 78 | return self::$instance; |
| 79 | } |
| 80 | |
| 81 | public function evaluateString( $value ) { |
| 82 | return $value; |
| 83 | } |
| 84 | |
| 85 | public function addProperty( $name, $property ) { |
| 86 | $this->groups[ $name ] = $property; |
| 87 | } |
| 88 | |
| 89 | public function evaluate( $value ) { |
| 90 | if ( LodashBasic::isString( $value ) ) { |
| 91 | return $this->evaluateString( $value ); |
| 92 | } |
| 93 | if ( is_array( $value ) ) { |
| 94 | foreach ( $value as $name => $val ) { |
| 95 | $value[ $name ] = $this->evaluate( $val ); |
| 96 | } |
| 97 | } |
| 98 | return $value; |
| 99 | } |
| 100 | |
| 101 | public function transform( $obj, $context, $skip_normal = false ) { |
| 102 | if ( ! $obj ) { |
| 103 | return; |
| 104 | } |
| 105 | $css = array(); |
| 106 | $obj = $this->filterProps( $obj ); |
| 107 | foreach ( array_keys( $obj ) as $prop ) { |
| 108 | if ( isset( $this->groups[ $prop ] ) ) { |
| 109 | $new_props = $this->groups[ $prop ]->parse( $obj[ $prop ], $context ); |
| 110 | $new_props = $this->evaluate( $new_props ); |
| 111 | $css = LodashBasic::merge( $css, (array) $new_props ); |
| 112 | } else { |
| 113 | |
| 114 | if ( strpos( $prop, '--' ) === 0 ) { |
| 115 | $css[ $prop ] = ParserUtils::toValueUnitString( $obj[ $prop ] ); |
| 116 | } else { |
| 117 | if ( ! $skip_normal ) { |
| 118 | if ( is_numeric( $obj[ $prop ] ) || is_string( $obj[ $prop ] ) ) { |
| 119 | $css[ $prop ] = $this->evaluate( $obj[ $prop ] ); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | return $css; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | public function filterProps( $obj ) { |
| 130 | $props_to_unset = array(); |
| 131 | if ( isset( $obj['size'] ) ) { |
| 132 | $props_to_unset = array_merge( |
| 133 | $props_to_unset, |
| 134 | array( |
| 135 | 'width', |
| 136 | 'height', |
| 137 | 'minWidth', |
| 138 | 'minHeight', |
| 139 | ) |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | Arr::forget( $obj, $props_to_unset ); |
| 144 | |
| 145 | return $obj; |
| 146 | } |
| 147 | } |
| 148 |