From 1cae6d3905f0fae1fac1fc2e13a25f78e125ca33 Mon Sep 17 00:00:00 2001 From: Ben van Hartingsveldt Date: Wed, 2 Oct 2024 15:23:18 +0200 Subject: [PATCH] Add examples --- README.md | 24 ++++++++++++++++++++++-- src/OEmbed.php | 3 +++ tests/OEmbedTest.php | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cba1e80..df11bed 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,28 @@ This yocLibrary enables your project to encode and decode OEmbed data in PHP. ### Encoding -TODO +```php +use YOCLIB\OEmbed\OEmbed; + +$data = [ + 'version' => '1.0', +]; + +$json = OEmbed::encode($data,'json'); +// or +$xml = OEmbed::encode($data,); +``` ### Decoding -TODO \ No newline at end of file +```php +use YOCLIB\OEmbed\OEmbed; + +$json = '{"version":"1.0"}'; +$data = OEmbed::decode($json,'json'); + +// or + +$xml = '1.0'; +$data = OEmbed::decode($xml); +``` \ No newline at end of file diff --git a/src/OEmbed.php b/src/OEmbed.php index 9deec28..925529a 100644 --- a/src/OEmbed.php +++ b/src/OEmbed.php @@ -74,6 +74,9 @@ public static function encodeJSON(array $data): ?string{ public static function encodeXML(array $data): ?string{ self::ensureValidData($data); $doc = new DOMDocument; + $doc->encoding = 'UTF-8'; + $doc->xmlStandalone = true; + $doc->xmlVersion = '1.0'; $doc->append($doc->createElement('oembed')); $arr = (array) $data; foreach($arr as $key=>$val){ diff --git a/tests/OEmbedTest.php b/tests/OEmbedTest.php index 61bc9af..f100cf4 100644 --- a/tests/OEmbedTest.php +++ b/tests/OEmbedTest.php @@ -32,7 +32,7 @@ public function testEncode(){ 'width' => 456, ]; $json = '{"version":"1.0","type":"photo","height":123,"width":456}'; - $xml = "\n1.0photo123456\n"; + $xml = "\n1.0photo123456\n"; self::assertEquals($json,OEmbed::encode($data,'json')); self::assertEquals($xml,OEmbed::encode($data));