Skip to content

Commit 12cf5e4

Browse files
authored
added youtube tag (#16)
* added youtube tag
1 parent 273752d commit 12cf5e4

4 files changed

Lines changed: 106 additions & 1 deletion

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "php parser for bb code",
55
"license": "MIT",
66
"require": {
7-
"php": ">=7.0"
7+
"php": ">=7.0",
8+
"tekstove/url-video-parser": "^1.0.1"
89
},
910
"require-dev": {
1011
"phpUnit/phpUnit": "5.6.*"

src/Potaka/BbCode/Factory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Potaka\BbCode\Tag\Link;
1010
use Potaka\BbCode\Tag\ImgTag;
1111

12+
use Potaka\BbCode\Tag\YoutubeTag;
13+
1214
/**
1315
* Create different BbCode configurations
1416
*
@@ -43,13 +45,17 @@ public function getFullBbCode() : BbCode
4345
$unknownTag = new Tag\UnknownSimpleType();
4446
$bbcode->addTag($unknownTag);
4547

48+
$youtubeTag = new YoutubeTag();
49+
$bbcode->addTag($youtubeTag);
50+
4651
$tags = [
4752
$bold,
4853
$underline,
4954
$italic,
5055
$link,
5156
$unknownTag,
5257
$img,
58+
$youtubeTag,
5359
];
5460

5561
// link allowed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Potaka\BbCode\Tag;
4+
5+
use Potaka\BbCode\Tokenizer\Tag as TokenTag;
6+
use Tekstove\UrlVideoParser\Youtube\YoutubeParser;
7+
use Tekstove\UrlVideoParser\Youtube\YoutubeException;
8+
9+
/**
10+
* Tag for youtube videos
11+
*
12+
* @author po_taka <angel.koilov@gmail.com>
13+
*/
14+
class YoutubeTag implements TagInterface
15+
{
16+
public function format(TokenTag $tokenTag): string
17+
{
18+
$link = $tokenTag->getText();
19+
20+
try {
21+
$parser = new YoutubeParser();
22+
$videoId = $parser->getId($link);
23+
} catch (YoutubeException $e) {
24+
$unknownTag = new UnknownSimpleType();
25+
return $unknownTag->format($tokenTag);
26+
}
27+
28+
return '<iframe src="https://www.youtube.com/embed/' . $videoId . '" frameborder="0" allowfullscreen></iframe>';
29+
}
30+
31+
public function getName(): string
32+
{
33+
return 'youtube';
34+
}
35+
36+
public function getOriginalText(TokenTag $tokenTag): string
37+
{
38+
return "[{$this->getName()}]{$tokenTag->getText()}[/{$this->getName()}";
39+
}
40+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
use Potaka\BbCode\Tokenizer\Tag as TokenTag;
6+
use Potaka\BbCode\Tag\YoutubeTag;
7+
8+
/**
9+
* Description of YoutubeTest
10+
*
11+
* @author po_taka <angel.koilov@gmail.com>
12+
*/
13+
class YoutubeTest extends TestCase
14+
{
15+
public function testFormat()
16+
{
17+
$cases = [
18+
[
19+
'link' => 'https://www.youtube.com/watch?v=zur_B7kw9uM',
20+
'id' => 'zur_B7kw9uM',
21+
],
22+
[
23+
'link' => 'https://www.youtube.com/watch?v=zur_B7kw9uM&feature=youtu.be&t=8259',
24+
'id' => 'zur_B7kw9uM',
25+
],
26+
];
27+
28+
foreach ($cases as $testData) {
29+
$tokenTag = new TokenTag('youtube');
30+
$tokenTag->setText($testData['link']);
31+
$youtubeTag = new YoutubeTag();
32+
$html = $youtubeTag->format($tokenTag);
33+
$this->assertSame('<iframe src="https://www.youtube.com/embed/' . $testData['id'] . '" frameborder="0" allowfullscreen></iframe>', $html);
34+
}
35+
}
36+
37+
public function testInvalidFormat()
38+
{
39+
$cases = [
40+
'ftp://lorempixel.com/output/abstract-q-c-640-480-2.jpg', // ftp
41+
'https://www.youtube.com/watch?v=zur_^B7kw9uM', // ^
42+
];
43+
44+
foreach ($cases as $testData) {
45+
$tokenTag = new TokenTag('youtube');
46+
$tokenTag->setText($testData);
47+
$youtubeTag = new YoutubeTag();
48+
$html = $youtubeTag->format($tokenTag);
49+
$this->assertSame('[youtube]' . $testData . '[/youtube]', $html);
50+
}
51+
}
52+
53+
public function testGetName()
54+
{
55+
$youtube = new YoutubeTag();
56+
$this->assertSame('youtube', $youtube->getName());
57+
}
58+
}

0 commit comments

Comments
 (0)