becklyn/video-platforms
最新稳定版本:2.1.0
Composer 安装命令:
composer require becklyn/video-platforms
包简介
Several helpers for integrating video tools in your Symfony application.
README 文档
README
Several helpers for integrating video tools in your Symfony application.
Installation
composer require becklyn/video-platforms
Usage
Parsing an URL
use Becklyn\VideoPlatforms\Parser\VideoUrlParser; function parse (VideoUrlParser $parser, string $videoUrl) { $video = $parser->parse($videoUrl); }
Storage
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());
Usage in entities
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; } }
Usage in Forms
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.
Validation
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"}) */
Registering a custom platform
Implement the VideoUrlParserInterface and either use autoconfiguration
or add the DI tag becklyn.video-platforms.parser.
Supported Formats
Vimeo
123456789(plain id, watch out for clashes)https://vimeo.com/123456789
YouTube
_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-uploademail
统计信息
- 总下载量: 6.23k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2020-05-14