mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-09 16:53:11 +08:00
Migrated from standard datetime objects to chronos objects
This commit is contained in:
@@ -3,6 +3,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Entity;
|
||||
|
||||
use Cake\Chronos\Chronos;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
@@ -36,8 +37,8 @@ class ShortUrl extends AbstractEntity
|
||||
*/
|
||||
private $shortCode;
|
||||
/**
|
||||
* @var \DateTime
|
||||
* @ORM\Column(name="date_created", type="datetime")
|
||||
* @var Chronos
|
||||
* @ORM\Column(name="date_created", type="chronos_datetime")
|
||||
*/
|
||||
private $dateCreated;
|
||||
/**
|
||||
@@ -56,13 +57,13 @@ class ShortUrl extends AbstractEntity
|
||||
*/
|
||||
private $tags;
|
||||
/**
|
||||
* @var \DateTime
|
||||
* @ORM\Column(name="valid_since", type="datetime", nullable=true)
|
||||
* @var Chronos|null
|
||||
* @ORM\Column(name="valid_since", type="chronos_datetime", nullable=true)
|
||||
*/
|
||||
private $validSince;
|
||||
/**
|
||||
* @var \DateTime
|
||||
* @ORM\Column(name="valid_until", type="datetime", nullable=true)
|
||||
* @var Chronos|null
|
||||
* @ORM\Column(name="valid_until", type="chronos_datetime", nullable=true)
|
||||
*/
|
||||
private $validUntil;
|
||||
/**
|
||||
@@ -74,7 +75,7 @@ class ShortUrl extends AbstractEntity
|
||||
public function __construct()
|
||||
{
|
||||
$this->shortCode = '';
|
||||
$this->dateCreated = new \DateTime();
|
||||
$this->dateCreated = Chronos::now();
|
||||
$this->visits = new ArrayCollection();
|
||||
$this->tags = new ArrayCollection();
|
||||
}
|
||||
@@ -117,12 +118,12 @@ class ShortUrl extends AbstractEntity
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDateCreated(): \DateTime
|
||||
public function getDateCreated(): Chronos
|
||||
{
|
||||
return $this->dateCreated;
|
||||
}
|
||||
|
||||
public function setDateCreated(\DateTime $dateCreated): self
|
||||
public function setDateCreated(Chronos $dateCreated): self
|
||||
{
|
||||
$this->dateCreated = $dateCreated;
|
||||
return $this;
|
||||
@@ -151,23 +152,23 @@ class ShortUrl extends AbstractEntity
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getValidSince(): ?\DateTime
|
||||
public function getValidSince(): ?Chronos
|
||||
{
|
||||
return $this->validSince;
|
||||
}
|
||||
|
||||
public function setValidSince(?\DateTime $validSince): self
|
||||
public function setValidSince(?Chronos $validSince): self
|
||||
{
|
||||
$this->validSince = $validSince;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getValidUntil(): ?\DateTime
|
||||
public function getValidUntil(): ?Chronos
|
||||
{
|
||||
return $this->validUntil;
|
||||
}
|
||||
|
||||
public function setValidUntil(?\DateTime $validUntil): self
|
||||
public function setValidUntil(?Chronos $validUntil): self
|
||||
{
|
||||
$this->validUntil = $validUntil;
|
||||
return $this;
|
||||
|
||||
@@ -3,6 +3,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Entity;
|
||||
|
||||
use Cake\Chronos\Chronos;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
|
||||
use Shlinkio\Shlink\Common\Exception\WrongIpException;
|
||||
@@ -25,8 +26,8 @@ class Visit extends AbstractEntity implements \JsonSerializable
|
||||
*/
|
||||
private $referer;
|
||||
/**
|
||||
* @var \DateTime
|
||||
* @ORM\Column(type="datetime", nullable=false)
|
||||
* @var Chronos
|
||||
* @ORM\Column(type="chronos_datetime", nullable=false)
|
||||
*/
|
||||
private $date;
|
||||
/**
|
||||
@@ -54,7 +55,7 @@ class Visit extends AbstractEntity implements \JsonSerializable
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->date = new \DateTime();
|
||||
$this->date = Chronos::now();
|
||||
}
|
||||
|
||||
public function getReferer(): string
|
||||
@@ -68,12 +69,12 @@ class Visit extends AbstractEntity implements \JsonSerializable
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDate(): \DateTime
|
||||
public function getDate(): Chronos
|
||||
{
|
||||
return $this->date;
|
||||
}
|
||||
|
||||
public function setDate(\DateTime $date): self
|
||||
public function setDate(Chronos $date): self
|
||||
{
|
||||
$this->date = $date;
|
||||
return $this;
|
||||
@@ -148,7 +149,7 @@ class Visit extends AbstractEntity implements \JsonSerializable
|
||||
{
|
||||
return [
|
||||
'referer' => $this->referer,
|
||||
'date' => isset($this->date) ? $this->date->format(\DateTime::ATOM) : null,
|
||||
'date' => isset($this->date) ? $this->date->toAtomString() : null,
|
||||
'userAgent' => $this->userAgent,
|
||||
'visitLocation' => $this->visitLocation,
|
||||
|
||||
|
||||
@@ -3,17 +3,19 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Model;
|
||||
|
||||
use Cake\Chronos\Chronos;
|
||||
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
use function is_string;
|
||||
|
||||
final class ShortUrlMeta
|
||||
{
|
||||
/**
|
||||
* @var \DateTime|null
|
||||
* @var Chronos|null
|
||||
*/
|
||||
private $validSince;
|
||||
/**
|
||||
* @var \DateTime|null
|
||||
* @var Chronos|null
|
||||
*/
|
||||
private $validUntil;
|
||||
/**
|
||||
@@ -43,8 +45,8 @@ final class ShortUrlMeta
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|\DateTimeInterface|null $validSince
|
||||
* @param string|\DateTimeInterface|null $validUntil
|
||||
* @param string|Chronos|null $validSince
|
||||
* @param string|Chronos|null $validUntil
|
||||
* @param string|null $customSlug
|
||||
* @param int|null $maxVisits
|
||||
* @return ShortUrlMeta
|
||||
@@ -86,26 +88,23 @@ final class ShortUrlMeta
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|\DateTime|null $date
|
||||
* @return \DateTime|null
|
||||
* @param string|Chronos|null $date
|
||||
* @return Chronos|null
|
||||
*/
|
||||
private function parseDateField($date): ?\DateTime
|
||||
private function parseDateField($date): ?Chronos
|
||||
{
|
||||
if ($date === null || $date instanceof \DateTime) {
|
||||
if ($date === null || $date instanceof Chronos) {
|
||||
return $date;
|
||||
}
|
||||
|
||||
if (\is_string($date)) {
|
||||
return new \DateTime($date);
|
||||
if (is_string($date)) {
|
||||
return Chronos::parse($date);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTime|null
|
||||
*/
|
||||
public function getValidSince(): ?\DateTime
|
||||
public function getValidSince(): ?Chronos
|
||||
{
|
||||
return $this->validSince;
|
||||
}
|
||||
@@ -115,10 +114,7 @@ final class ShortUrlMeta
|
||||
return $this->validSince !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTime|null
|
||||
*/
|
||||
public function getValidUntil(): ?\DateTime
|
||||
public function getValidUntil(): ?Chronos
|
||||
{
|
||||
return $this->validUntil;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Repository;
|
||||
|
||||
use Cake\Chronos\Chronos;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
@@ -131,7 +132,7 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
|
||||
*/
|
||||
public function findOneByShortCode(string $shortCode): ?ShortUrl
|
||||
{
|
||||
$now = new \DateTimeImmutable();
|
||||
$now = Chronos::now();
|
||||
|
||||
$qb = $this->createQueryBuilder('s');
|
||||
$qb->where($qb->expr()->eq('s.shortCode', ':shortCode'))
|
||||
|
||||
@@ -3,6 +3,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Service;
|
||||
|
||||
use Cake\Chronos\Chronos;
|
||||
use Cocur\Slugify\Slugify;
|
||||
use Cocur\Slugify\SlugifyInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
@@ -61,14 +62,6 @@ class UrlShortener implements UrlShortenerInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and persists a unique shortcode generated for provided url
|
||||
*
|
||||
* @param UriInterface $url
|
||||
* @param string[] $tags
|
||||
* @param \DateTime|null $validSince
|
||||
* @param \DateTime|null $validUntil
|
||||
* @param string|null $customSlug
|
||||
* @param int|null $maxVisits
|
||||
* @throws NonUniqueSlugException
|
||||
* @throws InvalidUrlException
|
||||
* @throws RuntimeException
|
||||
@@ -76,10 +69,10 @@ class UrlShortener implements UrlShortenerInterface
|
||||
public function urlToShortCode(
|
||||
UriInterface $url,
|
||||
array $tags = [],
|
||||
\DateTime $validSince = null,
|
||||
\DateTime $validUntil = null,
|
||||
string $customSlug = null,
|
||||
int $maxVisits = null
|
||||
?Chronos $validSince = null,
|
||||
?Chronos $validUntil = null,
|
||||
?string $customSlug = null,
|
||||
?int $maxVisits = null
|
||||
): ShortUrl {
|
||||
// If the URL validation is enabled, check that the URL actually exists
|
||||
if ($this->urlValidationEnabled) {
|
||||
|
||||
@@ -3,6 +3,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Service;
|
||||
|
||||
use Cake\Chronos\Chronos;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
||||
@@ -14,14 +15,6 @@ use Shlinkio\Shlink\Core\Exception\RuntimeException;
|
||||
interface UrlShortenerInterface
|
||||
{
|
||||
/**
|
||||
* Creates and persists a unique shortcode generated for provided url
|
||||
*
|
||||
* @param UriInterface $url
|
||||
* @param string[] $tags
|
||||
* @param \DateTime|null $validSince
|
||||
* @param \DateTime|null $validUntil
|
||||
* @param string|null $customSlug
|
||||
* @param int|null $maxVisits
|
||||
* @throws NonUniqueSlugException
|
||||
* @throws InvalidUrlException
|
||||
* @throws RuntimeException
|
||||
@@ -29,10 +22,10 @@ interface UrlShortenerInterface
|
||||
public function urlToShortCode(
|
||||
UriInterface $url,
|
||||
array $tags = [],
|
||||
\DateTime $validSince = null,
|
||||
\DateTime $validUntil = null,
|
||||
string $customSlug = null,
|
||||
int $maxVisits = null
|
||||
?Chronos $validSince = null,
|
||||
?Chronos $validUntil = null,
|
||||
?string $customSlug = null,
|
||||
?int $maxVisits = null
|
||||
): ShortUrl;
|
||||
|
||||
/**
|
||||
|
||||
@@ -24,7 +24,6 @@ class ShortUrlDataTransformer implements DataTransformerInterface
|
||||
|
||||
/**
|
||||
* @param ShortUrl $value
|
||||
* @return array
|
||||
*/
|
||||
public function transform($value): array
|
||||
{
|
||||
@@ -36,7 +35,7 @@ class ShortUrlDataTransformer implements DataTransformerInterface
|
||||
'shortCode' => $shortCode,
|
||||
'shortUrl' => $this->buildShortUrl($this->domainConfig, $shortCode),
|
||||
'longUrl' => $longUrl,
|
||||
'dateCreated' => $dateCreated !== null ? $dateCreated->format(\DateTime::ATOM) : null,
|
||||
'dateCreated' => $dateCreated !== null ? $dateCreated->toAtomString() : null,
|
||||
'visitsCount' => $value->getVisitsCount(),
|
||||
'tags' => \array_map([$this, 'serializeTag'], $value->getTags()->toArray()),
|
||||
|
||||
|
||||
@@ -12,12 +12,12 @@ class ShortUrlMetaInputFilter extends InputFilter
|
||||
{
|
||||
use InputFactoryTrait;
|
||||
|
||||
const VALID_SINCE = 'validSince';
|
||||
const VALID_UNTIL = 'validUntil';
|
||||
const CUSTOM_SLUG = 'customSlug';
|
||||
const MAX_VISITS = 'maxVisits';
|
||||
public const VALID_SINCE = 'validSince';
|
||||
public const VALID_UNTIL = 'validUntil';
|
||||
public const CUSTOM_SLUG = 'customSlug';
|
||||
public const MAX_VISITS = 'maxVisits';
|
||||
|
||||
public function __construct(array $data = null)
|
||||
public function __construct(?array $data = null)
|
||||
{
|
||||
$this->initialize();
|
||||
if ($data !== null) {
|
||||
@@ -25,7 +25,7 @@ class ShortUrlMetaInputFilter extends InputFilter
|
||||
}
|
||||
}
|
||||
|
||||
private function initialize()
|
||||
private function initialize(): void
|
||||
{
|
||||
$validSince = $this->createInput(self::VALID_SINCE, false);
|
||||
$validSince->getValidatorChain()->attach(new Date(['format' => \DateTime::ATOM]));
|
||||
|
||||
Reference in New Issue
Block a user