Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/Icons/src/Svg/SvgUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Symfony\UX\Icons\Svg;

final class SvgUtils
{
/**
* Transforms a valid icon ID into an icon name.
*
* @throws \InvalidArgumentException if the ID is not valid
* @see isValidId()
*/
public static function idToName(string $id): string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could these be added to the Icon object in #7?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, then combine this PR with an updated #7.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do in a moment !

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#9

{
if (!self::isValidId($id)) {
throw new \InvalidArgumentException(sprintf('The id "%s" is not a valid id.', $id));
}

return str_replace('--', ':', $id);
}

/**
* Transforms a valid icon name into an ID.
*
* @throws \InvalidArgumentException if the name is not valid
* @see isValidName()
*/
public static function nameToId(string $name): string
{
if (!self::isValidName($name)) {
throw new \InvalidArgumentException(sprintf('The name "%s" is not a valid name.', $name));
}

return str_replace(':', '--', $name);
}

/**
* Returns whether the given string is a valid icon ID.
*
* An icon ID is a string that contains only lowercase letters, numbers, and hyphens.
* It must be composed of slugs separated by double hyphens.
*
* @see https://regex101.com/r/mmvl5t/1
*/
public static function isValidId(string $id): bool
{
return (bool) preg_match('#^([a-z0-9]+(-[a-z0-9]+)*)(--[a-z0-9]+(-[a-z0-9]+)*)*$#', $id);
}

/**
* Returns whether the given string is a valid icon name.
*
* An icon name is a string that contains only lowercase letters, numbers, and hyphens.
* It must be composed of slugs separated by colons.
*
* @see https://regex101.com/r/Gh2Z9s/1
*/
public static function isValidName(string $name): bool
{
return (bool) preg_match('#^([a-z0-9]+(-[a-z0-9]+)*)(:[a-z0-9]+(-[a-z0-9]+)*)*$#', $name);
}
}
191 changes: 191 additions & 0 deletions src/Icons/tests/Unit/Svg/SvgUtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php

namespace Symfony\UX\Icons\Tests\Unit\Svg;

use PHPUnit\Framework\TestCase;
use Symfony\UX\Icons\Svg\SvgUtils;

class SvgUtilsTest extends TestCase
{
/**
* @dataProvider provideIdToName
*/
public function testIdToName(string $id, string $name)
{
$this->assertSame($name, SvgUtils::idToName($id));
}

/**
* @dataProvider provideInvalidIds
*/
public function testIdToNameThrowsException(string $id)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The id "'.$id.'" is not a valid id.');

SvgUtils::idToName($id);
}

/**
* @dataProvider provideNameToId
*/
public function testNameToId(string $name, string $id)
{
$this->assertEquals($id, SvgUtils::nameToId($name));
}

/**
* @dataProvider provideInvalidNames
*/
public function testNameToIdThrowsException(string $name)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The name "'.$name.'" is not a valid name.');

SvgUtils::nameToId($name);
}

/**
* @dataProvider provideValidIds
*/
public function testIsValidIdWithValidIds(string $id): void
{
$this->assertTrue(SvgUtils::isValidId($id));
}

/**
* @dataProvider provideInvalidIds
*/
public function testIsValidIdWithInvalidIds(string $id): void
{
$this->assertFalse(SvgUtils::isValidId($id));
}

/**
* @dataProvider provideValidNames
*/
public function testIsValidNameWithValidNames(string $name): void
{
$this->assertTrue(SvgUtils::isValidName($name));
}

/**
* @dataProvider provideInvalidNames
*/
public function testIsValidNameWithInvalidNames(string $name): void
{
$this->assertFalse(SvgUtils::isValidName($name));
}

/**
* @dataProvider provideInvalidIds
*/
public function testInvalidIdToName(string $id)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The id "'.$id.'" is not a valid id.');

$this->assertFalse(SvgUtils::isValidId($id));
SvgUtils::idToName($id);
}

public static function provideIdToName(): iterable
{
yield from [
['foo', 'foo'],
['foo-bar', 'foo-bar'],
['foo-bar-baz', 'foo-bar-baz'],
['foo--bar', 'foo:bar'],
['foo--bar--baz', 'foo:bar:baz'],
['foo-bar--baz', 'foo-bar:baz'],
['foo--bar-baz', 'foo:bar-baz'],
];
}

public static function provideNameToId(): iterable
{
yield from [
['foo', 'foo'],
['foo-bar', 'foo-bar'],
['foo-bar-baz', 'foo-bar-baz'],
['foo:bar', 'foo--bar'],
['foo:bar:baz', 'foo--bar--baz'],
['foo-bar:baz', 'foo-bar--baz'],
['foo:bar-baz', 'foo--bar-baz'],
];
}

public static function provideValidIds(): iterable
{
yield from self::provideValidIdentifiers();
yield from [
['foo--bar'],
['foo--bar-baz'],
['foo-bar--baz'],
];
}

public static function provideInvalidIds(): iterable
{
yield from self::provideInvalidIdentifiers();
yield from [
['foo:'],
[':foo'],
['foo::bar'],
];
}

public static function provideValidNames(): iterable
{
yield from self::provideValidIdentifiers();
yield from [
['foo:bar-baz'],
['foo:bar'],
];
}

public static function provideInvalidNames(): iterable
{
yield from self::provideInvalidIdentifiers();
yield from [
['foo:'],
[':foo'],
['foo::bar'],
['foo:::bar'],
['foo::'],
['::foo'],
['foo--bar'],
];
}

private static function provideValidIdentifiers(): iterable
{
yield from [
['foo'],
['123'],
['foo-bar'],
['123-456'],
];
}

private static function provideInvalidIdentifiers(): iterable
{
yield from [
[''],
['FOO'],
['&'],
['é'],
['.'],
['/'],
['foo-'],
['-bar'],
['_'],
['foo_bar'],
[' '],
['foo '],
[' foo'],
['🙂'],
];
}

}