Skip to content

Commit ed03b4b

Browse files
committed
Improve embed format usage
Added: - Methods `isValidFormat()` and `assertValidFormat()` to `EmbedRepository`. - Constants `FORMAT_*` to `EmbedRepositoryInterface` to standardize supported formats.
1 parent 2eeaffc commit ed03b4b

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

src/Charcoal/Embed/Action/UpdateEmbedDataAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function run(RequestInterface $request, ResponseInterface $response)
4646
{
4747
$ident = $request->getParam('ident');
4848

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

5151
if ($results['ident']) {
5252
$this->setSuccess(true);

src/Charcoal/Embed/Contract/EmbedRepositoryInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
*/
88
interface EmbedRepositoryInterface
99
{
10+
const FORMAT_ARRAY = 'array';
11+
const FORMAT_HTML = 'html';
12+
const FORMAT_SRC = 'src';
13+
1014
/**
1115
* @param string $ident The embed URI to save from.
1216
* @param string $format The embed format.

src/Charcoal/Embed/Mixin/EmbedAwareTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Charcoal\Embed\Mixin;
44

5+
use Charcoal\Embed\Contract\EmbedRepositoryInterface;
56
use Charcoal\Translator\Translation;
67
use Embed\Embed;
78
use Exception;
@@ -100,11 +101,11 @@ private function resolveEmbedFormat($url, $format = null)
100101
}
101102
}
102103

103-
if ($format === 'src') {
104+
if ($format === EmbedRepositoryInterface::FORMAT_SRC) {
104105
return $src;
105106
}
106107

107-
if ($format === 'array') {
108+
if ($format === EmbedRepositoryInterface::FORMAT_ARRAY) {
108109
$provider = strtolower($embed->providerName);
109110

110111
// Extract an image preview from embedable iframe.

src/Charcoal/Embed/Service/EmbedRepository.php

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ public function embedData($ident)
430430

431431
$item = $this->load($ident);
432432
if ($item === false) {
433-
$item = $this->processEmbed($ident, 'array');
433+
$item = $this->processEmbed($ident, self::FORMAT_ARRAY);
434434
$this->saveItem($item);
435435
}
436436

@@ -632,18 +632,57 @@ public function format()
632632
}
633633

634634
/**
635-
* @param string $format Format for EmbedRepository.
635+
* @param string $format The embed format.
636636
* @return self
637637
*/
638638
public function setFormat($format)
639+
{
640+
$this->assertValidFormat($format);
641+
642+
$this->format = $format;
643+
644+
return $this;
645+
}
646+
647+
/**
648+
* Determines if the embed format is valid.
649+
*
650+
* @param string $format The format to test.
651+
* @return boolean
652+
*/
653+
public function isValidFormat($format)
639654
{
640655
switch ($format) {
641-
case 'array':
642-
case 'src':
643-
$this->format = $format;
644-
break;
656+
case self::FORMAT_ARRAY:
657+
case self::FORMAT_HTML:
658+
case self::FORMAT_SRC:
659+
return true;
645660
}
646661

647-
return $this;
662+
return false;
663+
}
664+
665+
/**
666+
* Asserts that the embed format is valid, throws an exception if not.
667+
*
668+
* @param string $format The format to test.
669+
* @throws InvalidArgumentException If the format is not a string or unsupported.
670+
* @return void
671+
*/
672+
public function assertValidFormat($format)
673+
{
674+
if (!is_string($format)) {
675+
throw new InvalidArgumentException(sprintf(
676+
'Unsupported format; must be a string, received %s',
677+
(is_object($format) ? get_class($format) : gettype($format))
678+
));
679+
}
680+
681+
if (!$this->isValidFormat($format)) {
682+
throw new InvalidArgumentException(sprintf(
683+
'Unsupported "%s" format',
684+
$format
685+
));
686+
}
648687
}
649688
}

src/Charcoal/Property/EmbedProperty.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class EmbedProperty extends UrlProperty
1919
/**
2020
* @var string|null $embedFormat
2121
*/
22-
protected $embedFormat = 'array';
22+
protected $embedFormat = EmbedRepository::FORMAT_ARRAY;
2323

2424
/**
2525
* @param Container $container A Pimple DI container.
@@ -46,6 +46,8 @@ public function embedFormat()
4646
*/
4747
public function setEmbedFormat($format)
4848
{
49+
$this->embedRepository()->assertValidFormat($format);
50+
4951
$this->embedFormat = $format;
5052

5153
return $this;

0 commit comments

Comments
 (0)