| 1 |
<?php |
| 2 |
namespace Depicter\Document\Models\Common; |
| 3 |
|
| 4 |
|
| 5 |
use Averta\WordPress\Utility\JSON; |
| 6 |
use Depicter\Document\CSS\Breakpoints; |
| 7 |
|
| 8 |
class Animation |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Animation in phase |
| 12 |
* |
| 13 |
* @var object |
| 14 |
*/ |
| 15 |
public $in; |
| 16 |
|
| 17 |
/** |
| 18 |
* Animation out phase |
| 19 |
* |
| 20 |
* @var object |
| 21 |
*/ |
| 22 |
public $out; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var bool|null |
| 26 |
*/ |
| 27 |
public $waitForAction; |
| 28 |
|
| 29 |
/** |
| 30 |
* Known phases |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
protected $phases = ['in', 'out']; |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* @param string $phase |
| 39 |
* @param string $breakpoint |
| 40 |
* |
| 41 |
* @return false|string |
| 42 |
*/ |
| 43 |
public function getAnimation( $phase, $breakpoint ) { |
| 44 |
|
| 45 |
if( empty( $this->{$phase}->data->{$breakpoint}->type ) || ! isset( $this->{$phase}->data->{$breakpoint}->params ) ){ |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
$params = $this->{$phase}->data->{$breakpoint}->params; |
| 50 |
$params->type = $this->{$phase}->data->{$breakpoint}->type; |
| 51 |
|
| 52 |
return JSON::encode( $params ); |
| 53 |
} |
| 54 |
|
| 55 |
private function getWaitForAnimationAttr( $phase, $breakpoint ) { |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get all animation attributes |
| 61 |
* |
| 62 |
* @return array |
| 63 |
*/ |
| 64 |
public function getAnimationAttrs() { |
| 65 |
$attrs = []; |
| 66 |
|
| 67 |
// Collect animation attributes |
| 68 |
foreach ( Breakpoints::names() as $breakpoint ){ |
| 69 |
foreach ( $this->phases as $phase ){ |
| 70 |
$breakpoint_prefix = $breakpoint ? $breakpoint . '-' : $breakpoint; |
| 71 |
$breakpoint_prefix = $breakpoint == 'default' ? '' : $breakpoint_prefix; |
| 72 |
if( $animation_value = $this->getAnimation( $phase, $breakpoint ) ){ |
| 73 |
$attrs[ 'data-'. $breakpoint_prefix .'animation-' . $phase ] = $this->getAnimation( $phase, $breakpoint ); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// Get animation interactive attributes |
| 79 |
if( ! is_null( $this->waitForAction ) ){ |
| 80 |
$attrs[ "data-wait-for-action" ] = $this->waitForAction ? "true" : "false"; |
| 81 |
} |
| 82 |
|
| 83 |
if( isset( $this->out->wait ) ){ |
| 84 |
$attrs[ "data-animation-out-wait" ] = $this->out->wait ? "true" : "false"; |
| 85 |
} |
| 86 |
|
| 87 |
// Get animation interactive attributes |
| 88 |
foreach ( $this->phases as $phase ){ |
| 89 |
if( isset( $this->{$phase}->interactive ) ){ |
| 90 |
$attrs[ "data-animation-{$phase}-interactive" ] = $this->{$phase}->interactive ? "true" : "false"; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return $attrs; |
| 95 |
} |
| 96 |
} |
| 97 |
|