Skip to content

Commit 044e2c1

Browse files
authored
Merge pull request #17 from rappasoft/develop
Add new string helpers
2 parents 361c67c + fcd96a8 commit 044e2c1

File tree

3 files changed

+160
-13
lines changed

3 files changed

+160
-13
lines changed

docs/usage.md

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ weight: 5
1616
* @return bool
1717
*/
1818
function array_accessible($value): bool
19-
``````
19+
```
2020

2121
### array_add
2222

@@ -31,7 +31,7 @@ function array_accessible($value): bool
3131
* @return array
3232
*/
3333
function array_add(array $array, string $key, $value): array
34-
````
34+
```
3535

3636
### array_collapse
3737

@@ -44,7 +44,7 @@ function array_add(array $array, string $key, $value): array
4444
* @return array
4545
*/
4646
function array_collapse(iterable $array): array
47-
````
47+
```
4848

4949
### array_cross_join
5050

@@ -57,7 +57,7 @@ function array_collapse(iterable $array): array
5757
* @return array
5858
*/
5959
function array_cross_join(...$arrays): array
60-
````
60+
```
6161

6262
### array_divide
6363

@@ -70,7 +70,7 @@ function array_cross_join(...$arrays): array
7070
* @return array
7171
*/
7272
function array_divide(array $array): array
73-
````
73+
```
7474

7575
### array_dot
7676

@@ -84,7 +84,7 @@ function array_divide(array $array): array
8484
* @return array
8585
*/
8686
function array_dot(iterable $array, string $prepend = ''): array
87-
````
87+
```
8888

8989
### array_except
9090

@@ -98,7 +98,7 @@ function array_dot(iterable $array, string $prepend = ''): array
9898
* @return array
9999
*/
100100
function array_except(array $array, $keys): array
101-
````
101+
```
102102

103103
### array_exists
104104

@@ -112,7 +112,7 @@ function array_except(array $array, $keys): array
112112
* @return bool
113113
*/
114114
function array_exists($array, $key): bool
115-
````
115+
```
116116

117117
### array_first
118118

@@ -127,7 +127,7 @@ function array_exists($array, $key): bool
127127
* @return mixed
128128
*/
129129
function array_first(iterable $array, callable $callback = null, $default = null)
130-
````
130+
```
131131

132132
### array_last
133133

@@ -142,7 +142,7 @@ function array_first(iterable $array, callable $callback = null, $default = null
142142
* @return mixed
143143
*/
144144
function array_last(array $array, callable $callback = null, $default = null)
145-
````
145+
```
146146

147147
### array_flatten
148148

@@ -403,7 +403,7 @@ function array_where(array $array, callable $callback): array
403403
* @return array
404404
*/
405405
function array_wrap($value): array
406-
````
406+
```
407407

408408
### data_fill
409409

@@ -475,6 +475,18 @@ function head(array $array)
475475
function last(array $array)
476476
```
477477

478+
### to_array
479+
480+
```php
481+
/**
482+
* Convert Json into Array.
483+
*
484+
* @param string $json
485+
*
486+
* @return array
487+
*/
488+
function to_array(string $json)
489+
```
478490

479491
## Strings
480492

@@ -920,7 +932,7 @@ function str_starts_with(string $haystack, $needles): bool
920932
### str_studly
921933

922934
```php
923-
/**
935+
/**
924936
* Convert a value to studly caps case.
925937
*
926938
* @param string $value
@@ -930,6 +942,56 @@ function str_starts_with(string $haystack, $needles): bool
930942
function str_studly(string $value): string
931943
```
932944

945+
### str_pascal
946+
947+
```php
948+
/**
949+
* Convert a string to pascal case.
950+
*
951+
* @param string $value
952+
*
953+
* @return string
954+
*/
955+
function str_pascal(string $value): string
956+
```
957+
958+
### str_camel
959+
960+
```php
961+
/**
962+
* Convert a string to cameel case.
963+
*
964+
* @param string $value
965+
*
966+
* @return string
967+
*/
968+
function str_camel(string $value): string
969+
```
970+
971+
### str_uuid4
972+
973+
```php
974+
/**
975+
* Generate a UUID (version 4).
976+
*
977+
* @return string
978+
*/
979+
function str_uuid4(): string
980+
```
981+
982+
### str_jwt
983+
984+
```php
985+
/**
986+
* Generate a JWT.
987+
*
988+
* @param array $payload
989+
*
990+
* @return string
991+
*/
992+
function str_jwt(array $payload): string
993+
```
994+
933995
## Classes
934996

935997
### class_basename
@@ -1047,4 +1109,3 @@ function value($value)
10471109
*/
10481110
function with($object)
10491111
```
1050-

src/arrays.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,3 +904,17 @@ function last(array $array)
904904
return end($array);
905905
}
906906
}
907+
908+
if (! function_exists('to_array')) {
909+
/**
910+
* Convert Json into Array.
911+
*
912+
* @param string $json
913+
*
914+
* @return array
915+
*/
916+
function to_array(string $json)
917+
{
918+
return (array)json_decode($json);
919+
}
920+
}

src/strings.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,3 +668,75 @@ function str_studly(string $value): string
668668
return $studlyCache[$key] = str_replace(' ', '', $value);
669669
}
670670
}
671+
672+
if (! function_exists('str_pascal')) {
673+
/**
674+
* Convert a string to pascal case.
675+
*
676+
* @param string $value
677+
*
678+
* @return string
679+
*/
680+
function str_pascal(string $value): string
681+
{
682+
$words = preg_replace('/[\p{P}]/u', ' ', $value);
683+
684+
return str_replace(' ', '', ucwords($words));
685+
}
686+
}
687+
688+
if (! function_exists('str_camel')) {
689+
/**
690+
* Convert a string to camel case.
691+
*
692+
* @param string $value
693+
*
694+
* @return string
695+
*/
696+
function str_camel(string $value): string
697+
{
698+
return lcfirst(str_pascal($value));
699+
}
700+
}
701+
702+
if (! function_exists('str_uuid4')) {
703+
/**
704+
* Generate a UUID (version 4).
705+
*
706+
* @return string
707+
*/
708+
function str_uuid4() {
709+
$bytes = random_bytes(16);
710+
711+
$bytes[6] = chr(ord($bytes[6]) & 0x0f | 0x40);
712+
$bytes[8] = chr(ord($bytes[8]) & 0x3f | 0x80);
713+
714+
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($bytes), 4));
715+
}
716+
}
717+
718+
if (! function_exists('str_jwt')) {
719+
/**
720+
* Generate a JWT.
721+
*
722+
* @param array $payload
723+
*
724+
* @return string
725+
*/
726+
function str_jwt(array $payload) {
727+
$patterns = ['+', '/', '='];
728+
729+
$replacements = ['-', '_', ''];
730+
731+
$header = json_encode(['typ' => 'JWT', 'alg' => 'HS256']);
732+
$chain[] = str_replace($patterns, $replacements, base64_encode($header));
733+
734+
$payload = json_encode($payload);
735+
$chain[] = str_replace($patterns, $replacements, base64_encode($payload));
736+
737+
$signature = hash_hmac('sha256', $chain[0] . "." . $chain[1], 'abC123!', true);
738+
$chain[] = str_replace($patterns, $replacements, base64_encode($signature));
739+
740+
return implode('.', $chain);
741+
}
742+
}

0 commit comments

Comments
 (0)