Skip to content

Commit 55a7cf2

Browse files
committed
refactor
1 parent db43b81 commit 55a7cf2

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

src/TwigComponent/src/AttributeBag.php renamed to src/TwigComponent/src/ComponentAttributes.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,40 @@
1616
*
1717
* @experimental
1818
*/
19-
final class AttributeBag
19+
final class ComponentAttributes
2020
{
21+
/** @var array<string, string> */
22+
public array $attributes;
23+
2124
/**
2225
* @param array<string, string> $attributes
2326
*/
24-
public function __construct(public array $attributes)
25-
{
26-
}
27-
28-
public static function create(array|self $attributes): self
27+
public function __construct(array $attributes)
2928
{
30-
return $attributes instanceof self ? $attributes : new self($attributes);
29+
$this->attributes = $attributes;
3130
}
3231

3332
public function __toString(): string
3433
{
35-
return \array_reduce(
36-
\array_keys($this->attributes),
37-
fn(string $carry, string $key) => \sprintf('%s %s="%s"', $carry, $key, $this->attributes[$key]),
34+
return array_reduce(
35+
array_keys($this->attributes),
36+
fn (string $carry, string $key) => sprintf('%s %s="%s"', $carry, $key, $this->attributes[$key]),
3837
''
3938
);
4039
}
4140

42-
public function prepend(array $with): self
41+
/**
42+
* @return array<string, string>
43+
*/
44+
public function all(): array
4345
{
44-
foreach ($this->attributes as $key => $value) {
45-
$with[$key] = isset($with[$key]) ? "{$with[$key]} {$value}" : $value;
46-
}
47-
48-
return new self($with);
46+
return $this->attributes;
4947
}
5048

51-
public function append(array $with): self
49+
public function merge(array $with): self
5250
{
5351
foreach ($this->attributes as $key => $value) {
54-
$with[$key] = isset($with[$key]) ? "{$value} {$with[$key]}" : $value;
52+
$with[$key] = isset($with[$key]) ? "{$with[$key]} {$value}" : $value;
5553
}
5654

5755
return new self($with);
@@ -62,15 +60,15 @@ public function only(string ...$keys): self
6260
$attributes = [];
6361

6462
foreach ($this->attributes as $key => $value) {
65-
if (in_array($key, $keys, true)) {
63+
if (\in_array($key, $keys, true)) {
6664
$attributes[$key] = $value;
6765
}
6866
}
6967

7068
return new self($attributes);
7169
}
7270

73-
public function reject(string ...$keys): self
71+
public function without(string ...$keys): self
7472
{
7573
$clone = clone $this;
7674

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
311

412
namespace Symfony\UX\TwigComponent;
513

14+
use Symfony\UX\LiveComponent\Attribute\LiveProp;
615
use Symfony\UX\TwigComponent\Attribute\PreMount;
716

817
/**
@@ -12,21 +21,24 @@
1221
*/
1322
trait HasAttributesTrait
1423
{
15-
public AttributeBag $attributes;
24+
#[LiveProp(hydrateWith: 'hydrateAttributes', dehydrateWith: 'dehydrateAttributes')]
25+
public ComponentAttributes $attributes;
1626

1727
#[PreMount]
1828
public function mapAttributes(array $data): array
1929
{
20-
$data['attributes'] = $this->initAttributes($data['attributes'] ?? []);
30+
$data['attributes'] = new ComponentAttributes($data['attributes'] ?? []);
2131

2232
return $data;
2333
}
2434

25-
/**
26-
* Override to add your own attribute initialization.
27-
*/
28-
protected function initAttributes(array $attributes): AttributeBag
35+
public static function dehydrateAttributes(ComponentAttributes $attributes): array
36+
{
37+
return $attributes->all();
38+
}
39+
40+
public static function hydrateAttributes(array $attributes): ComponentAttributes
2941
{
30-
return AttributeBag::create($attributes);
42+
return new ComponentAttributes($attributes);
3143
}
3244
}

src/TwigComponent/src/Twig/ComponentRuntime.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\UX\TwigComponent\Twig;
1313

14-
use Symfony\UX\TwigComponent\AttributeBag;
14+
use Symfony\UX\TwigComponent\ComponentAttributes;
1515
use Symfony\UX\TwigComponent\ComponentFactory;
1616
use Symfony\UX\TwigComponent\ComponentRenderer;
1717
use Twig\Environment;
@@ -37,7 +37,7 @@ public function __construct(ComponentFactory $componentFactory, ComponentRendere
3737
public function render(Environment $twig, string $name, array $props = []): string
3838
{
3939
if (!$this->safeClassesRegistered) {
40-
$twig->getExtension(EscaperExtension::class)->addSafeClass(AttributeBag::class, ['html']);
40+
$twig->getExtension(EscaperExtension::class)->addSafeClass(ComponentAttributes::class, ['html']);
4141

4242
$this->safeClassesRegistered = true;
4343
}

0 commit comments

Comments
 (0)