Skip to content

Commit 8380d6e

Browse files
Add generateId() method to Utils::class helper. (#3)
1 parent 7667c2d commit 8380d6e

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Change Log
22

3-
## 0.1.1 Under development
3+
## 0.1.1 February 29, 2024
4+
5+
- Enh #3: Add `generateId()` method to `Utils::class` helper (@terabytesoftw)
46

57
## 0.1.0 February 28, 2024
68

src/Utils.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use function strrchr;
1616
use function strrpos;
1717
use function substr;
18+
use function uniqid;
1819

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

6768
/**
68-
* Returns the short name of the given class.
69+
* Generate arrayable name from string.
6970
*
70-
* @param string $class The class name.
71+
* @param string $name String to convert.
7172
*/
72-
public static function getShortNameClass(string $class): string
73+
public static function generateArrayableName(string $name): string
7374
{
74-
return substr(strrchr($class, '\\'), 1) . '::class';
75+
return !str_ends_with($name, '[]') ? $name . '[]' : $name;
7576
}
7677

7778
/**
78-
* Generate arrayable name from string.
79+
* Generates a unique ID for an element.
7980
*
80-
* @param string $name String to convert.
81+
* @param string $prefix The prefix string. If not specified, the default is 'id-'.
82+
*
83+
* @return string The unique ID.
8184
*/
82-
public static function generateArrayableName(string $name): string
85+
public static function generateId(string $prefix = 'id-'): string
8386
{
84-
return !str_ends_with($name, '[]') ? $name . '[]' : $name;
87+
return uniqid($prefix);
8588
}
8689

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

154+
/**
155+
* Returns the short name of the given class.
156+
*
157+
* @param string $class The class name.
158+
*/
159+
public static function getShortNameClass(string $class): string
160+
{
161+
return substr(strrchr($class, '\\'), 1) . '::class';
162+
}
163+
151164
/**
152165
* This method parses an property expression and returns an associative array containing real property name,
153166
* prefix, and suffix.

tests/UtilsTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,19 @@ public function testConvertToPatterInvalid(string $regexp, string $message, ?str
3636
Utils::convertToPattern($regexp, $delimiter);
3737
}
3838

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

44-
public function testGenerateArrayableName(): void
44+
public function testGenerateId(): void
4545
{
46-
$this->assertSame('test.name[]', Utils::generateArrayableName('test.name'));
46+
$this->assertMatchesRegularExpression('/^id-[0-9a-f]{13}$/', Utils::generateId());
47+
}
48+
49+
public function testGenerateIdWithPrefix(): void
50+
{
51+
$this->assertMatchesRegularExpression('/^prefix-[0-9a-f]{13}$/', Utils::generateId('prefix-'));
4752
}
4853

4954
public function testGenerateInputId(): void
@@ -85,6 +90,11 @@ public function testGetInputNameExceptionWithTabular(): void
8590
Utils::generateInputName('', '[0]dates[0]');
8691
}
8792

93+
public function testGetShortNameClass(): void
94+
{
95+
$this->assertSame('UtilsTest::class', Utils::getShortNameClass(self::class));
96+
}
97+
8898
public function testMultibyteGenerateArrayableName(): void
8999
{
90100
$this->assertSame('登录[]', Utils::generateArrayableName('登录'));

0 commit comments

Comments
 (0)