/**
  * @param array $array
  * @return ReferencedElementAbstract
  */
 public static function fromArray(array $array)
 {
     $element = null;
     if (empty($array['type'])) {
         throw new \RuntimeException('Element type is not specified.');
     }
     switch ($array['type']) {
         case LinkReferencedElement::TYPE_ID:
             $element = new LinkReferencedElement();
             break;
         case ImageReferencedElement::TYPE_ID:
             $element = new ImageReferencedElement();
             break;
         case 'video':
             // @TODO: BC. Remove.
         // @TODO: BC. Remove.
         case MediaReferencedElement::TYPE_ID:
             $element = new MediaReferencedElement();
             break;
         default:
             throw new \RuntimeException(sprintf('Unrecognized element type [%s].', $array['type']));
     }
     $element->fillFromArray($array);
     return $element;
 }
 /**
  * @param mixed $value
  * @return null
  */
 public function reverseTransform($value)
 {
     if (empty($value)) {
         $this->property->getMetadata()->clear();
         return null;
     }
     if (!is_array($value)) {
         throw new TransformationFailedException(sprintf('Expected array only, got [%s]', gettype($value)));
     }
     if (!isset($value['url'])) {
         throw new TransformationFailedException('Media URL is missing.');
     }
     $mediaEmbed = $this->container['cms.media_embed'];
     /* @var $mediaEmbed MediaEmbed */
     $mediaObject = $mediaEmbed->parseUrl($value['url']);
     if ($mediaObject === null) {
         throw new TransformationFailedException(sprintf('Failed to parse media URL [%s].', $value['url']));
     }
     $metadata = $this->property->getMetadata();
     $mediaElement = new MediaReferencedElement();
     $mediaElement->setUrl($value['url']);
     if (isset($value['width'])) {
         $width = (int) $value['width'];
         if ($width < 1) {
             throw new TransformationFailedException(sprintf('Invalid width value: [%s]', $value['width']));
         }
         $mediaElement->setWidth($width);
     }
     if (isset($value['height'])) {
         $height = (int) $value['height'];
         if ($height < 1) {
             throw new TransformationFailedException(sprintf('Invalid height value: [%s]', $value['height']));
         }
         $mediaElement->setHeight($height);
     }
     if (!isset($metadata['media'])) {
         $this->property->addMetadata(new BlockPropertyMetadata('media', $this->property));
     }
     $metadata['media']->setReferencedElement($mediaElement);
     return $value['url'];
 }
Esempio n. 3
0
 /**
  * @param MediaReferencedElement $mediaElement
  * @return null|string
  */
 protected function parseSupraMedia(MediaReferencedElement $mediaElement)
 {
     $mediaEmbed = $this->container['cms.media_embed'];
     /* @var $mediaEmbed MediaEmbed */
     $mediaObject = $mediaEmbed->parseUrl($mediaElement->getUrl());
     if ($mediaObject === null) {
         return null;
     }
     $mediaObject->setWidth($mediaElement->getWidth());
     $mediaObject->setHeight($mediaElement->getHeight());
     return $mediaObject->getEmbedCode();
 }
Esempio n. 4
0
 /**
  * @param MediaReferencedElement $element
  * @return null|string
  */
 protected function handleMediaElement(MediaReferencedElement $element)
 {
     $mediaEmbed = $this->container['cms.media_embed'];
     /* @var $mediaEmbed MediaEmbed */
     $mediaObject = $mediaEmbed->parseUrl($element->getUrl());
     if ($mediaObject === null) {
         return null;
     }
     $metadata = $this->property->getMetadata();
     if ($metadata->offsetExists('width')) {
         $width = (int) $metadata->get('width')->getValue();
         $mediaObject->setWidth($width);
     }
     if ($metadata->offsetExists('height')) {
         $height = (int) $metadata->get('height')->getValue();
         $mediaObject->setHeight($height);
     }
     return $mediaObject->getEmbedCode();
 }
 /**
  * @param array $data
  * @return string
  */
 protected function reverseTransformMediaData(array $data)
 {
     if (!isset($data['url'])) {
         throw new TransformationFailedException('Media URL is missing.');
     }
     $mediaEmbed = $this->container['cms.media_embed'];
     /* @var $mediaEmbed MediaEmbed */
     $mediaObject = $mediaEmbed->parseUrl($data['url']);
     if ($mediaObject === null) {
         throw new TransformationFailedException(sprintf('Failed to parse media URL [%s].', $data['url']));
     }
     $mediaElement = new MediaReferencedElement();
     $mediaElement->setUrl($data['url']);
     $this->getMediaMetadata()->setReferencedElement($mediaElement);
     if (isset($data['width'])) {
         $width = (int) $data['width'];
         if ($width < 1) {
             throw new TransformationFailedException(sprintf('Invalid width value: [%s]', $data['width']));
         }
         $mediaElement->setWidth($width);
     }
     if (isset($data['height'])) {
         $height = (int) $data['height'];
         if ($height < 1) {
             throw new TransformationFailedException(sprintf('Invalid height value: [%s]', $data['height']));
         }
         $mediaElement->setHeight($height);
     }
     return $data['url'];
 }
 public function reverseTransform($value)
 {
     // used fonts list
     // @FIXME: sanitize list
     $fonts = !empty($value['fonts']) ? $value['fonts'] : array();
     $this->getFontsMetadata()->setValue(serialize($fonts));
     // additional metadata
     $metadata = $this->property->getMetadata();
     // @TODO: not performance-wise.
     $metadata->clear();
     if (!empty($value['data'])) {
         foreach ($value['data'] as $name => $itemData) {
             if (empty($itemData['type'])) {
                 throw new TransformationFailedException(sprintf('No type specified for HTML metadata element [%s].', $name));
             }
             $element = null;
             switch ($itemData['type']) {
                 case ImageReferencedElement::TYPE_ID:
                 case LinkReferencedElement::TYPE_ID:
                     $element = ReferencedElementAbstract::fromArray($itemData);
                     break;
                 case 'video':
                     // @TODO: BC. Remove.
                 // @TODO: BC. Remove.
                 case MediaReferencedElement::TYPE_ID:
                     if (empty($itemData['url'])) {
                         throw new TransformationFailedException(sprintf('No media URL specified for item [%s].', $name));
                     }
                     $mediaEmbed = $this->container['cms.media_embed'];
                     /* @var $mediaEmbed MediaEmbed */
                     $mediaObject = $mediaEmbed->parseUrl($itemData['url']);
                     if ($mediaObject === null) {
                         throw new TransformationFailedException(sprintf('Failed to parse media URL [%s].', $itemData['url']));
                     }
                     $element = new MediaReferencedElement();
                     $element->setUrl($itemData['url']);
                     // width attribute
                     if (isset($itemData['width'])) {
                         $width = (int) $itemData['width'];
                         if ($width < 1) {
                             throw new TransformationFailedException(sprintf('Invalid width value: [%s]', $itemData['width']));
                         }
                         $element->setWidth($width);
                     }
                     // height attribute
                     if (isset($itemData['height'])) {
                         $height = (int) $itemData['height'];
                         if ($height < 1) {
                             throw new TransformationFailedException(sprintf('Invalid height value: [%s]', $itemData['height']));
                         }
                         $element->setHeight($height);
                     }
                     break;
                 default:
                     throw new TransformationFailedException(sprintf('Unrecognized HTML metadata element type [%s].', $itemData['type']));
             }
             $metaItem = new BlockPropertyMetadata($name, $this->property);
             $metaItem->setReferencedElement($element);
             $metadata->set($name, $metaItem);
         }
     }
     // return HTML as is
     return !empty($value['html']) ? $value['html'] : null;
 }