forked from symfony/ux
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Enforce strict format for ID & name #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| { | ||
| 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); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'], | ||
| ['🙂'], | ||
| ]; | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#9