| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Document\Models\Elements; |
| 4 |
|
| 5 |
use Averta\Core\Utility\Arr; |
| 6 |
use Averta\Core\Utility\JSON; |
| 7 |
use Depicter\Document\Models\Common\Styles; |
| 8 |
use Depicter\Document\Models\Element; |
| 9 |
use Depicter\Html\Html; |
| 10 |
|
| 11 |
class Component extends Element |
| 12 |
{ |
| 13 |
|
| 14 |
/** |
| 15 |
* Render arrow markup |
| 16 |
* |
| 17 |
* @return \TypeRocket\Html\Html|void |
| 18 |
* @throws \JsonMapper_Exception |
| 19 |
*/ |
| 20 |
public function render() { |
| 21 |
$args = $this->getDefaultAttributes(); |
| 22 |
return Html::div( $args ); |
| 23 |
} |
| 24 |
|
| 25 |
public function getDefaultAttributes(){ |
| 26 |
$parentAttrs = parent::getDefaultAttributes(); // TODO: Change the autogenerated stub |
| 27 |
|
| 28 |
$options = $this->options; |
| 29 |
foreach( $options as $key => $option ) { |
| 30 |
if ( strpos( $key, 'imageAsset' ) ) { |
| 31 |
if ( is_object( $options->$key ) ) { |
| 32 |
$source = $this->hasDataSheet() ? $this->maybeReplaceDataSheetTags( $options->$key->src ) : $options->$key->src; |
| 33 |
$options->$key->srcset = \Depicter::media()->getSrcSet( $source ); |
| 34 |
$options->$key->src = \Depicter::media()->getSourceUrl( $source ); |
| 35 |
} else { |
| 36 |
// $option is string and is the src id of image asset |
| 37 |
$source = $this->hasDataSheet() ? $this->maybeReplaceDataSheetTags( $option ) : $option; |
| 38 |
$options->$key = \Depicter::media()->getSourceUrl( $source ); |
| 39 |
} |
| 40 |
} else if ( $key == 'choices' ) { |
| 41 |
foreach( $option as $optionKey => $choice ) { |
| 42 |
foreach ( $choice as $choiceKey => $value ) { |
| 43 |
if ( strpos( $choiceKey, 'imageAsset' ) ) { |
| 44 |
$source = $this->hasDataSheet() ? $this->maybeReplaceDataSheetTags( $value ) : $value; |
| 45 |
$options->$key[ $optionKey ]->$choiceKey = [ |
| 46 |
'src' => \Depicter::media()->getSourceUrl( $source ), |
| 47 |
'srcset' => \Depicter::media()->getSrcSet( $source ) |
| 48 |
]; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
$componentProps = JSON::encode( $options ); |
| 56 |
$componentProps = str_replace( ':imageAsset', '', $componentProps ); |
| 57 |
|
| 58 |
$attrs = [ |
| 59 |
'data-component-type' => $this->componentType, |
| 60 |
'data-component-props' => $componentProps |
| 61 |
]; |
| 62 |
|
| 63 |
return Arr::merge( $attrs, $parentAttrs ); |
| 64 |
} |
| 65 |
} |
| 66 |
|