Skip to content

Commit

Permalink
Added support for phpPackage, phpPackageNamespace and `phpPackage…
Browse files Browse the repository at this point in the history
…Name`.
  • Loading branch information
AlexSkrypnyk committed May 20, 2024
1 parent aa41a0a commit a5a3d02
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
| `phpMethodRaw` | `I am a__string-With sp@ce¥s 14 and 😀 unicode élève` <br/> `iAmAStringWithSp@ce¥s14And😀UnicodeÉlève` |
| `phpNamespace` | `I am a__string-With sp@ce¥s 14 and 😀 unicode élève` <br/> `IAmAStringWithSpces14AndUnicodeEleve` |
| `phpNamespaceRaw` | `I am a__string-With sp@ce¥s 14 and 😀 unicode élève` <br/> `IAmAStringWithSp@ce¥s14And😀UnicodeÉlève` |
| `phpPackage` | `I am a__string-W/ith sp@ce¥s 14 and 😀 unicode élève` <br/> `i-am-a__string-w/ith-sp-ce-s-14-and-unicode-l-ve` |
| `phpPackageName` | `I am a__string-With sp@ce¥s 14 and 😀 unicode élève` <br/> `i-am-a__string-with-sp-ce-s-14-and-unicode-l-ve` |
| `phpPackageNamespace` | `I am a__string-With sp@ce¥s 14 and 😀 unicode élève` <br/> `i-am-a__string-with-sp-ce-s-14-and-unicode-l-ve` |
| `sentence` | `I am a__string-With sp@ce¥s 14 and 😀 unicode élève` <br/> `I am a string-with sp@ce¥s 14 and 😀 unicode élève` |

## Installation and usage
Expand Down
40 changes: 40 additions & 0 deletions Str2Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* @SuppressWarnings(PHPMD.TooManyMethods)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class Str2Name {

Expand Down Expand Up @@ -93,6 +94,7 @@ public static function label(string $string): string {
*/
public static function machine(string $string): string {
$string = static::strict($string);

return static::snake(str_replace(['-'], ' ', $string));
}

Expand Down Expand Up @@ -216,6 +218,43 @@ public static function phpMethodRaw(string $string): string {
return static::camel($string);
}

/**
* @from I am a__string-W/ith sp@ce¥s 14 and 😀 unicode élève
* @to i-am-a__string-w/ith-sp-ce-s-14-and-unicode-l-ve
*/
public static function phpPackage(string $string): string {
$parts = explode('/', $string);

if (count($parts) !== 2) {
return '';
}

$namespace = (string) preg_replace('/[^a-z0-9_.-]+/', '-', mb_strtolower($parts[0]));
$name = (string) preg_replace('/[^a-z0-9_.-]+/', '-', mb_strtolower($parts[1]));

if ($namespace === '-' || $name === '-') {
return '';
}

return $namespace . '/' . $name;
}

/**
* @from I am a__string-With sp@ce¥s 14 and 😀 unicode élève
* @to i-am-a__string-with-sp-ce-s-14-and-unicode-l-ve
*/
public static function phpPackageNamespace(string $string): string {
return (string) preg_replace('/[^a-z0-9_.-]+/', '-', mb_strtolower($string));
}

/**
* @from I am a__string-With sp@ce¥s 14 and 😀 unicode élève
* @to i-am-a__string-with-sp-ce-s-14-and-unicode-l-ve
*/
public static function phpPackageName(string $string): string {
return (string) preg_replace('/[^a-z0-9_.-]+/', '-', mb_strtolower($string));
}

/**
* @from I am a__string-With sp@ce¥s 14 and 😀 unicode élève
* @to i_am_a__stringwith_sp@ce¥s_14_and_😀_unicode_élève
Expand All @@ -230,6 +269,7 @@ public static function domain(string $string): string {
*/
public static function httpHeader(string $string): string {
$string = static::strict($string);

return static::train($string);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/phpunit/Unit/PhpPackageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace AlexSkrypnyk\Str2Name\Tests\Unit;

use AlexSkrypnyk\Str2Name\Str2Name;
use PHPUnit\Framework\Attributes\CoversMethod;

#[CoversMethod(Str2Name::class, 'phpPackage')]
class PhpPackageTest extends MethodTestCase {

protected static array $cases = [
['abc/def', 'abc/def'],
['Abc/dEf', 'abc/def'],
['Ab.c/d.Ef', 'ab.c/d.ef'],

['abc', ''],
['¡¢', ''],
['¡¢/£¤¥', ''],
];

}

0 comments on commit a5a3d02

Please sign in to comment.