| 1 |
<?php |
| 2 |
namespace Depicter\Document\Models\Common; |
| 3 |
|
| 4 |
|
| 5 |
use Depicter\Document\CSS\Breakpoints; |
| 6 |
use Depicter\Document\Models\Traits\HasDataSheetTrait; |
| 7 |
|
| 8 |
class Styles |
| 9 |
{ |
| 10 |
use HasDataSheetTrait; |
| 11 |
|
| 12 |
/** |
| 13 |
* @var Styles\Transform |
| 14 |
*/ |
| 15 |
public $transform; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var Styles\Opacity |
| 19 |
*/ |
| 20 |
public $opacity; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var Styles\BoxShadow |
| 24 |
*/ |
| 25 |
public $boxShadow; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var Styles\BackgroundBlur |
| 29 |
*/ |
| 30 |
public $backgroundBlur; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var Styles\BackgroundColor |
| 34 |
*/ |
| 35 |
public $backgroundColor; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var Styles\Border |
| 39 |
*/ |
| 40 |
public $border; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var Styles\Corner |
| 44 |
*/ |
| 45 |
public $corner; |
| 46 |
|
| 47 |
/** |
| 48 |
* @var Styles\Filter |
| 49 |
*/ |
| 50 |
public $filter; |
| 51 |
|
| 52 |
/** |
| 53 |
* @var Styles\TextShadow |
| 54 |
*/ |
| 55 |
public $textShadow; |
| 56 |
|
| 57 |
/** |
| 58 |
* @var Styles\Margin |
| 59 |
*/ |
| 60 |
public $margin; |
| 61 |
|
| 62 |
/** |
| 63 |
* @var Styles\Padding |
| 64 |
*/ |
| 65 |
public $padding; |
| 66 |
|
| 67 |
/** |
| 68 |
* @var Styles\BlendingMode |
| 69 |
*/ |
| 70 |
public $blendingMode; |
| 71 |
|
| 72 |
/** |
| 73 |
* @var Styles\Typography |
| 74 |
*/ |
| 75 |
public $typography; |
| 76 |
|
| 77 |
/** |
| 78 |
* @var Styles\Transition |
| 79 |
*/ |
| 80 |
public $transition; |
| 81 |
|
| 82 |
/** |
| 83 |
* @var Styles\Svg |
| 84 |
*/ |
| 85 |
public $svg; |
| 86 |
|
| 87 |
/** |
| 88 |
* @var Styles\Hover |
| 89 |
*/ |
| 90 |
public $hover; |
| 91 |
|
| 92 |
|
| 93 |
/** |
| 94 |
* Retrieves list of fonts used in typography options |
| 95 |
* |
| 96 |
* @return array |
| 97 |
*/ |
| 98 |
public function getFontsList() |
| 99 |
{ |
| 100 |
if( !empty( $this->typography ) ){ |
| 101 |
return $this->typography->getFontsList(); |
| 102 |
} |
| 103 |
|
| 104 |
return []; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Retrieves styles for all available modules |
| 109 |
* |
| 110 |
* @param array $states The states to generate style for |
| 111 |
* |
| 112 |
* @return array |
| 113 |
*/ |
| 114 |
public function getGeneralCss( $states = 'all' ) { |
| 115 |
$cssModules = [ |
| 116 |
'filter', |
| 117 |
'textShadow', |
| 118 |
'opacity', |
| 119 |
'padding', |
| 120 |
'typography', |
| 121 |
'boxShadow', |
| 122 |
'transform', |
| 123 |
'backgroundBlur', |
| 124 |
'backgroundColor', |
| 125 |
'border', |
| 126 |
'corner', |
| 127 |
'margin' |
| 128 |
]; |
| 129 |
|
| 130 |
$stylesList = $this->generateCssForModules( $cssModules, $states ); |
| 131 |
$stylesList = $this->replaceDynamicTags( $stylesList ); |
| 132 |
|
| 133 |
return $stylesList; |
| 134 |
} |
| 135 |
|
| 136 |
protected function replaceDynamicTags( $stylesList ){ |
| 137 |
if( ! $dataSheet = $this->getDataSheet() ){ |
| 138 |
return $stylesList; |
| 139 |
} |
| 140 |
|
| 141 |
foreach( $stylesList as $key => $value ){ |
| 142 |
if( empty( $value) ){ |
| 143 |
continue; |
| 144 |
} |
| 145 |
if( is_string( $value ) ){ |
| 146 |
$stylesList[ $key ] = \Depicter::dataSource()->tagsManager()->convert( $value, $dataSheet ); |
| 147 |
} elseif( is_array( $value ) ) { |
| 148 |
$stylesList[ $key ] = $this->replaceDynamicTags( $value ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return $stylesList; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Get styles for SVG |
| 157 |
* |
| 158 |
* @return array |
| 159 |
*/ |
| 160 |
public function getSvgCss() { |
| 161 |
return $this->generateCssForModules( [ 'svg' ] ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Get transition CSS |
| 166 |
* |
| 167 |
* @return array |
| 168 |
*/ |
| 169 |
public function getTransitionCss() { |
| 170 |
$css = $this->getInitialCssVariable(); |
| 171 |
/** |
| 172 |
* While transition is defined in 'hover' property, we need to consider an exception to generate |
| 173 |
* transition style for normal state not :hover |
| 174 |
**/ |
| 175 |
$css = !empty( $this->hover->transition ) ? $this->hover->transition->set( $css ) : $css; |
| 176 |
|
| 177 |
return $css; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Collects and retrieves responsive styles from css modules |
| 182 |
* |
| 183 |
* @param array|string $cssModules |
| 184 |
* @param string $states The states to generate style for |
| 185 |
* |
| 186 |
* @return array |
| 187 |
*/ |
| 188 |
protected function generateCssForModules( $cssModules = [], $states = 'all' ){ |
| 189 |
$knownStates = ['normal', 'hover']; |
| 190 |
|
| 191 |
// translate $states if string or null passed |
| 192 |
if( is_string( $states ) ){ |
| 193 |
if( $states === 'all' ){ |
| 194 |
$states = $knownStates; |
| 195 |
} elseif ( in_array( $states, $knownStates ) ){ |
| 196 |
$states = (array) $states; |
| 197 |
} |
| 198 |
} |
| 199 |
if( is_null( $states ) ){ |
| 200 |
$states = $knownStates; |
| 201 |
} |
| 202 |
|
| 203 |
$css = $this->getInitialCssVariable(); |
| 204 |
|
| 205 |
// Collect styles from modules |
| 206 |
foreach ( $cssModules as $cssModule ) { |
| 207 |
if( in_array( 'normal', $states ) ){ |
| 208 |
$css = ! empty( $this->{$cssModule} ) ? $this->{$cssModule}->set( $css ) : $css; |
| 209 |
} |
| 210 |
if( in_array( 'hover', $states ) ){ |
| 211 |
$css['hover'] = !empty( $this->hover->{$cssModule} ) ? $this->hover->{$cssModule}->set( $css['hover'] ) : $css['hover']; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
// check if hover style is disabled for one breakpoint then remove all the css modules for that breakpoint |
| 216 |
if ( in_array( 'hover', $states ) ) { |
| 217 |
$devices = Breakpoints::names(); |
| 218 |
foreach ( $devices as $device ) { |
| 219 |
if ( empty( $this->hover->enable->{$device} ) ) { |
| 220 |
$css['hover'][ $device ] = []; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
return $css; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Retrieves a structured list for normal and hover states and breakpoints |
| 230 |
* |
| 231 |
* @return array |
| 232 |
*/ |
| 233 |
protected function getInitialCssVariable(){ |
| 234 |
$css = []; |
| 235 |
$devices = Breakpoints::names(); |
| 236 |
foreach ( $devices as $device ) { |
| 237 |
$css[ $device ] = []; |
| 238 |
} |
| 239 |
$css['hover'] = $css; |
| 240 |
|
| 241 |
return $css; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Get blending mode styles |
| 246 |
* |
| 247 |
* @return array |
| 248 |
*/ |
| 249 |
public function getBlendingModeStyle(): array{ |
| 250 |
return $this->generateCssForModules(['blendingMode']); |
| 251 |
} |
| 252 |
|
| 253 |
} |
| 254 |
|