Browse Source

added youtube tag (#16)

* added youtube tag
tags/0.2.0
Angel Koilov (po_taka) GitHub 7 years ago
parent
commit
12cf5e4271
4 changed files with 106 additions and 1 deletions
  1. +2
    -1
      composer.json
  2. +6
    -0
      src/Potaka/BbCode/Factory.php
  3. +40
    -0
      src/Potaka/BbCode/Tag/YoutubeTag.php
  4. +58
    -0
      tests/phpUnit/Test/Potaka/BbCode/Tag/YoutubeTest.php

+ 2
- 1
composer.json View File

@@ -4,7 +4,8 @@
"description": "php parser for bb code",
"license": "MIT",
"require": {
"php": ">=7.0"
"php": ">=7.0",
"tekstove/url-video-parser": "^1.0.1"
},
"require-dev": {
"phpUnit/phpUnit": "5.6.*"


+ 6
- 0
src/Potaka/BbCode/Factory.php View File

@@ -9,6 +9,8 @@ use Potaka\BbCode\Tag\Italic;
use Potaka\BbCode\Tag\Link;
use Potaka\BbCode\Tag\ImgTag;

use Potaka\BbCode\Tag\YoutubeTag;

/**
* Create different BbCode configurations
*
@@ -43,6 +45,9 @@ class Factory
$unknownTag = new Tag\UnknownSimpleType();
$bbcode->addTag($unknownTag);

$youtubeTag = new YoutubeTag();
$bbcode->addTag($youtubeTag);

$tags = [
$bold,
$underline,
@@ -50,6 +55,7 @@ class Factory
$link,
$unknownTag,
$img,
$youtubeTag,
];

// link allowed


+ 40
- 0
src/Potaka/BbCode/Tag/YoutubeTag.php View File

@@ -0,0 +1,40 @@
<?php

namespace Potaka\BbCode\Tag;

use Potaka\BbCode\Tokenizer\Tag as TokenTag;
use Tekstove\UrlVideoParser\Youtube\YoutubeParser;
use Tekstove\UrlVideoParser\Youtube\YoutubeException;

/**
* Tag for youtube videos
*
* @author po_taka <angel.koilov@gmail.com>
*/
class YoutubeTag implements TagInterface
{
public function format(TokenTag $tokenTag): string
{
$link = $tokenTag->getText();

try {
$parser = new YoutubeParser();
$videoId = $parser->getId($link);
} catch (YoutubeException $e) {
$unknownTag = new UnknownSimpleType();
return $unknownTag->format($tokenTag);
}

return '<iframe src="https://www.youtube.com/embed/' . $videoId . '" frameborder="0" allowfullscreen></iframe>';
}

public function getName(): string
{
return 'youtube';
}

public function getOriginalText(TokenTag $tokenTag): string
{
return "[{$this->getName()}]{$tokenTag->getText()}[/{$this->getName()}";
}
}

+ 58
- 0
tests/phpUnit/Test/Potaka/BbCode/Tag/YoutubeTest.php View File

@@ -0,0 +1,58 @@
<?php

use PHPUnit\Framework\TestCase;

use Potaka\BbCode\Tokenizer\Tag as TokenTag;
use Potaka\BbCode\Tag\YoutubeTag;

/**
* Description of YoutubeTest
*
* @author po_taka <angel.koilov@gmail.com>
*/
class YoutubeTest extends TestCase
{
public function testFormat()
{
$cases = [
[
'link' => 'https://www.youtube.com/watch?v=zur_B7kw9uM',
'id' => 'zur_B7kw9uM',
],
[
'link' => 'https://www.youtube.com/watch?v=zur_B7kw9uM&feature=youtu.be&t=8259',
'id' => 'zur_B7kw9uM',
],
];

foreach ($cases as $testData) {
$tokenTag = new TokenTag('youtube');
$tokenTag->setText($testData['link']);
$youtubeTag = new YoutubeTag();
$html = $youtubeTag->format($tokenTag);
$this->assertSame('<iframe src="https://www.youtube.com/embed/' . $testData['id'] . '" frameborder="0" allowfullscreen></iframe>', $html);
}
}

public function testInvalidFormat()
{
$cases = [
'ftp://lorempixel.com/output/abstract-q-c-640-480-2.jpg', // ftp
'https://www.youtube.com/watch?v=zur_^B7kw9uM', // ^
];

foreach ($cases as $testData) {
$tokenTag = new TokenTag('youtube');
$tokenTag->setText($testData);
$youtubeTag = new YoutubeTag();
$html = $youtubeTag->format($tokenTag);
$this->assertSame('[youtube]' . $testData . '[/youtube]', $html);
}
}

public function testGetName()
{
$youtube = new YoutubeTag();
$this->assertSame('youtube', $youtube->getName());
}
}

Loading…
Cancel
Save