diff --git a/module/Core/src/Util/TagManagerTrait.php b/module/Core/src/Util/TagManagerTrait.php index c6258f91..9fac8700 100644 --- a/module/Core/src/Util/TagManagerTrait.php +++ b/module/Core/src/Util/TagManagerTrait.php @@ -7,11 +7,9 @@ namespace Shlinkio\Shlink\Core\Util; use Doctrine\Common\Collections; use Doctrine\ORM\EntityManagerInterface; use Shlinkio\Shlink\Core\Entity\Tag; +use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter; use function Functional\map; -use function str_replace; -use function strtolower; -use function trim; /** @deprecated */ trait TagManagerTrait @@ -23,8 +21,11 @@ trait TagManagerTrait */ private function tagNamesToEntities(EntityManagerInterface $em, array $tags): Collections\Collection { - $entities = map($tags, function (string $tagName) use ($em) { - $tagName = $this->normalizeTagName($tagName); + $normalizedTags = ShortUrlInputFilter::withNonRequiredLongUrl([ + ShortUrlInputFilter::TAGS => $tags, + ])->getValue(ShortUrlInputFilter::TAGS); + + $entities = map($normalizedTags, function (string $tagName) use ($em) { $tag = $em->getRepository(Tag::class)->findOneBy(['name' => $tagName]) ?? new Tag($tagName); $em->persist($tag); @@ -33,9 +34,4 @@ trait TagManagerTrait return new Collections\ArrayCollection($entities); } - - private function normalizeTagName(string $tagName): string - { - return str_replace(' ', '-', strtolower(trim($tagName))); - } } diff --git a/module/Core/test/Service/UrlShortenerTest.php b/module/Core/test/Service/UrlShortenerTest.php index caa224b6..ed038141 100644 --- a/module/Core/test/Service/UrlShortenerTest.php +++ b/module/Core/test/Service/UrlShortenerTest.php @@ -119,7 +119,7 @@ class UrlShortenerTest extends TestCase ), ShortUrl::withLongUrl($url)]; yield [ ShortUrlMeta::fromRawData(['findIfExists' => true, 'longUrl' => $url, 'tags' => ['foo', 'bar']]), - ShortUrl::withLongUrl($url)->setTags(new ArrayCollection([new Tag('bar'), new Tag('foo')])), + ShortUrl::fromMeta(ShortUrlMeta::fromRawData(['longUrl' => $url, 'tags' => ['foo', 'bar']])), ]; yield [ ShortUrlMeta::fromRawData(['findIfExists' => true, 'maxVisits' => 3, 'longUrl' => $url]), @@ -157,7 +157,8 @@ class UrlShortenerTest extends TestCase 'validUntil' => Chronos::parse('2017-01-01'), 'maxVisits' => 4, 'longUrl' => $url, - ]))->setTags(new ArrayCollection([new Tag('foo'), new Tag('bar'), new Tag('baz')])), + 'tags' => ['foo', 'bar', 'baz'], + ])), ]; } }