becklyn/video-platforms
Symfony bundle providing helpers to parse and normalize video URLs (YouTube/Vimeo etc.), serialize/store as “platform@id” or arrays, validate via constraints, and integrate with entities and forms using a VideoUrlType.
Several helpers for integrating video tools in your Symfony application.
composer require becklyn/video-platforms
use Becklyn\VideoPlatforms\Parser\VideoUrlParser;
function parse (VideoUrlParser $parser, string $videoUrl)
{
$video = $parser->parse($videoUrl);
}
Any video will be stored as normalized array, and can be recreated from it. See below "Usage in entities" for more information.
There is also a simple string-based serialization, although you will lose the initial format:
use Becklyn\VideoPlatforms\Video\Video;
$video = new Video("youtube", "123");
assert("youtube@123" === $video->serialize());
The internal format is <platform>@<id>. That can easily be stored in the database.
To unserialize, just use
use Becklyn\VideoPlatforms\Video\Video;
$serialized = "youtube@123";
$video = Video::unserialize($serialized);
assert("youtube" === $video->getPlatform());
assert("123" === $video->getId());
// will be autogenerate
assert("youtube@123" === $video->getUrl());
Your entity should look something like this:
use Becklyn\VideoPlatforms\Validation\Constraint\VideoUrl;
use Becklyn\VideoPlatforms\Video\Video;
class MyEntity
{
/**
* @ORM\Column(name="video", type="json")
*
* @VideoUrl(platforms={"vimeo"})
* @Assert\NotNull()
*/
private ?array $video = null;
/**
*/
public function getVideo () : ?Video
{
return Video::createFromArray($this->video);
}
/**
*/
public function setVideoUrl (?Video $video) : void
{
$this->video = null !== $video
? $video->toArray()
: null;
}
}
In your form you should use the VideoUrlType:
use Becklyn\VideoPlatforms\Form\Type\VideoUrlType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class MyForm extends AbstractType
{
/**
* @inheritDoc
*/
public function buildForm (FormBuilderInterface $builder, array $options) : void
{
parent::buildForm($builder, $options);
$builder
->add("video", VideoUrlType::class, [
"label" => "video.label",
"required" => true,
]);
}
}
Tip:
When entering the value in a form field, you can always use the string-serialized version, to avoid parsing clashes. So just enter
vimeo@123for example.
You can use the @VideoUrl() annotation on any property.
/**
* @VideoUrl()
*/
You can also define which platforms you want to allow. Use the platform key:
/**
* @VideoUrl(platforms={"vimeo"})
*/
Implement the VideoUrlParserInterface and either use autoconfiguration
or add the DI tag becklyn.video-platforms.parser.
123456789 (plain id, watch out for clashes)https://vimeo.com/123456789_1234567890 (plain id, watch out for clashes)https://www.youtube.com/watch?v=_1234567890https://www.youtube.com/v/_1234567890https://youtu.be/_1234567890https://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D_1234567890&format=jsonhttps://youtube.com/embed/_1234567890https://www.youtube.com/attribution_link?a=sgsfg&u=%2Fwatch%3Fv%3D_1234567890%26feature%3Dem-uploademailHow can I help you explore Laravel packages today?