Skip to content

Commit

Permalink
Refactor getShortNameClass() method to support optional suffix pa…
Browse files Browse the repository at this point in the history
…rameter. (#4)
  • Loading branch information
terabytesoftw committed Feb 29, 2024
1 parent 5736f57 commit 36a1d22
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
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.2 February 29, 2024

- Enh #4: Refactor `getShortNameClass()` method to support optional `suffix` and `lowercase` parameters (@terabytesoftw)

## 0.1.1 February 29, 2024

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ The `Utils::class` helper can be used to get the short class name.
The method accepts one parameter:

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

```php
<?php
Expand All @@ -161,7 +165,7 @@ declare(strict_types=1);

use PHPForge\Html\Helper\Utils;

$shortName = Utils::getShortClassName('PHPForge\Html\Helper\Utils'); // return: `Utils`
$shortName = Utils::getShortClassName('PHPForge\Html\Helper\Utils'); // return: `Utils::class`
```

### Generate arrayable name
Expand Down
16 changes: 14 additions & 2 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
use function mb_strtolower;
use function preg_match;
use function preg_replace;
use function str_ends_with;
use function str_replace;
use function strlen;
use function strrchr;
use function strrpos;
use function strtolower;
use function substr;
use function uniqid;

Expand Down Expand Up @@ -155,10 +157,20 @@ public static function generateInputName(string $fieldModel, string $property, b
* Returns the short name of the given class.
*
* @param string $class The class name.
* @param bool $suffix Whether to append the `::class` suffix to the class name. If `false`, the suffix will not be
* appended.
* @param bool $lowercase Whether to return the class name in lowercase. If `false`, the class name will be returned
* in its original case.
*/
public static function getShortNameClass(string $class): string
public static function getShortNameClass(string $class, bool $suffix = true, bool $lowercase = false): string
{
return substr(strrchr($class, '\\'), 1) . '::class';
if ($lowercase === true) {
$class = strtolower($class);
}

$class = substr(strrchr($class, '\\'), 1);

return $suffix === true ? "$class::class" : $class;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ public function testGetShortNameClass(): void
$this->assertSame('UtilsTest::class', Utils::getShortNameClass(self::class));
}

public function testGetShortNameClassWithLowercase(): void
{
$this->assertSame('utilstest', Utils::getShortNameClass(self::class, false, true));
}

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

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

0 comments on commit 36a1d22

Please sign in to comment.