From a5a3d024a944e32c9d92dc70c75a0b3ed6fb6d42 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Mon, 20 May 2024 11:48:16 +1000 Subject: [PATCH] Added support for `phpPackage`, `phpPackageNamespace` and `phpPackageName`. --- README.md | 3 ++ Str2Name.php | 40 +++++++++++++++++++++++++++ tests/phpunit/Unit/PhpPackageTest.php | 23 +++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 tests/phpunit/Unit/PhpPackageTest.php diff --git a/README.md b/README.md index a4880e8..f88ac90 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,9 @@ | `phpMethodRaw` | `I am a__string-With sp@ce¥s 14 and 😀 unicode élève`
`iAmAStringWithSp@ce¥s14And😀UnicodeÉlève` | | `phpNamespace` | `I am a__string-With sp@ce¥s 14 and 😀 unicode élève`
`IAmAStringWithSpces14AndUnicodeEleve` | | `phpNamespaceRaw` | `I am a__string-With sp@ce¥s 14 and 😀 unicode élève`
`IAmAStringWithSp@ce¥s14And😀UnicodeÉlève` | +| `phpPackage` | `I am a__string-W/ith sp@ce¥s 14 and 😀 unicode élève`
`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`
`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`
`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`
`I am a string-with sp@ce¥s 14 and 😀 unicode élève` | ## Installation and usage diff --git a/Str2Name.php b/Str2Name.php index 8846387..465c468 100644 --- a/Str2Name.php +++ b/Str2Name.php @@ -9,6 +9,7 @@ * * @SuppressWarnings(PHPMD.TooManyMethods) * @SuppressWarnings(PHPMD.TooManyPublicMethods) + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ class Str2Name { @@ -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)); } @@ -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 @@ -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); } diff --git a/tests/phpunit/Unit/PhpPackageTest.php b/tests/phpunit/Unit/PhpPackageTest.php new file mode 100644 index 0000000..1af1260 --- /dev/null +++ b/tests/phpunit/Unit/PhpPackageTest.php @@ -0,0 +1,23 @@ +