-
-
Notifications
You must be signed in to change notification settings - Fork 368
[TwigComponent]: Add the ability to spread attributes from the ...
attribute
#2923
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
base: 2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,16 +26,22 @@ final class ComponentAttributes implements \Stringable, \IteratorAggregate, \Cou | |
private const ALPINE_REGEX = '#^x-([a-z]+):[^:]+$#'; | ||
private const VUE_REGEX = '#^v-([a-z]+):[^:]+$#'; | ||
|
||
private const SPREADABLE_ATTRIBUTE = '...'; | ||
|
||
/** @var array<string, string|bool> */ | ||
private array $attributes = []; | ||
|
||
/** @var array<string,true> */ | ||
private array $rendered = []; | ||
|
||
/** | ||
* @param array<string, string|bool> $attributes | ||
*/ | ||
public function __construct( | ||
private array $attributes, | ||
array $attributes, | ||
private readonly EscaperRuntime $escaper, | ||
) { | ||
$this->attributes = $this->handleAttributes($attributes); | ||
} | ||
|
||
public function __toString(): string | ||
|
@@ -103,6 +109,31 @@ public function __clone(): void | |
$this->rendered = []; | ||
} | ||
|
||
private function handleAttributes(array $attributes): array | ||
{ | ||
$spreadAttributes = $attributes[self::SPREADABLE_ATTRIBUTE] ?? null; | ||
|
||
if (!$spreadAttributes) { | ||
return $attributes; | ||
} | ||
|
||
if ($spreadAttributes instanceof StimulusAttributes) { | ||
$spreadAttributes = $spreadAttributes->toArray(); | ||
} | ||
|
||
if ($spreadAttributes instanceof \Traversable) { | ||
$spreadAttributes = iterator_to_array($spreadAttributes); | ||
} | ||
|
||
if (!\is_array($spreadAttributes)) { | ||
throw new \InvalidArgumentException(\sprintf('The "%s" attribute must be an array, "%s" given.', self::SPREADABLE_ATTRIBUTE, get_debug_type($spreadAttributes))); | ||
} | ||
|
||
unset($attributes[self::SPREADABLE_ATTRIBUTE]); | ||
|
||
return [...$attributes, ...$spreadAttributes]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is something that can be done userland, no ? Wether in PHP or Twig There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean ? The interest is to be recursive. Currently, for the "primary" attributes it's not that usefull but for nested attributes, we need that |
||
} | ||
|
||
public function render(string $attribute): ?string | ||
{ | ||
if (null === $value = $this->attributes[$attribute] ?? null) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,12 +113,6 @@ public function mountFromObject(object $component, array $data, ComponentMetadat | |
$attributes = $data[$attributesVar] ?? []; | ||
unset($data[$attributesVar]); | ||
|
||
foreach ($data as $key => $value) { | ||
if ($value instanceof \Stringable) { | ||
$data[$key] = (string) $value; | ||
} | ||
} | ||
Comment on lines
-116
to
-120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this removal ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because this loop force attributes to be strings if they are Stringable. However, StimulusAttributes implements Stringable so I can't pass recursively the object in the ComponentAttributes. Maybe I can exclude StimulusAttributes like this : |
||
|
||
return new MountedComponent( | ||
$componentMetadata->getName(), | ||
$component, | ||
|
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.
This check should be done in constructor to avoid method call for everyone not using this feature
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.
I didn't want to break the code guideline but ok, if it's allowed