Separator.php
79 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core\Separators; |
| 4 | |
| 5 | use Kubio\Config; |
| 6 | use Kubio\Core\Element; |
| 7 | use Kubio\Core\LodashBasic; |
| 8 | use Kubio\Core\StyleManager\ParserUtils; |
| 9 | use Kubio\Core\Styles\Utils; |
| 10 | use function file_exists; |
| 11 | use function file_get_contents; |
| 12 | use function sanitize_file_name; |
| 13 | use const KUBIO_ROOT_DIR; |
| 14 | |
| 15 | class Separator extends Element { |
| 16 | |
| 17 | function __construct( $tag_name, $props, $children, $block ) { |
| 18 | parent::__construct( $tag_name, $props, $children, $block ); |
| 19 | |
| 20 | $position = $this->getProp( 'position' ); |
| 21 | $negative = $this->getProp( 'negative' ); |
| 22 | |
| 23 | $enabledByMedia = $this->getProp( 'enabledByMedia' ); |
| 24 | |
| 25 | $visibilityPerMedia = $this->getVisibilityPerMedia( $enabledByMedia ); |
| 26 | |
| 27 | $style = array( |
| 28 | 'fill' => $this->getProp( 'color' ), |
| 29 | 'height' => ParserUtils::toValueUnitString( $this->getProp( 'height' ) ), |
| 30 | ); |
| 31 | |
| 32 | if ( ! $this->getProp( 'overlap' ) ) { |
| 33 | $style['position'] = 'relative'; |
| 34 | } |
| 35 | |
| 36 | $style[ $position ] = 'calc(0px)'; |
| 37 | |
| 38 | $type = sanitize_file_name( $this->getProp( 'type' ) ); |
| 39 | |
| 40 | $top = $position === 'top'; |
| 41 | $shouldUseNegative = $negative && file_exists( KUBIO_ROOT_DIR . "lib/shapes/separators/${type}-negative.svg" ); |
| 42 | |
| 43 | $supportsNegative = file_exists( KUBIO_ROOT_DIR . "lib/shapes/separators/${type}-negative.svg" ); |
| 44 | |
| 45 | if ( ( $shouldUseNegative && $top ) || ( ! $shouldUseNegative && ! $top ) ) { |
| 46 | $style['transform'] = 'rotateX(180deg)'; |
| 47 | } |
| 48 | |
| 49 | if ( $negative && $supportsNegative ) { |
| 50 | $type = $type . '-negative'; |
| 51 | } |
| 52 | |
| 53 | $html = file_get_contents( KUBIO_ROOT_DIR . 'lib/shapes/separators/' . $type . '.svg' ); |
| 54 | $this->extendProps( |
| 55 | array( |
| 56 | 'className' => array_merge( array( 'h-separator' ), $visibilityPerMedia ), |
| 57 | 'style' => $style, |
| 58 | ) |
| 59 | ); |
| 60 | |
| 61 | $this->setChildren( array( $html ) ); |
| 62 | } |
| 63 | |
| 64 | public function getVisibilityPerMedia( $enabledByMedia = array() ) { |
| 65 | $classes = array(); |
| 66 | $prefix = 'h-separator--display'; |
| 67 | foreach ( $enabledByMedia as $media => $enabled ) { |
| 68 | $value = $enabled ? 'flex' : 'none'; |
| 69 | $mediaPrefix = utils::getMediaPrefix( $media ); |
| 70 | $values = LodashBasic::compactWithExceptions( array( $prefix, $value, $mediaPrefix ), array( '0', 0 ) ); |
| 71 | $prefixedClass = implode( '-', $values ); |
| 72 | |
| 73 | $classes[] = $prefixedClass; |
| 74 | } |
| 75 | return $classes; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 |