Skip to content

Commit

Permalink
Improved documentation with better examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ajibukunoluwa committed Jan 21, 2022
1 parent 0a98f28 commit fa95161
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions src/Helpers/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/SupportsDynamicCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down
8 changes: 4 additions & 4 deletions tests/Units/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

0 comments on commit fa95161

Please sign in to comment.