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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log

## 0.1.1 Under development
## 0.1.1 February 29, 2024

- Enh #3: Add `generateId()` method to `Utils::class` helper (@terabytesoftw)

## 0.1.0 February 28, 2024

Expand Down
29 changes: 21 additions & 8 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function strrchr;
use function strrpos;
use function substr;
use function uniqid;

/**
* Utils provides a set of static methods for common tasks.
Expand Down Expand Up @@ -65,23 +66,25 @@ public static function convertToPattern(string $regexp, string $delimiter = null
}

/**
* Returns the short name of the given class.
* Generate arrayable name from string.
*
* @param string $class The class name.
* @param string $name String to convert.
*/
public static function getShortNameClass(string $class): string
public static function generateArrayableName(string $name): string
{
return substr(strrchr($class, '\\'), 1) . '::class';
return !str_ends_with($name, '[]') ? $name . '[]' : $name;
}

/**
* Generate arrayable name from string.
* Generates a unique ID for an element.
*
* @param string $name String to convert.
* @param string $prefix The prefix string. If not specified, the default is 'id-'.
*
* @return string The unique ID.
*/
public static function generateArrayableName(string $name): string
public static function generateId(string $prefix = 'id-'): string
{
return !str_ends_with($name, '[]') ? $name . '[]' : $name;
return uniqid($prefix);
}

/**
Expand Down Expand Up @@ -148,6 +151,16 @@ public static function generateInputName(string $fieldModel, string $property, b
throw new InvalidArgumentException('The field model cannot be empty for tabular inputs.');
}

/**
* Returns the short name of the given class.
*
* @param string $class The class name.
*/
public static function getShortNameClass(string $class): string
{
return substr(strrchr($class, '\\'), 1) . '::class';
}

/**
* This method parses an property expression and returns an associative array containing real property name,
* prefix, and suffix.
Expand Down
18 changes: 14 additions & 4 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ public function testConvertToPatterInvalid(string $regexp, string $message, ?str
Utils::convertToPattern($regexp, $delimiter);
}

public function testGetShortNameClass(): void
public function testGenerateArrayableName(): void
{
$this->assertSame('UtilsTest::class', Utils::getShortNameClass(self::class));
$this->assertSame('test.name[]', Utils::generateArrayableName('test.name'));
}

public function testGenerateArrayableName(): void
public function testGenerateId(): void
{
$this->assertSame('test.name[]', Utils::generateArrayableName('test.name'));
$this->assertMatchesRegularExpression('/^id-[0-9a-f]{13}$/', Utils::generateId());
}

public function testGenerateIdWithPrefix(): void
{
$this->assertMatchesRegularExpression('/^prefix-[0-9a-f]{13}$/', Utils::generateId('prefix-'));
}

public function testGenerateInputId(): void
Expand Down Expand Up @@ -85,6 +90,11 @@ public function testGetInputNameExceptionWithTabular(): void
Utils::generateInputName('', '[0]dates[0]');
}

public function testGetShortNameClass(): void
{
$this->assertSame('UtilsTest::class', Utils::getShortNameClass(self::class));
}

public function testMultibyteGenerateArrayableName(): void
{
$this->assertSame('登录[]', Utils::generateArrayableName('登录'));
Expand Down