diff --git a/README.md b/README.md index 43a32f7..a96441c 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,11 @@ OEmbed: - X (Twitter) - TikTok - Reddit -- Facebook Custom: - Youtube +- Facebook - Instagram - Github Gist diff --git a/src/Embed/Embed.php b/src/Embed/Embed.php index f7d9aed..80ab284 100644 --- a/src/Embed/Embed.php +++ b/src/Embed/Embed.php @@ -17,10 +17,15 @@ class Embed public static function getParsers(): array { $namespace = __NAMESPACE__ . '\\Platforms\\'; - return array_map( + + $parsers = array_map( fn ($file) => $namespace . pathinfo((string)$file, PATHINFO_FILENAME), (array)glob(__DIR__ . '/Platforms/*.php') ); + + usort($parsers, fn ($a, $b) => $b::PRIORITY <=> $a::PRIORITY); + + return $parsers; } /** @@ -40,6 +45,25 @@ public static function parse( throw new EmbedUnableToResolveException(); } + /** + * @param string $url + * @return array{parser: EmbedParserAbstract, matches: string[]}|null + */ + public static function getMatchingParser(string $url): ?array + { + foreach (self::getParsers() as $parserClass) { + /** @var EmbedParserAbstract $parser */ + $parser = new $parserClass($url); + if ($parser->match()) { + return [ + 'parser' => $parser, + 'matches' => $parser->match(), + ]; + } + } + return null; + } + /** * @throws UnfoldException */ diff --git a/src/Embed/EmbedParserAbstract.php b/src/Embed/EmbedParserAbstract.php index c5abee0..e780e75 100644 --- a/src/Embed/EmbedParserAbstract.php +++ b/src/Embed/EmbedParserAbstract.php @@ -16,6 +16,9 @@ */ abstract class EmbedParserAbstract { + // define priority relatively + public const PRIORITY = 0; + protected UnfoldConfig $config; public function __construct( diff --git a/src/Embed/PlatformHelpers/FacebookHelper.php b/src/Embed/PlatformHelpers/FacebookHelper.php new file mode 100644 index 0000000..59a9619 --- /dev/null +++ b/src/Embed/PlatformHelpers/FacebookHelper.php @@ -0,0 +1,15 @@ + + +HTML; + } + +} diff --git a/src/Embed/Platforms/FacebookPage.php b/src/Embed/Platforms/FacebookPage.php new file mode 100644 index 0000000..6702801 --- /dev/null +++ b/src/Embed/Platforms/FacebookPage.php @@ -0,0 +1,36 @@ +url; + + $name = explode('/', $url)[3] ?? ''; + $sdk = FacebookHelper::sdkScript(); + + return <<
$name+HTML; + } +} diff --git a/src/Embed/Platforms/FacebookPost.php b/src/Embed/Platforms/FacebookPost.php new file mode 100644 index 0000000..35731ae --- /dev/null +++ b/src/Embed/Platforms/FacebookPost.php @@ -0,0 +1,50 @@ +url; + $sdk = FacebookHelper::sdkScript(); + + return << +HTML; + } +} diff --git a/src/Embed/Platforms/FacebookVideo.php b/src/Embed/Platforms/FacebookVideo.php new file mode 100644 index 0000000..1b49b6a --- /dev/null +++ b/src/Embed/Platforms/FacebookVideo.php @@ -0,0 +1,46 @@ +url; + return << +HTML; + } +} diff --git a/src/Embed/Platforms/Reddit.php b/src/Embed/Platforms/Reddit.php index c1bb944..105040c 100644 --- a/src/Embed/Platforms/Reddit.php +++ b/src/Embed/Platforms/Reddit.php @@ -11,11 +11,7 @@ public function regex() { return [ // oembed - "https://reddit.com/r/.*/comments/.*/.*", - "https://www.reddit.com/r/.*/comments/.*/.*", - - // custom - "https://old.reddit.com/r/.*/comments/.*/.*", // added: 2024-10-19 + "https://(?:(?:www|old)\.)?reddit.com/r/.*/comments/.*/.*", ]; } diff --git a/src/UnfoldConfig.php b/src/UnfoldConfig.php index bc7007a..2c36853 100644 --- a/src/UnfoldConfig.php +++ b/src/UnfoldConfig.php @@ -75,7 +75,7 @@ public function __construct( /** * Meta requires an access_token to access the OEmbed Read Graph API - * This is required for both Facebook & Instagram + * This is required for both FacebookPost & Instagram * @todo */ public ?string $facebookAccessToken = null, diff --git a/tests/Unit/Embed/EmbedTest.php b/tests/Unit/Embed/EmbedTest.php new file mode 100644 index 0000000..c543d6d --- /dev/null +++ b/tests/Unit/Embed/EmbedTest.php @@ -0,0 +1,14 @@ +toBeTrue(); +}); diff --git a/tests/Unit/EmbedParsers/OEmbedTest.php b/tests/Unit/Embed/OEmbedTest.php similarity index 100% rename from tests/Unit/EmbedParsers/OEmbedTest.php rename to tests/Unit/Embed/OEmbedTest.php diff --git a/tests/Unit/Embed/Platforms/FacebookPageTest.php b/tests/Unit/Embed/Platforms/FacebookPageTest.php new file mode 100644 index 0000000..d005327 --- /dev/null +++ b/tests/Unit/Embed/Platforms/FacebookPageTest.php @@ -0,0 +1,61 @@ +parse($parser->match()); +// var_dump($response->html); +// expect($response->html)->toBeString(); +//}); + +it('embeds facebook page', function () { + $url = 'https://www.facebook.com/geonarah'; + + $parser = new FacebookPage($url); + $match = $parser->match(); + $response = $parser->parse($match); + + $html = $response->html; + expect($html)->toContain('