Skip to content

Commit

Permalink
Fix parseId()
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed Jun 24, 2015
1 parent 0a97b82 commit 67cdd3f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 28 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public function video($host, $id, array $options = []) {
if (!$MediaObject) {
return '';
}
if (!empty($options['attributes'])) {
foreach ($options['attributes'] as $attribute => $value) {
$MediaObject->setAttribute($attribute, $value);
}
}
return $MediaObject->getEmbedCode();
}
```
Expand Down
89 changes: 61 additions & 28 deletions src/MediaEmbed/Object/MediaObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,6 @@ public function __construct(array $stub, array $config) {

$this->_setDefaultParams($stub);

/*
// iframe or object?
if (isset($stub['iframe-player'])) {
if ($config['prefer'] === 'iframe') {
$src = $this->getObjectSrc($data, 'iframe-player');
$stub['iframe-player'] = $src;
return true;
}
unset($stub['iframe-player']);
}
*/
$type = 'embed-src';
if (isset($this->_stub['iframe-player'])) {
if ($this->config['prefer'] === 'iframe') {
Expand All @@ -73,9 +62,16 @@ public function __construct(array $stub, array $config) {
$this->_objectParams['movie'] = $src;
$this->_objectAttributes['data'] = $src;
}
if (!empty($this->_stub['reverse'])) {
$flashvars = $this->_objectParams['flashvars'];
$this->_objectParams['flashvars'] = str_replace('$r2', $this->_stub['id'], $flashvars);

if (empty($this->_stub['reverse'])) {
return;
}

$flashvars = $this->_objectParams['flashvars'];
if (strpos($flashvars, '$r2') !== false) {
$this->_objectParams['flashvars'] = str_replace('$r2', $this->_stub['id'], $flashvars);
} else {
$this->_objectParams['flashvars'] = str_replace('$2', $this->_stub['id'], $flashvars);
}
}

Expand Down Expand Up @@ -154,7 +150,6 @@ public function website() {
/**
* Returns a png img
*
* @param array $stub or string $alias
* @return Resource or null if not available
*/
public function icon() {
Expand All @@ -181,8 +176,8 @@ public function icon() {
}

/**
* @param string location Absolute path with trailing slash
* @param binary Icon Icon data
* @param string $location Absolute path with trailing slash
* @param binary $icon Icon data
* @return string|null $filename
*/
public function saveIcon($location = null, $icon = null) {
Expand Down Expand Up @@ -244,10 +239,9 @@ public function setParam($param, $value = null) {
* Override a default object attribute value
*
* @param $param mixed - the name of the attribute to be set
* or an array of multiple attribs to be set
* or an array of multiple attribs to be set
* @param $value string - (optional) the value to set the param to
* if only one param is being set
*
* if only one param is being set
* @return $this
*/
public function setAttribute($param, $value = null) {
Expand Down Expand Up @@ -276,28 +270,53 @@ public function setAttribute($param, $value = null) {
/**
* Set the height of the object
*
* @param mixed - height to set the object to
*
* @param mixed $height Height to set the object to
* @param bool $adjustWidth
* @return $this
*/
public function setHeight($height) {
public function setHeight($height, $adjustWidth = false) {
if ($adjustWidth && is_numeric($height)) {
$this->_adjustDimensions('width', 'height', $height);
}
return $this->setAttribute('height', $height);
}

/**
* Set the width of the object
*
* @param mixed - width to set the object to
*
* @param mixed $width Width to set the object to
* @param bool $adjustHeight
* @return $this
*/
public function setWidth($width) {
public function setWidth($width, $adjustHeight = false) {
if ($adjustHeight && is_numeric($width)) {
$this->_adjustDimensions('height', 'width', $width);
}
return $this->setAttribute('width', $width);
}

/**
* Auto-adjusts one dimension from the other to keep the current ratio.
*
* @param string $type
* @param string $fromType
* @param int $fromLength
* @return void
*/
protected function _adjustDimensions($type, $fromType, $fromLength) {
$currentLength = $this->getAttributes($type);
$currentFromLength = $this->getAttributes($fromType);

$ratio = $fromLength / $currentFromLength;
$newLength = $currentLength * $ratio;

$this->setAttribute($type, (int)$newLength);
}

/**
* Return object params about the video metadata
*
* @param string|null $key
* @return array|string - object params
*/
public function getParams($key = null) {
Expand All @@ -323,6 +342,7 @@ public function getParams($key = null) {
/**
* Return object attribute
*
* @param string|null $key
* @return array - object attribute
*/
public function getAttributes($key = null) {
Expand Down Expand Up @@ -360,6 +380,7 @@ public function getEmbedCode() {
/**
* Getter/setter of what this Object currently prefers as output type
*
* @param string|null $type
* @return $this|string
*/
protected function prefers($type = null) {
Expand All @@ -385,7 +406,12 @@ protected function _getObjectSrc($type = 'embed-src') {
return;
}

$src = str_replace('$r2', $this->_stub['id'], $this->_stub[$type]);
$stubSrc = $this->_stub[$type];
if (strpos($stubSrc, '$r2') !== false) {
$src = str_replace('$r2', $this->_stub['id'], $stubSrc);
} else {
$src = str_replace('$2', $this->_stub['id'], $stubSrc);
}
if (!empty($host['replace'])) {
foreach ($host['replace'] as $placeholder => $replacement) {
$src = str_replace($placeholder, $replacement, $src);
Expand All @@ -408,7 +434,13 @@ public function getImageSrc($data) {
return;
}

$src = str_replace('$r2', $this->_stub['id'], $this->_stub['image-src']);
$stubImgSrc = $this->_stub['image-src'];
if (strpos($stubImgSrc, '$r2') !== false) {
$src = str_replace('$r2', $this->_stub['id'], $stubImgSrc);
} else {
$src = str_replace('$2', $this->_stub['id'], $stubImgSrc);
}

return $src;
}

Expand Down Expand Up @@ -498,6 +530,7 @@ protected function _buildIframe() {
* Set the default params for the type of
* stub we are working with
*
* @param array $stub
* @return void
*/
protected function _setDefaultParams($stub) {
Expand Down

0 comments on commit 67cdd3f

Please sign in to comment.