mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-11 09:43:13 +08:00
Updated short URL creation responses to include more information
This commit is contained in:
@@ -81,7 +81,7 @@ class UrlShortener implements UrlShortenerInterface
|
||||
\DateTime $validUntil = null,
|
||||
string $customSlug = null,
|
||||
int $maxVisits = null
|
||||
): string {
|
||||
): ShortUrl {
|
||||
// If the URL validation is enabled, check that the URL actually exists
|
||||
if ($this->urlValidationEnabled) {
|
||||
$this->checkUrlExists($url);
|
||||
@@ -108,7 +108,7 @@ class UrlShortener implements UrlShortenerInterface
|
||||
$this->em->flush();
|
||||
|
||||
$this->em->commit();
|
||||
return $shortCode;
|
||||
return $shortUrl;
|
||||
} catch (\Throwable $e) {
|
||||
if ($this->em->getConnection()->isTransactionActive()) {
|
||||
$this->em->rollback();
|
||||
|
||||
@@ -33,7 +33,7 @@ interface UrlShortenerInterface
|
||||
\DateTime $validUntil = null,
|
||||
string $customSlug = null,
|
||||
int $maxVisits = null
|
||||
): string;
|
||||
): ShortUrl;
|
||||
|
||||
/**
|
||||
* Tries to find the mapped URL for provided short code. Returns null if not found
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\Transformer;
|
||||
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Entity\Tag;
|
||||
use Zend\Diactoros\Uri;
|
||||
|
||||
class ShortUrlDataTransformer implements DataTransformerInterface
|
||||
{
|
||||
@@ -31,12 +32,7 @@ class ShortUrlDataTransformer implements DataTransformerInterface
|
||||
|
||||
return [
|
||||
'shortCode' => $shortCode,
|
||||
'shortUrl' => \sprintf(
|
||||
'%s://%s/%s',
|
||||
$this->domainConfig['schema'] ?? 'http',
|
||||
$this->domainConfig['hostname'] ?? '',
|
||||
$shortCode
|
||||
),
|
||||
'shortUrl' => $this->buildShortUrl($shortCode),
|
||||
'longUrl' => $longUrl,
|
||||
'dateCreated' => $dateCreated !== null ? $dateCreated->format(\DateTime::ATOM) : null,
|
||||
'visitsCount' => $value->getVisitsCount(),
|
||||
@@ -47,6 +43,13 @@ class ShortUrlDataTransformer implements DataTransformerInterface
|
||||
];
|
||||
}
|
||||
|
||||
private function buildShortUrl(string $shortCode): string
|
||||
{
|
||||
return (string) (new Uri())->withPath($shortCode)
|
||||
->withScheme($this->domainConfig['schema'] ?? 'http')
|
||||
->withHost($this->domainConfig['hostname'] ?? '');
|
||||
}
|
||||
|
||||
private function serializeTag(Tag $tag): string
|
||||
{
|
||||
return $tag->getName();
|
||||
|
||||
Reference in New Issue
Block a user