From fa951615d7bab23139dbd2a0fa1d1cfb551585c9 Mon Sep 17 00:00:00 2001 From: ajibukunoluwa Date: Fri, 21 Jan 2022 15:52:53 +0100 Subject: [PATCH] Improved documentation with better examples --- README.md | 6 +++--- src/Helpers/Str.php | 11 ++++++----- src/Traits/SupportsDynamicCalls.php | 2 +- tests/Units/StrTest.php | 8 ++++---- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index b67ccfd..0288c56 100644 --- a/README.md +++ b/README.md @@ -64,14 +64,14 @@ var_dump(Duration::fiftyFourDays()); // returns 4665600; ### Dynamic calls In addition to the methods provided above, the package uses `PHP` `__callStatic()` method to allow you make dynamic calls on the `Duration` trait. -For example, you want to get the number of seconds in 37 days, you can achieve this by calling a `studly-case` text of the number (`thirtySeven` in this case), plus the unit (`Days` in this case). That will leave us with something like this: +For example, you want to get the number of seconds in 37 days, you can achieve this by calling a `camel-case` text of the number (`thirtySeven` in this case), plus the unit (`Days` in this case). That will leave us with something like this: ```php -// The formula = studlyCaseOfTheNumberInWords + Unit +// The formula = camelCaseOfTheNumberInWords + Unit Duration::thirtySevenDays(); // returns the number of seconds in 37 days ``` -> **Note:** The number in words **MUST** be in `studly-case`. Any other case will throw an `InvalidArgumentException`. Additionally, it must be followed by a `title-case` of the unit. The available units are `Seconds`, `Minutes`, `Hours`, and `Days`. +> **Note:** The number in words **MUST** be in `camel-case`. Any other case will throw an `InvalidArgumentException`. Additionally, it must be followed by a `title-case` of the unit. The available units are `Seconds`, `Minutes`, `Hours`, and `Days`. ## Usage diff --git a/src/Helpers/Str.php b/src/Helpers/Str.php index e112d94..82f4e01 100644 --- a/src/Helpers/Str.php +++ b/src/Helpers/Str.php @@ -40,8 +40,9 @@ public static function simplePluralize(string $word): string */ public static function wordsToNumber($data): int { - if (isset(static::$wordsToNumberCache[$data])) { - return static::$wordsToNumberCache[$data]; + $key = $data; + if (isset(static::$wordsToNumberCache[$key])) { + return static::$wordsToNumberCache[$key]; } $wordsToValue = [ @@ -139,17 +140,17 @@ function ($val) { $last = $part; } - return static::$wordsToNumberCache[$data] = (int) ($sum + $stack->pop()); + return static::$wordsToNumberCache[$key] = (int) ($sum + $stack->pop()); } /** - * Convert a studly case string to space separated words. + * Convert a camel case string to space separated words. * * @param string $string * * @return string */ - public static function studlyToSpaceSeparated(string $string): string + public static function camelToSpaceSeparated(string $string): string { $words = array_map(function ($word) { return strtolower($word); diff --git a/src/Traits/SupportsDynamicCalls.php b/src/Traits/SupportsDynamicCalls.php index 5f5b2a6..6bba938 100644 --- a/src/Traits/SupportsDynamicCalls.php +++ b/src/Traits/SupportsDynamicCalls.php @@ -30,7 +30,7 @@ private static function allowedSuffixes(): array */ public static function dynamicCall($methodName): int { - $wordsInMethodName = explode(' ', Str::studlyToSpaceSeparated($methodName)); + $wordsInMethodName = explode(' ', Str::camelToSpaceSeparated($methodName)); $suffix = end($wordsInMethodName); if (! in_array($suffix, static::allowedSuffixes())) { diff --git a/tests/Units/StrTest.php b/tests/Units/StrTest.php index e3fab83..4900e32 100644 --- a/tests/Units/StrTest.php +++ b/tests/Units/StrTest.php @@ -38,15 +38,15 @@ } }); -it('can correctly convert studly to space separated', function () { - $studlyCaseToSpaceSeparated = [ +it('can correctly convert camel to space separated', function () { + $camelCaseToSpaceSeparated = [ 'thisIsALongWord' => 'this is a long word', 'thisIsALongWordWithNumbers' => 'this is a long word with numbers', 'thisIsALongWordWithNumbersAndSymbols' => 'this is a long word with numbers and symbols', 'iNeedAJob' => 'i need a job', ]; - foreach ($studlyCaseToSpaceSeparated as $studly => $space) { - expect(Str::studlyToSpaceSeparated($studly))->toBe($space); + foreach ($camelCaseToSpaceSeparated as $camel => $space) { + expect(Str::camelToSpaceSeparated($camel))->toBe($space); } });