| 1 |
<?php |
| 2 |
namespace Depicter\Document\Models\Common; |
| 3 |
|
| 4 |
|
| 5 |
use Depicter\Document\CSS\Breakpoints; |
| 6 |
use Depicter\Document\Helper\Helper; |
| 7 |
use Depicter\Document\Models\Common\Styles\Transition; |
| 8 |
use Depicter\Document\Models\Traits\HasDataSheetTrait; |
| 9 |
|
| 10 |
class Styles |
| 11 |
{ |
| 12 |
use HasDataSheetTrait; |
| 13 |
|
| 14 |
/** |
| 15 |
* @var Styles\Transform |
| 16 |
*/ |
| 17 |
public $transform; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var Styles\Opacity |
| 21 |
*/ |
| 22 |
public $opacity; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var Styles\BoxShadow |
| 26 |
*/ |
| 27 |
public $boxShadow; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var Styles\BackgroundBlur |
| 31 |
*/ |
| 32 |
public $backgroundBlur; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var Styles\BackgroundColor |
| 36 |
*/ |
| 37 |
public $backgroundColor; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var Styles\Border |
| 41 |
*/ |
| 42 |
public $border; |
| 43 |
|
| 44 |
/** |
| 45 |
* @var Styles\Corner |
| 46 |
*/ |
| 47 |
public $corner; |
| 48 |
|
| 49 |
/** |
| 50 |
* @var Styles\Filter |
| 51 |
*/ |
| 52 |
public $filter; |
| 53 |
|
| 54 |
/** |
| 55 |
* @var Styles\TextShadow |
| 56 |
*/ |
| 57 |
public $textShadow; |
| 58 |
|
| 59 |
/** |
| 60 |
* @var Styles\Margin |
| 61 |
*/ |
| 62 |
public $margin; |
| 63 |
|
| 64 |
/** |
| 65 |
* @var Styles\Padding |
| 66 |
*/ |
| 67 |
public $padding; |
| 68 |
|
| 69 |
/** |
| 70 |
* @var Styles\BlendingMode |
| 71 |
*/ |
| 72 |
public $blendingMode; |
| 73 |
|
| 74 |
/** |
| 75 |
* @var Styles\Typography |
| 76 |
*/ |
| 77 |
public $typography; |
| 78 |
|
| 79 |
/** |
| 80 |
* @var Styles\Transition |
| 81 |
*/ |
| 82 |
public $transition; |
| 83 |
|
| 84 |
/** |
| 85 |
* @var Styles\Svg |
| 86 |
*/ |
| 87 |
public $svg; |
| 88 |
|
| 89 |
/** |
| 90 |
* @var Styles\Hover |
| 91 |
*/ |
| 92 |
public $hover; |
| 93 |
|
| 94 |
/** |
| 95 |
* @var Styles\Flex |
| 96 |
*/ |
| 97 |
public $flex; |
| 98 |
|
| 99 |
/** |
| 100 |
* @var Styles\Width |
| 101 |
*/ |
| 102 |
public $width; |
| 103 |
|
| 104 |
/** |
| 105 |
* @var Styles\Height |
| 106 |
*/ |
| 107 |
public $height; |
| 108 |
|
| 109 |
|
| 110 |
/** |
| 111 |
* Retrieves list of fonts used in typography options |
| 112 |
* |
| 113 |
* @return array |
| 114 |
*/ |
| 115 |
public function getFontsList() |
| 116 |
{ |
| 117 |
if( !empty( $this->typography ) ){ |
| 118 |
return $this->typography->getFontsList(); |
| 119 |
} |
| 120 |
|
| 121 |
return []; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Retrieves styles for all available modules |
| 126 |
* |
| 127 |
* @param array $states The states to generate style for |
| 128 |
* |
| 129 |
* @return array |
| 130 |
*/ |
| 131 |
public function getGeneralCss( $states = 'all' ) { |
| 132 |
$cssModules = [ |
| 133 |
'width', |
| 134 |
'height', |
| 135 |
'filter', |
| 136 |
'textShadow', |
| 137 |
'opacity', |
| 138 |
'padding', |
| 139 |
'typography', |
| 140 |
'boxShadow', |
| 141 |
'transform', |
| 142 |
'backgroundBlur', |
| 143 |
'backgroundColor', |
| 144 |
'border', |
| 145 |
'corner', |
| 146 |
'margin', |
| 147 |
'flex' |
| 148 |
]; |
| 149 |
|
| 150 |
$stylesList = $this->generateCssForModules( $cssModules, $states ); |
| 151 |
$stylesList = $this->replaceDynamicTags( $stylesList ); |
| 152 |
|
| 153 |
return $stylesList; |
| 154 |
} |
| 155 |
|
| 156 |
protected function replaceDynamicTags( $stylesList ){ |
| 157 |
if( ! $dataSheet = $this->getDataSheet() ){ |
| 158 |
return $stylesList; |
| 159 |
} |
| 160 |
|
| 161 |
foreach( $stylesList as $key => $value ){ |
| 162 |
if( empty( $value) ){ |
| 163 |
continue; |
| 164 |
} |
| 165 |
if( is_string( $value ) ){ |
| 166 |
$stylesList[ $key ] = \Depicter::dataSource()->tagsManager()->convert( $value, $dataSheet ); |
| 167 |
} elseif( is_array( $value ) ) { |
| 168 |
$stylesList[ $key ] = $this->replaceDynamicTags( $value ); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return $stylesList; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Get styles for SVG |
| 177 |
* |
| 178 |
* @return array |
| 179 |
*/ |
| 180 |
public function getSvgCss() { |
| 181 |
return $this->generateCssForModules( [ 'svg' ] ); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Get transition CSS |
| 186 |
* |
| 187 |
* @return array |
| 188 |
*/ |
| 189 |
public function getTransitionCss() { |
| 190 |
if ( empty( $this->hover ) ) { |
| 191 |
return []; |
| 192 |
} |
| 193 |
|
| 194 |
$css = $this->getInitialCssVariable(); |
| 195 |
/** |
| 196 |
* While transition is defined in 'hover' property, we need to consider an exception to generate |
| 197 |
* transition style for normal state not :hover |
| 198 |
**/ |
| 199 |
$this->hover->transition = !empty( $this->hover->transition ) ? $this->hover->transition : new Transition(); |
| 200 |
$this->hover->transition->setHoverStatus( $this->hover->enable ?? [] ); |
| 201 |
|
| 202 |
return $this->hover->transition->set( $css ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Collects and retrieves responsive styles from css modules |
| 207 |
* |
| 208 |
* @param array|string $cssModules |
| 209 |
* @param string $states The states to generate style for |
| 210 |
* |
| 211 |
* @return array |
| 212 |
*/ |
| 213 |
protected function generateCssForModules( $cssModules = [], $states = 'all' ){ |
| 214 |
$knownStates = ['normal', 'hover']; |
| 215 |
|
| 216 |
// translate $states if string or null passed |
| 217 |
if( is_string( $states ) ){ |
| 218 |
if( $states === 'all' ){ |
| 219 |
$states = $knownStates; |
| 220 |
} elseif ( in_array( $states, $knownStates ) ){ |
| 221 |
$states = (array) $states; |
| 222 |
} |
| 223 |
} |
| 224 |
if( is_null( $states ) ){ |
| 225 |
$states = $knownStates; |
| 226 |
} |
| 227 |
|
| 228 |
$css = $this->getInitialCssVariable(); |
| 229 |
$css['hover'] = $css; |
| 230 |
|
| 231 |
// Collect styles from modules |
| 232 |
foreach ( $cssModules as $cssModule ) { |
| 233 |
if( in_array( 'normal', $states ) ){ |
| 234 |
$css = ! empty( $this->{$cssModule} ) ? $this->{$cssModule}->set( $css ) : $css; |
| 235 |
} |
| 236 |
if( in_array( 'hover', $states ) ){ |
| 237 |
if( !empty( $this->hover->{$cssModule} ) ){ |
| 238 |
// pass hover enabled breakpoints to the style class. The style class will be aware of normal or hover state |
| 239 |
if( method_exists( $this->hover->{$cssModule}, 'setHoverStatus' ) ){ |
| 240 |
$this->hover->{$cssModule}->setHoverStatus( $this->hover->enable ?? [] ); |
| 241 |
} |
| 242 |
$css['hover'] = $this->hover->{$cssModule}->set( $css['hover'] ); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
// check if hover style is disabled for one breakpoint then remove all the css modules for that breakpoint |
| 248 |
if ( in_array( 'hover', $states ) ) { |
| 249 |
$devices = Breakpoints::names(); |
| 250 |
foreach ( $devices as $device ) { |
| 251 |
if ( ! Helper::isHoverStyleEnabled( $this->hover, $device ) ) { |
| 252 |
$css['hover'][ $device ] = []; |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
return $css; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Collects and retrieves responsive styles from css modules for a single state |
| 262 |
* |
| 263 |
* @param array|string $cssModules |
| 264 |
* @param string $state The state to generate style for |
| 265 |
* |
| 266 |
* @return array |
| 267 |
*/ |
| 268 |
public function generateCssForModulesOfState( $cssModules = [], $state = 'normal' ){ |
| 269 |
|
| 270 |
$css = $this->getInitialCssVariable(); |
| 271 |
|
| 272 |
// Collect styles from modules |
| 273 |
foreach ( $cssModules as $cssModule ) { |
| 274 |
if( 'normal' === $state ){ |
| 275 |
$css = ! empty( $this->{$cssModule} ) ? $this->{$cssModule}->set( $css ) : $css; |
| 276 |
|
| 277 |
} elseif( 'hover' === $state ){ |
| 278 |
if( ! empty( $this->hover->{$cssModule} ) ){ |
| 279 |
// pass hover enabled breakpoints to the style class. The style class will be aware of normal or hover state |
| 280 |
if( method_exists( $this->hover->{$cssModule}, 'setHoverStatus' ) ){ |
| 281 |
$this->hover->{$cssModule}->setHoverStatus( $this->hover->enable ?? [] ); |
| 282 |
} |
| 283 |
$css = $this->hover->{$cssModule}->set( $css ); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
// check if hover style is disabled for one breakpoint then remove all the css modules for that breakpoint |
| 289 |
if ( 'hover' === $state ) { |
| 290 |
$devices = Breakpoints::names(); |
| 291 |
|
| 292 |
foreach ( $devices as $device ) { |
| 293 |
if ( ! Helper::isHoverStyleEnabled( $this->hover, $device ) ) { |
| 294 |
$css[ $device ] = []; |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
return $css; |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Retrieves a structured list for normal and hover states and breakpoints |
| 304 |
* |
| 305 |
* @return array |
| 306 |
*/ |
| 307 |
protected function getInitialCssVariable(){ |
| 308 |
$css = []; |
| 309 |
$devices = Breakpoints::names(); |
| 310 |
foreach ( $devices as $device ) { |
| 311 |
$css[ $device ] = []; |
| 312 |
} |
| 313 |
|
| 314 |
return $css; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Get blending mode styles |
| 319 |
* |
| 320 |
* @return array |
| 321 |
*/ |
| 322 |
public function getBlendingModeStyle(): array{ |
| 323 |
return $this->generateCssForModules(['blendingMode']); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Get align self flex styles |
| 328 |
* |
| 329 |
* @return array |
| 330 |
*/ |
| 331 |
public function getFlexAlignStyle(): array{ |
| 332 |
$css = $this->generateCssForModules(['flex']); |
| 333 |
$devices = Breakpoints::names(); |
| 334 |
// We implement this functionality to prevent additional flex styles defined in component elements from being applied to the frame element when the component is a child of a group element. |
| 335 |
foreach ( $devices as $device ){ |
| 336 |
foreach( $css[ $device ] as $cssProperty => $value ){ |
| 337 |
if ( 'align-self' != $cssProperty ){ |
| 338 |
unset( $css[ $device ][ $cssProperty ] ); |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
return $css; |
| 344 |
} |
| 345 |
|
| 346 |
} |
| 347 |
|