Skip to content

Commit 36a1d22

Browse files
Refactor getShortNameClass() method to support optional suffix parameter. (#4)
1 parent 5736f57 commit 36a1d22

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
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.2 February 29, 2024
4+
5+
- Enh #4: Refactor `getShortNameClass()` method to support optional `suffix` and `lowercase` parameters (@terabytesoftw)
46

57
## 0.1.1 February 29, 2024
68

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ The `Utils::class` helper can be used to get the short class name.
153153
The method accepts one parameter:
154154

155155
- `class:` (string): The class name to get the short name.
156+
- `suffix:` (string): Whether to append the `::class` suffix to the class name.
157+
For default, it is `true`. If it is `false`, the method will return the short name without the `::class` suffix.
158+
- `lowercase:` (bool): Whether to convert the class name to lowercase or not.
159+
For default, it is `false`.
156160

157161
```php
158162
<?php
@@ -161,7 +165,7 @@ declare(strict_types=1);
161165

162166
use PHPForge\Html\Helper\Utils;
163167

164-
$shortName = Utils::getShortClassName('PHPForge\Html\Helper\Utils'); // return: `Utils`
168+
$shortName = Utils::getShortClassName('PHPForge\Html\Helper\Utils'); // return: `Utils::class`
165169
```
166170

167171
### Generate arrayable name

src/Utils.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use function mb_strtolower;
1111
use function preg_match;
1212
use function preg_replace;
13+
use function str_ends_with;
1314
use function str_replace;
1415
use function strlen;
1516
use function strrchr;
1617
use function strrpos;
18+
use function strtolower;
1719
use function substr;
1820
use function uniqid;
1921

@@ -155,10 +157,20 @@ public static function generateInputName(string $fieldModel, string $property, b
155157
* Returns the short name of the given class.
156158
*
157159
* @param string $class The class name.
160+
* @param bool $suffix Whether to append the `::class` suffix to the class name. If `false`, the suffix will not be
161+
* appended.
162+
* @param bool $lowercase Whether to return the class name in lowercase. If `false`, the class name will be returned
163+
* in its original case.
158164
*/
159-
public static function getShortNameClass(string $class): string
165+
public static function getShortNameClass(string $class, bool $suffix = true, bool $lowercase = false): string
160166
{
161-
return substr(strrchr($class, '\\'), 1) . '::class';
167+
if ($lowercase === true) {
168+
$class = strtolower($class);
169+
}
170+
171+
$class = substr(strrchr($class, '\\'), 1);
172+
173+
return $suffix === true ? "$class::class" : $class;
162174
}
163175

164176
/**

tests/UtilsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ public function testGetShortNameClass(): void
9595
$this->assertSame('UtilsTest::class', Utils::getShortNameClass(self::class));
9696
}
9797

98+
public function testGetShortNameClassWithLowercase(): void
99+
{
100+
$this->assertSame('utilstest', Utils::getShortNameClass(self::class, false, true));
101+
}
102+
103+
public function testGetShortNameClassWithoutSuffix(): void
104+
{
105+
$this->assertSame('UtilsTest', Utils::getShortNameClass(self::class, false));
106+
}
107+
98108
public function testMultibyteGenerateArrayableName(): void
99109
{
100110
$this->assertSame('登录[]', Utils::generateArrayableName('登录'));

0 commit comments

Comments
 (0)