Skip to content
Merged
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
29 changes: 26 additions & 3 deletions src/Parser/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,43 @@ public function bound(): bool
*/
public function isStaticValue(): bool
{
return $this->dynamic === false || in_array($this->value, ['true', 'false', 'null'], true);
return $this->dynamic === false || $this->hasConstantDynamicValue();
}

/**
* Resolve the compile time value of this attribute.
*/
public function getStaticValue(): mixed
{
if (! $this->isStaticValue()) {
if ($this->hasConstantDynamicValue()) {
return $this->getConstantValue();
}

if ($this->dynamic) {
throw new \LogicException("Cannot get static value of dynamic attribute '{$this->name}'.");
}

return $this->value;
}

/**
* Check if this attribute is dynamic and has a constant value (true, false, or null).
*/
protected function hasConstantDynamicValue(): bool
{
return $this->dynamic && in_array($this->value, ['true', 'false', 'null'], true);
}

/**
* Resolve the constant value of this attribute (true, false, or null).
*/
protected function getConstantValue()
{
return match ($this->value) {
'true' => true,
'false' => false,
'null' => null,
default => $this->value,
default => throw new \LogicException("Invalid constant value '{$this->value}'."),
};
}
}
2 changes: 1 addition & 1 deletion src/Support/AttributeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function parse(string $attributesString): array
$valueless = is_null($value);

if ($valueless) {
$value = 'true';
$value = true;
}

$quotes = '';
Expand Down
9 changes: 8 additions & 1 deletion tests/ComparisonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@
['readonly' => false],
));

test('foldable literal string attributes matching PHP constants', fn () => compare(<<<'BLADE'
<x-foldable.input value="true" />
<x-foldable.input value="false" />
<x-foldable.input value="null" />
BLADE
));

test('same component in a slot doesnt affect parents attributes', fn () => compare(<<<'BLADE'
<x-card>
<x-card x-data>
Expand Down Expand Up @@ -115,4 +122,4 @@
</x-wrapper>
</x-card>
BLADE
));
));
30 changes: 30 additions & 0 deletions tests/Parser/AttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Livewire\Blaze\Support\AttributeParser;

test('getStaticValue returns constatnts for dynamic constant values', function () {
$attribute = app(AttributeParser::class)->parse(':foo="true"')['foo'];

expect($attribute->isStaticValue())->toBeTrue();
expect($attribute->getStaticValue())->toBeTrue();
});

test('getStaticValue returns strings for static constant values', function () {
$attribute = app(AttributeParser::class)->parse('foo="true"')['foo'];

expect($attribute->isStaticValue())->toBeTrue();
expect($attribute->getStaticValue())->toBe('true');
});

test('getStaticValue returns true for valueless attributes', function () {
$attribute = app(AttributeParser::class)->parse('foo')['foo'];

expect($attribute->isStaticValue())->toBeTrue();
expect($attribute->getStaticValue())->toBeTrue();
});

test('getStaticValue throws for dynamic attributes', function () {
$attribute = app(AttributeParser::class)->parse(':foo="$bar"')['foo'];

$attribute->getStaticValue();
})->throws(LogicException::class);
2 changes: 1 addition & 1 deletion tests/Support/AttributeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
expect($attrs)->toHaveKey('disabled');
expect($attrs['disabled'])
->name->toBe('disabled')
->value->toBe('true')
->value->toBeTrue()
->valueless->toBeTrue()
->dynamic->toBeFalse()
->quotes->toBe('');
Expand Down
Loading