Skip to content

Commit

Permalink
Merge pull request #41 from hyvor/php-docs
Browse files Browse the repository at this point in the history
php docs
  • Loading branch information
supun-io authored Oct 30, 2024
2 parents efec109 + cf8c38e commit 92126d0
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/Unfolded/UnfoldedLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public function __construct(
public string $lastUrl,
public ?string $title,
public ?string $description,
public array $authors,
public array $tags,
public ?string $siteName,
public ?string $siteUrl,
public ?string $canonicalUrl,
Expand All @@ -31,6 +29,8 @@ public function __construct(
public ?string $thumbnailUrl,
public ?string $iconUrl,
public ?string $locale,
public array $authors,
public array $tags,
public int $durationMs
) {
}
Expand All @@ -46,17 +46,15 @@ public static function fromMetadata(
$metadataPriority = new MetadataPriority($metadata);

$currentUrl = $config->url;
if ($lastRequest && (string)$lastRequest->getUri() !== $currentUrl) {
$currentUrl = (string)$lastRequest->getUri();
if ($lastRequest && (string) $lastRequest->getUri() !== $currentUrl) {
$currentUrl = (string) $lastRequest->getUri();
}

return new self(
$config->url,
$currentUrl,
$metadataPriority->title(),
$metadataPriority->description(),
$metadataPriority->authors(),
$metadataPriority->tags(),
$metadataPriority->siteName(),
$metadataPriority->siteUrl($currentUrl),
$metadataPriority->canonicalUrl(),
Expand All @@ -65,6 +63,8 @@ public static function fromMetadata(
$metadataPriority->thumbnailUrl(),
$metadataPriority->iconUrl(),
$metadataPriority->locale(),
$metadataPriority->authors(),
$metadataPriority->tags(),
$config->duration()
);
}
Expand Down
126 changes: 126 additions & 0 deletions website/src/routes/[[slug]]/Php.svelte
Original file line number Diff line number Diff line change
@@ -1 +1,127 @@
<script>
import { CodeBlock } from '@hyvor/design/components';
</script>

<h1>PHP Library</h1>

<p>Install the PHP library using composer:</p>

<CodeBlock code="composer require hyvor/unfold" />

<h2 id="emeds">Embeds</h2>

<p>
Call <code>Unfold::embed()</code> to get the embed code for a URL of a
<a href="/#embed-platforms">supported platform</a>. This returns an <code>UnfoldedEmbed</code> object.
</p>

<CodeBlock
code={`
<?php
use Hyvor\\Unfold\\Unfold;
use Hyvor\\Unfold\\Exception\\EmbedUnableToResolveException;
use Hyvor\\Unfold\\Exception\\UnfoldException;
try {
$embed = Unfold::embed('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
$embed->embed; // html code to embed
$embed->durationMs; // duration to resolve the embed in milliseconds
} catch (EmbedUnableToResolveException) {
// This URL did not match any of the supported platforms
} catch (UnfoldException $e) {
// any other possible error
// $e->getMessage();
}
`}
language="js"
/>

<h2 id="link-previews">Link Previews</h2>

<p>
Call <code>Unfold::link()</code> to get metadata of a URL. This returns an
<code>UnfoldedLink</code> object.
</p>

<CodeBlock
code={`
<?php
use Hyvor\\Unfold\\Unfold;
use Hyvor\\Unfold\\Exception\\UnfoldException;
use Hyvor\\Unfold\\Exception\\LinkScrapeException;
try {
$link = Unfold::link('https://hyvor.com');
$link->title;
$link->description;
$link->siteName;
$link->siteUrl;
$link->canonicalUrl;
$link->publishedTime;
$link->modifiedTime;
$link->thumbnailUrl;
$link->iconUrl;
$link->locale;
$link->authors;
$link->tags;
$link->durationMs;
} catch (LinkScrapeException $e) {
// This URL could not be scraped due to non-200 status code or other reasons
// $e->getMessage(); returns more details including the HTTP status
} catch (UnfoldException $e) {
// any other possible error
// $e->getMessage();
}
`}
language="js"
/>

<h2 id="config">Config</h2>

<p>
Both <code>embed()</code> and <code>link()</code> methods accept an optional second parameter that
accepts a <code>Hyvor\Unfold\UnfoldConfig</code>.
</p>

<p>
Here is an example with all the available configurations (feel free to read the comments in the
code for more details):
</p>

<CodeBlock
code={`
use Hyvor\\Unfold\\Unfold;
use Hyvor\\Unfold\\UnfoldConfig;
$url = 'https://hyvor.com';
$config = new UnfoldConfig(
httpClient: new MyCustomPsr18Client(),
httpMaxRedirects: 5,
httpUserAgent: 'My Custom User Agent',
);
$link = Unfold::link($url, $config);
`}
language="js"
/>

<h3 id="psr-18">PSR-18</h3>

<p>
This library does not include an HTTP Client. Instead, it uses <code>php-http/discovery</code> to
find a PSR-18 HTTP Client installed in your project. If you want to use a custom HTTP Client, you
can pass it as the <code>httpClient</code> parameter in the <code>UnfoldConfig</code> object.
</p>

<p>
Currently, an HTTP Client is only required for the <code>link()</code> method. The
<code>embed()</code> method depends on regex to parse URLs into embeds.
</p>
4 changes: 2 additions & 2 deletions website/src/routes/demo/DemoLink.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
}
.left {
padding: 15px 20px;
flex: 1;
flex: 3;
flex-shrink: 0;
}
.title {
Expand All @@ -105,7 +105,7 @@
margin-top: 6px;
}
.right {
flex: 1;
flex: 2;
flex-shrink: 0;
position: relative;
}
Expand Down

0 comments on commit 92126d0

Please sign in to comment.