Skip to content

Commit

Permalink
Improve embed format usage
Browse files Browse the repository at this point in the history
Added:
- Methods `isValidFormat()` and `assertValidFormat()` to `EmbedRepository`.
- Constants `FORMAT_*` to `EmbedRepositoryInterface` to standardize supported formats.
  • Loading branch information
mcaskill committed Jan 17, 2022
1 parent 2eeaffc commit ed03b4b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Charcoal/Embed/Action/UpdateEmbedDataAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function run(RequestInterface $request, ResponseInterface $response)
{
$ident = $request->getParam('ident');

$results = $this->embedRepository()->saveEmbedData($ident, 'array');
$results = $this->embedRepository()->saveEmbedData($ident, self::FORMAT_ARRAY);

if ($results['ident']) {
$this->setSuccess(true);
Expand Down
4 changes: 4 additions & 0 deletions src/Charcoal/Embed/Contract/EmbedRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
*/
interface EmbedRepositoryInterface
{
const FORMAT_ARRAY = 'array';
const FORMAT_HTML = 'html';
const FORMAT_SRC = 'src';

/**
* @param string $ident The embed URI to save from.
* @param string $format The embed format.
Expand Down
5 changes: 3 additions & 2 deletions src/Charcoal/Embed/Mixin/EmbedAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Charcoal\Embed\Mixin;

use Charcoal\Embed\Contract\EmbedRepositoryInterface;
use Charcoal\Translator\Translation;
use Embed\Embed;
use Exception;
Expand Down Expand Up @@ -100,11 +101,11 @@ private function resolveEmbedFormat($url, $format = null)
}
}

if ($format === 'src') {
if ($format === EmbedRepositoryInterface::FORMAT_SRC) {
return $src;
}

if ($format === 'array') {
if ($format === EmbedRepositoryInterface::FORMAT_ARRAY) {
$provider = strtolower($embed->providerName);

// Extract an image preview from embedable iframe.
Expand Down
53 changes: 46 additions & 7 deletions src/Charcoal/Embed/Service/EmbedRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public function embedData($ident)

$item = $this->load($ident);
if ($item === false) {
$item = $this->processEmbed($ident, 'array');
$item = $this->processEmbed($ident, self::FORMAT_ARRAY);
$this->saveItem($item);
}

Expand Down Expand Up @@ -632,18 +632,57 @@ public function format()
}

/**
* @param string $format Format for EmbedRepository.
* @param string $format The embed format.
* @return self
*/
public function setFormat($format)
{
$this->assertValidFormat($format);

$this->format = $format;

return $this;
}

/**
* Determines if the embed format is valid.
*
* @param string $format The format to test.
* @return boolean
*/
public function isValidFormat($format)
{
switch ($format) {
case 'array':
case 'src':
$this->format = $format;
break;
case self::FORMAT_ARRAY:
case self::FORMAT_HTML:
case self::FORMAT_SRC:
return true;
}

return $this;
return false;
}

/**
* Asserts that the embed format is valid, throws an exception if not.
*
* @param string $format The format to test.
* @throws InvalidArgumentException If the format is not a string or unsupported.
* @return void
*/
public function assertValidFormat($format)
{
if (!is_string($format)) {
throw new InvalidArgumentException(sprintf(
'Unsupported format; must be a string, received %s',
(is_object($format) ? get_class($format) : gettype($format))
));
}

if (!$this->isValidFormat($format)) {
throw new InvalidArgumentException(sprintf(
'Unsupported "%s" format',
$format
));
}
}
}
4 changes: 3 additions & 1 deletion src/Charcoal/Property/EmbedProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EmbedProperty extends UrlProperty
/**
* @var string|null $embedFormat
*/
protected $embedFormat = 'array';
protected $embedFormat = EmbedRepository::FORMAT_ARRAY;

/**
* @param Container $container A Pimple DI container.
Expand All @@ -46,6 +46,8 @@ public function embedFormat()
*/
public function setEmbedFormat($format)
{
$this->embedRepository()->assertValidFormat($format);

$this->embedFormat = $format;

return $this;
Expand Down

0 comments on commit ed03b4b

Please sign in to comment.