forked from symfony/ux
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Icon object #9
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
Merged
kbond
merged 6 commits into
kbond:feat/ux-icons-draft-2
from
smnandre:feat/icon-object-2
Feb 3, 2024
Merged
Icon object #9
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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,185 @@ | ||
| <?php | ||
|
|
||
| namespace Symfony\UX\Icons\Svg; | ||
|
|
||
| /** | ||
| * | ||
| * @author Simon André <smn.andre@gmail.com> | ||
| * | ||
| * @internal | ||
| */ | ||
| final class Icon implements \Stringable, \Serializable, \ArrayAccess | ||
| { | ||
| /** | ||
| * 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); | ||
| } | ||
|
|
||
| public function __construct( | ||
| private readonly string $innerSvg, | ||
| private readonly array $attributes = [], | ||
| ) | ||
| { | ||
| // @todo validate attributes (?) | ||
| // the main idea is to have a way to validate the attributes | ||
| // before the icon is cached to improve performances | ||
| // (avoiding to validate the attributes each time the icon is rendered) | ||
| } | ||
|
|
||
| public function toHtml(): string | ||
| { | ||
| $htmlAttributes = ''; | ||
| foreach ($this->attributes as $name => $value) { | ||
| if (false === $value) { | ||
| continue; | ||
| } | ||
| $htmlAttributes .= ' '.$name; | ||
| if (true !== $value) { | ||
| $value = htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); | ||
| $htmlAttributes .= '="'. $value .'"'; | ||
| } | ||
| } | ||
|
|
||
| return '<svg'.$htmlAttributes.'>'.$this->innerSvg.'</svg>'; | ||
| } | ||
|
|
||
| public function getInnerSvg(): string | ||
| { | ||
| return $this->innerSvg; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, string|bool> | ||
| */ | ||
| public function getAttributes(): array | ||
| { | ||
| return $this->attributes; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, string|bool> $attributes | ||
| * @return self | ||
| */ | ||
| public function withAttributes(array $attributes): self | ||
| { | ||
| foreach ($attributes as $name => $value) { | ||
| if (!is_string($name)) { | ||
| throw new \InvalidArgumentException(sprintf('Attribute names must be string, "%s" given.', get_debug_type($name))); | ||
| } | ||
| // @todo regexp would be better ? | ||
| if (!ctype_alnum($name) && !str_contains($name, '-')) { | ||
| throw new \InvalidArgumentException(sprintf('Invalid attribute name "%s".', $name)); | ||
| } | ||
| if (!is_string($value) && !is_bool($value)) { | ||
| throw new \InvalidArgumentException(sprintf('Invalid value type for attribute "%s". Boolean or string allowed, "%s" provided. ', $name, get_debug_type($value))); | ||
| } | ||
| } | ||
|
|
||
| return new self($this->innerSvg, [...$this->attributes, ...$attributes]); | ||
| } | ||
|
|
||
| public function withInnerSvg(string $innerSvg): self | ||
| { | ||
| // @todo validate svg ? | ||
| // The main idea is to not validate the attributes for every icon | ||
| // when they come from a pack (and thus share a set of attributes) | ||
|
|
||
| return new self($innerSvg, $this->attributes); | ||
| } | ||
|
|
||
| public function __toString(): string | ||
| { | ||
| return $this->toHtml(); | ||
| } | ||
|
|
||
| public function serialize(): string | ||
| { | ||
| return serialize([$this->innerSvg, $this->attributes]); | ||
| } | ||
|
|
||
| public function unserialize(string $data): void | ||
| { | ||
| [$this->innerSvg, $this->attributes] = unserialize($data); | ||
| } | ||
|
|
||
| public function __serialize(): array | ||
| { | ||
| return [$this->innerSvg, $this->attributes]; | ||
| } | ||
|
|
||
| public function __unserialize(array $data): void | ||
| { | ||
| [$this->innerSvg, $this->attributes] = $data; | ||
| } | ||
|
|
||
| public function offsetExists(mixed $offset): bool | ||
| { | ||
| return isset($this->attributes[$offset]); | ||
| } | ||
|
|
||
| public function offsetGet(mixed $offset): mixed | ||
| { | ||
| return $this->attributes[$offset]; | ||
| } | ||
|
|
||
| public function offsetSet(mixed $offset, mixed $value): void | ||
| { | ||
| throw new \LogicException('The Icon object is immutable.'); | ||
| } | ||
|
|
||
| public function offsetUnset(mixed $offset): void | ||
| { | ||
| throw new \LogicException('The Icon object is immutable.'); | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.
we need to actually use these new methods now, right?
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.
Of course, i try to do micro step by micro step because the rebase dance is not my first talent :)
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.
Better now :)