Skip to content

Commit

Permalink
Add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed Oct 2, 2024
1 parent 3ea1d10 commit 1cae6d3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```php
use YOCLIB\OEmbed\OEmbed;

$json = '{"version":"1.0"}';
$data = OEmbed::decode($json,'json');

// or

$xml = '<oembed><version>1.0</version></oembed>';
$data = OEmbed::decode($xml);
```
3 changes: 3 additions & 0 deletions src/OEmbed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
2 changes: 1 addition & 1 deletion tests/OEmbedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testEncode(){
'width' => 456,
];
$json = '{"version":"1.0","type":"photo","height":123,"width":456}';
$xml = "<?xml version=\"1.0\"?>\n<oembed><version>1.0</version><type>photo</type><height>123</height><width>456</width></oembed>\n";
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<oembed><version>1.0</version><type>photo</type><height>123</height><width>456</width></oembed>\n";

self::assertEquals($json,OEmbed::encode($data,'json'));
self::assertEquals($xml,OEmbed::encode($data));
Expand Down

0 comments on commit 1cae6d3

Please sign in to comment.