Skip to content

Commit

Permalink
Add prefix for generateId(). (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Aug 28, 2023
1 parent 2b60049 commit ff06206
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Attribute/HasId.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ public function id(string|null $id): static
/**
* Generate a unique ID for the widget.
*
* @param string $value The value to use for the ID.
*
* @return string A unique ID for the widget.
*/
public function generateId(): string
public function generateId(string $value = 'id_'): string
{
/** @psalm-var string */
return $this->attributes['id'] ??= uniqid('id_');
return $this->attributes['id'] ??= uniqid($value);
}
}
1 change: 1 addition & 0 deletions tests/Attribute/HasIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function testGenerateId(): void

$this->assertNotEmpty($instance->generateId());
$this->assertMatchesRegularExpression('/^id_[a-z0-9]{13}$/', $instance->generateId());
$this->assertMatchesRegularExpression('/^alert_[a-z0-9]{13}$/', $instance->id(null)->generateId('alert_'));
}

public function testGenerateIdWithId(): void
Expand Down

0 comments on commit ff06206

Please sign in to comment.