Skip to content

[Twig] Add attribute merging behaviour #1404

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
wants to merge 1 commit into from
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
1 change: 1 addition & 0 deletions src/TwigComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Make `ComponentAttributes` traversable/countable.
- Add attribute merging behaviour notation.

## 2.13.0

Expand Down
39 changes: 35 additions & 4 deletions src/TwigComponent/src/ComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
*/
final class ComponentAttributes implements \IteratorAggregate, \Countable
{
private const PREFIX = '^:';
private const SUFFIX = '$:';
private const REPLACE = '@:';

private const BEHAVIOUR_REGEX = '#^([\^$@]:)(.+)$#';
private const AUTOSUFFIX_TAGS = ['class', 'data-controller', 'data-action'];

/**
* @param array<string, string|bool> $attributes
*/
Expand All @@ -47,7 +54,7 @@ function (string $carry, string $key) {
return match ($value) {
true => "{$carry} {$key}",
false => $carry,
default => sprintf('%s %s="%s"', $carry, $key, $value),
default => sprintf('%s %s="%s"', $carry, $key, preg_replace(self::BEHAVIOUR_REGEX, '$2', (string) $value)),
};
},
''
Expand Down Expand Up @@ -80,10 +87,18 @@ public function defaults(iterable $attributes): self
}

foreach ($this->attributes as $key => $value) {
if (\in_array($key, ['class', 'data-controller', 'data-action'], true) && isset($attributes[$key])) {
$attributes[$key] = "{$attributes[$key]} {$value}";
[$behaviour, $value] = self::parseBehaviour($value);

continue;
if (null === $behaviour && \in_array($key, self::AUTOSUFFIX_TAGS, true)) {
$behaviour = self::SUFFIX;
}

if (isset($attributes[$key]) && \is_string($attributes[$key])) {
$value = match ($behaviour) {
self::PREFIX => "{$value} {$attributes[$key]}",
self::SUFFIX => "{$attributes[$key]} {$value}",
default => $value,
};
}

$attributes[$key] = $value;
Expand Down Expand Up @@ -167,4 +182,20 @@ public function count(): int
{
return \count($this->attributes);
}

/**
* @return array{0: self::PREFIX|self::SUFFIX|self::REPLACE|null, 1: mixed}
*/
private static function parseBehaviour(mixed $value): array
{
if (!\is_string($value)) {
return [null, $value];
}

if (!preg_match(self::BEHAVIOUR_REGEX, $value, $matches)) {
return [null, $value];
}

return [$matches[1], $matches[2]];
}
}
109 changes: 109 additions & 0 deletions src/TwigComponent/tests/Unit/ComponentAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,113 @@ public function testIsTraversableAndCountable(): void
$this->assertSame($attributes->all(), iterator_to_array($attributes));
$this->assertCount(1, $attributes);
}

public function testDefaultBehaviourNotation(): void
{
$attributes = new ComponentAttributes([
'data-1' => 'value',
'data-2' => '$:value',
'data-3' => '^:value',
'data-4' => '@:value',
'data-5' => true,
'data-6' => '^:value',
'data-7' => '$:value',
]);

$attributes = $attributes->defaults([
'data-1' => 'default',
'data-2' => 'default',
'data-3' => 'default',
'data-4' => 'default',
'data-5' => false,
'data-6' => false,
'data-7' => true,
]);

$this->assertSame([
'data-1' => 'value',
'data-2' => 'default value',
'data-3' => 'value default',
'data-4' => 'value',
'data-5' => true,
'data-6' => 'value',
'data-7' => 'value',
], $attributes->all());
}

public function testCanOverrideCustomBehaviourAttributes(): void
{
$attributes = new ComponentAttributes([
'class' => '@:value',
'data-controller' => '@:value',
'data-action' => '@:value',
]);

$attributes = $attributes->defaults([
'class' => 'default',
'data-controller' => 'default',
'data-action' => 'default',
]);

$this->assertSame([
'class' => 'value',
'data-controller' => 'value',
'data-action' => 'value',
], $attributes->all());
}

public function testCanPrefixCustomBehaviourAttributes(): void
{
$attributes = new ComponentAttributes([
'class' => '^:value',
'data-controller' => '^:value',
'data-action' => '^:value',
]);

$attributes = $attributes->defaults([
'class' => 'default',
'data-controller' => 'default',
'data-action' => 'default',
]);

$this->assertSame([
'class' => 'value default',
'data-controller' => 'value default',
'data-action' => 'value default',
], $attributes->all());
}

public function testCanSuffixCustomBehaviourAttributes(): void
{
$attributes = new ComponentAttributes([
'class' => '$:value',
'data-controller' => '$:value',
'data-action' => '$:value',
]);

$attributes = $attributes->defaults([
'class' => 'default',
'data-controller' => 'default',
'data-action' => 'default',
]);

$this->assertSame([
'class' => 'default value',
'data-controller' => 'default value',
'data-action' => 'default value',
], $attributes->all());
}

public function testBehaviourNotationIsNotRendered(): void
{
$attributes = new ComponentAttributes([
'data-first' => 'value',
'data-second' => '$:value',
'data-third' => '^:value',
'data-fourth' => '@:value',
'data-fifth' => true,
]);

$this->assertSame(' data-first="value" data-second="value" data-third="value" data-fourth="value" data-fifth', (string) $attributes);
}
}