Skip to content

Commit

Permalink
Merge pull request #17 from rappasoft/develop
Browse files Browse the repository at this point in the history
Add new string helpers
  • Loading branch information
rappasoft authored Apr 24, 2023
2 parents 361c67c + fcd96a8 commit 044e2c1
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 13 deletions.
87 changes: 74 additions & 13 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ weight: 5
* @return bool
*/
function array_accessible($value): bool
``````
```

### array_add

Expand All @@ -31,7 +31,7 @@ function array_accessible($value): bool
* @return array
*/
function array_add(array $array, string $key, $value): array
````
```

### array_collapse

Expand All @@ -44,7 +44,7 @@ function array_add(array $array, string $key, $value): array
* @return array
*/
function array_collapse(iterable $array): array
````
```

### array_cross_join

Expand All @@ -57,7 +57,7 @@ function array_collapse(iterable $array): array
* @return array
*/
function array_cross_join(...$arrays): array
````
```

### array_divide

Expand All @@ -70,7 +70,7 @@ function array_cross_join(...$arrays): array
* @return array
*/
function array_divide(array $array): array
````
```

### array_dot

Expand All @@ -84,7 +84,7 @@ function array_divide(array $array): array
* @return array
*/
function array_dot(iterable $array, string $prepend = ''): array
````
```

### array_except

Expand All @@ -98,7 +98,7 @@ function array_dot(iterable $array, string $prepend = ''): array
* @return array
*/
function array_except(array $array, $keys): array
````
```

### array_exists

Expand All @@ -112,7 +112,7 @@ function array_except(array $array, $keys): array
* @return bool
*/
function array_exists($array, $key): bool
````
```

### array_first

Expand All @@ -127,7 +127,7 @@ function array_exists($array, $key): bool
* @return mixed
*/
function array_first(iterable $array, callable $callback = null, $default = null)
````
```

### array_last

Expand All @@ -142,7 +142,7 @@ function array_first(iterable $array, callable $callback = null, $default = null
* @return mixed
*/
function array_last(array $array, callable $callback = null, $default = null)
````
```

### array_flatten

Expand Down Expand Up @@ -403,7 +403,7 @@ function array_where(array $array, callable $callback): array
* @return array
*/
function array_wrap($value): array
````
```

### data_fill

Expand Down Expand Up @@ -475,6 +475,18 @@ function head(array $array)
function last(array $array)
```

### to_array

```php
/**
* Convert Json into Array.
*
* @param string $json
*
* @return array
*/
function to_array(string $json)
```

## Strings

Expand Down Expand Up @@ -920,7 +932,7 @@ function str_starts_with(string $haystack, $needles): bool
### str_studly

```php
/**
/**
* Convert a value to studly caps case.
*
* @param string $value
Expand All @@ -930,6 +942,56 @@ function str_starts_with(string $haystack, $needles): bool
function str_studly(string $value): string
```

### str_pascal

```php
/**
* Convert a string to pascal case.
*
* @param string $value
*
* @return string
*/
function str_pascal(string $value): string
```

### str_camel

```php
/**
* Convert a string to cameel case.
*
* @param string $value
*
* @return string
*/
function str_camel(string $value): string
```

### str_uuid4

```php
/**
* Generate a UUID (version 4).
*
* @return string
*/
function str_uuid4(): string
```

### str_jwt

```php
/**
* Generate a JWT.
*
* @param array $payload
*
* @return string
*/
function str_jwt(array $payload): string
```

## Classes

### class_basename
Expand Down Expand Up @@ -1047,4 +1109,3 @@ function value($value)
*/
function with($object)
```

14 changes: 14 additions & 0 deletions src/arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -904,3 +904,17 @@ function last(array $array)
return end($array);
}
}

if (! function_exists('to_array')) {
/**
* Convert Json into Array.
*
* @param string $json
*
* @return array
*/
function to_array(string $json)
{
return (array)json_decode($json);
}
}
72 changes: 72 additions & 0 deletions src/strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,3 +668,75 @@ function str_studly(string $value): string
return $studlyCache[$key] = str_replace(' ', '', $value);
}
}

if (! function_exists('str_pascal')) {
/**
* Convert a string to pascal case.
*
* @param string $value
*
* @return string
*/
function str_pascal(string $value): string
{
$words = preg_replace('/[\p{P}]/u', ' ', $value);

return str_replace(' ', '', ucwords($words));
}
}

if (! function_exists('str_camel')) {
/**
* Convert a string to camel case.
*
* @param string $value
*
* @return string
*/
function str_camel(string $value): string
{
return lcfirst(str_pascal($value));
}
}

if (! function_exists('str_uuid4')) {
/**
* Generate a UUID (version 4).
*
* @return string
*/
function str_uuid4() {
$bytes = random_bytes(16);

$bytes[6] = chr(ord($bytes[6]) & 0x0f | 0x40);
$bytes[8] = chr(ord($bytes[8]) & 0x3f | 0x80);

return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($bytes), 4));
}
}

if (! function_exists('str_jwt')) {
/**
* Generate a JWT.
*
* @param array $payload
*
* @return string
*/
function str_jwt(array $payload) {
$patterns = ['+', '/', '='];

$replacements = ['-', '_', ''];

$header = json_encode(['typ' => 'JWT', 'alg' => 'HS256']);
$chain[] = str_replace($patterns, $replacements, base64_encode($header));

$payload = json_encode($payload);
$chain[] = str_replace($patterns, $replacements, base64_encode($payload));

$signature = hash_hmac('sha256', $chain[0] . "." . $chain[1], 'abC123!', true);
$chain[] = str_replace($patterns, $replacements, base64_encode($signature));

return implode('.', $chain);
}
}

0 comments on commit 044e2c1

Please sign in to comment.