]> git.mxchange.org Git - friendica.git/commitdiff
Move bb_translate_video
authorPhilipp Holzer <admin+github@philipp.info>
Tue, 22 Oct 2019 22:14:47 +0000 (00:14 +0200)
committerPhilipp Holzer <admin+github@philipp.info>
Tue, 22 Oct 2019 22:14:47 +0000 (00:14 +0200)
- To new Class BBCode\Video
- Adding tests
- Make BaseObject::getClass() public

include/text.php
mod/item.php
src/BaseObject.php
src/Content/Text/BBCode/Video.php [new file with mode: 0644]
tests/src/Content/Text/BBCode/VideoTest.php [new file with mode: 0644]

index 0935c5e7409a562f9c0c021c754d01627c560b46..6eb1e46ffd0021e62e95d4b8cd4d5261e872f176 100644 (file)
@@ -223,22 +223,6 @@ function return_bytes($size_str) {
        }
 }
 
-function bb_translate_video($s) {
-
-       $matches = null;
-       $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
-       if ($r) {
-               foreach ($matches as $mtch) {
-                       if ((stristr($mtch[1], 'youtube')) || (stristr($mtch[1], 'youtu.be'))) {
-                               $s = str_replace($mtch[0], '[youtube]' . $mtch[1] . '[/youtube]', $s);
-                       } elseif (stristr($mtch[1], 'vimeo')) {
-                               $s = str_replace($mtch[0], '[vimeo]' . $mtch[1] . '[/vimeo]', $s);
-                       }
-               }
-       }
-       return $s;
-}
-
 /// @TODO Rewrite this
 function is_a_date_arg($s) {
        $i = intval($s);
index 7c8ebee4abfdf5e3ae5c8271b394bd8bf4d0147e..c9a33cc20643591459691a68e9f3490106c649be 100644 (file)
@@ -24,8 +24,8 @@ use Friendica\Core\Hook;
 use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
-use Friendica\Core\System;
 use Friendica\Core\Session;
+use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\Model\Attach;
@@ -499,8 +499,9 @@ function item_post(App $a) {
                $objecttype = ACTIVITY_OBJ_BOOKMARK;
        }
 
-       $body = bb_translate_video($body);
-
+       /** @var BBCode\Video $bbCodeVideo */
+       $bbCodeVideo = \Friendica\BaseObject::getClass(BBCode\Video::class);
+       $body =  $bbCodeVideo->transform($body);
 
        // Fold multi-line [code] sequences
        $body = preg_replace('/\[\/code\]\s*\[code\]/ism', "\n", $body);
index 996824f4a132b631dddfbad6a6296f6a5f5905fd..20481884517e565123d8b17f7038a6033250525a 100644 (file)
@@ -54,7 +54,7 @@ class BaseObject
         *
         * @throws InternalServerErrorException
         */
-       protected static function getClass(string $name)
+       public static function getClass(string $name)
        {
                if (empty(self::$dice)) {
                        throw new InternalServerErrorException('DICE isn\'t initialized.');
diff --git a/src/Content/Text/BBCode/Video.php b/src/Content/Text/BBCode/Video.php
new file mode 100644 (file)
index 0000000..b73ddce
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+namespace Friendica\Content\Text\BBCode;
+
+/**
+ * Video specific BBCode util class
+ */
+final class Video
+{
+       /**
+        * Transforms video BBCode tagged links to youtube/vimeo tagged links
+        *
+        * @param string $bbCodeString The input BBCode styled string
+        *
+        * @return string The transformed text
+        */
+       public function transform(string $bbCodeString)
+       {
+               $matches = null;
+               $found = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$bbCodeString,$matches,PREG_SET_ORDER);
+               if ($found) {
+                       foreach ($matches as $match) {
+                               if ((stristr($match[1], 'youtube')) || (stristr($match[1], 'youtu.be'))) {
+                                       $bbCodeString = str_replace($match[0], '[youtube]' . $match[1] . '[/youtube]', $bbCodeString);
+                               } elseif (stristr($match[1], 'vimeo')) {
+                                       $bbCodeString = str_replace($match[0], '[vimeo]' . $match[1] . '[/vimeo]', $bbCodeString);
+                               }
+                       }
+               }
+               return $bbCodeString;
+       }
+}
diff --git a/tests/src/Content/Text/BBCode/VideoTest.php b/tests/src/Content/Text/BBCode/VideoTest.php
new file mode 100644 (file)
index 0000000..dcdbf93
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+namespace src\Content\Text\BBCode;
+
+use Friendica\Content\Text\BBCode\Video;
+use Friendica\Test\MockedTest;
+
+class VideoTest extends MockedTest
+{
+       public function dataVideo()
+       {
+               return [
+                       'youtube' => [
+                               'input' => '[video]https://youtube.link/4523[/video]',
+                               'assert' => '[youtube]https://youtube.link/4523[/youtube]',
+                       ],
+                       'youtu.be' => [
+                               'input' => '[video]https://youtu.be.link/4523[/video]',
+                               'assert' => '[youtube]https://youtu.be.link/4523[/youtube]',
+                       ],
+                       'vimeo' => [
+                               'input' => '[video]https://vimeo.link/2343[/video]',
+                               'assert' => '[vimeo]https://vimeo.link/2343[/vimeo]',
+                       ],
+                       'mixed' => [
+                               'input' => '[video]https://vimeo.link/2343[/video] With other [b]string[/b] [video]https://youtu.be/blaa[/video]',
+                               'assert' => '[vimeo]https://vimeo.link/2343[/vimeo] With other [b]string[/b] [youtube]https://youtu.be/blaa[/youtube]',
+                       ]
+               ];
+       }
+
+       /**
+        * Test if the BBCode is successfully transformed for video links
+        *
+        * @dataProvider dataVideo
+        */
+       public function testTransform(string $input, string $assert)
+       {
+               $bbCodeVideo = new Video();
+
+               $this->assertEquals($assert, $bbCodeVideo->transform($input));
+       }
+}