mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Moved code to convert a ShortUrl into a full link as string to the entity itself
This commit is contained in:
@@ -8,6 +8,7 @@ use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||
use Zend\Diactoros\Uri;
|
||||
|
||||
use function count;
|
||||
|
||||
@@ -135,7 +136,14 @@ class ShortUrl extends AbstractEntity
|
||||
return $this->maxVisits !== null && $this->getVisitsCount() >= $this->maxVisits;
|
||||
}
|
||||
|
||||
public function domain(string $fallback = ''): string
|
||||
public function toString(array $domainConfig): string
|
||||
{
|
||||
return (string) (new Uri())->withPath($this->shortCode)
|
||||
->withScheme($domainConfig['schema'] ?? 'http')
|
||||
->withHost($this->resolveDomain($domainConfig['hostname'] ?? ''));
|
||||
}
|
||||
|
||||
private function resolveDomain(string $fallback = ''): string
|
||||
{
|
||||
if ($this->domain === null) {
|
||||
return $fallback;
|
||||
|
||||
@@ -5,15 +5,12 @@ namespace Shlinkio\Shlink\Core\Transformer;
|
||||
|
||||
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Util\ShortUrlBuilderTrait;
|
||||
|
||||
use function Functional\invoke;
|
||||
use function Functional\invoke_if;
|
||||
|
||||
class ShortUrlDataTransformer implements DataTransformerInterface
|
||||
{
|
||||
use ShortUrlBuilderTrait;
|
||||
|
||||
/** @var array */
|
||||
private $domainConfig;
|
||||
|
||||
@@ -23,21 +20,20 @@ class ShortUrlDataTransformer implements DataTransformerInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ShortUrl $value
|
||||
* @param ShortUrl $shortUrl
|
||||
*/
|
||||
public function transform($value): array
|
||||
public function transform($shortUrl): array
|
||||
{
|
||||
$longUrl = $value->getLongUrl();
|
||||
$shortCode = $value->getShortCode();
|
||||
$longUrl = $shortUrl->getLongUrl();
|
||||
|
||||
return [
|
||||
'shortCode' => $shortCode,
|
||||
'shortUrl' => $this->buildShortUrl($this->domainConfig, $shortCode),
|
||||
'shortCode' => $shortUrl->getShortCode(),
|
||||
'shortUrl' => $shortUrl->toString($this->domainConfig),
|
||||
'longUrl' => $longUrl,
|
||||
'dateCreated' => $value->getDateCreated()->toAtomString(),
|
||||
'visitsCount' => $value->getVisitsCount(),
|
||||
'tags' => invoke($value->getTags(), '__toString'),
|
||||
'meta' => $this->buildMeta($value),
|
||||
'dateCreated' => $shortUrl->getDateCreated()->toAtomString(),
|
||||
'visitsCount' => $shortUrl->getVisitsCount(),
|
||||
'tags' => invoke($shortUrl->getTags(), '__toString'),
|
||||
'meta' => $this->buildMeta($shortUrl),
|
||||
|
||||
// Deprecated
|
||||
'originalUrl' => $longUrl,
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Util;
|
||||
|
||||
use Zend\Diactoros\Uri;
|
||||
|
||||
trait ShortUrlBuilderTrait
|
||||
{
|
||||
private function buildShortUrl(array $domainConfig, string $shortCode): string
|
||||
{
|
||||
return (string) (new Uri())->withPath($shortCode)
|
||||
->withScheme($domainConfig['schema'] ?? 'http')
|
||||
->withHost($domainConfig['hostname'] ?? '');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user