Skip to content

Commit 5192fbb

Browse files
committed
update utility docs
1 parent 0094817 commit 5192fbb

1 file changed

Lines changed: 129 additions & 1 deletion

File tree

v5/utilities.md

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,23 @@ Arr::pluck($array, ['name'], 'id');
289289
// [123 => 'John', 789 => 'Sally']
290290
```
291291

292+
### Arr::reduceAllowedStr
293+
294+
The `Arr::reduceAllowedStr()` method builds a string of unique words from an array's keys based on their boolean values:
295+
296+
```php
297+
use \TypeRocket\Utility\Arr;
298+
299+
$array = [
300+
'button button-primary' => true,
301+
'button' => true,
302+
'link link-external' => false,
303+
];
304+
305+
Arr::reduceAllowedStr($array);
306+
// 'button button-primary'
307+
```
308+
292309
### Arr::replaceRecursivePreferNew
293310

294311
The `Arr::replaceRecursivePreferNew()` method works like `array_replace_recursive` but keeps the order of the new array and allows for setting stop break points to the replacement search. The method take three arguments:
@@ -363,6 +380,59 @@ Str::camelize($name, '_', $capitalize_first_char);
363380
// 'HiThere'
364381
```
365382

383+
### Str::classNames
384+
385+
The `Str::classNames()` method constructs a list of classes for an HTML element. The method builds a string of unique words from an array's keys based on their boolean values:
386+
387+
```php
388+
use \TypeRocket\Utility\Str;
389+
390+
$array = [
391+
'button' => true,
392+
'button-primary' => true,
393+
'button-large' => true,
394+
'button-disabled' => false,
395+
];
396+
397+
Str::classNames($array);
398+
// 'button button-primary button-large'
399+
```
400+
401+
If the first argument is a string it will be appended to the result:
402+
403+
```php
404+
use \TypeRocket\Utility\Str;
405+
406+
$base = 'button';
407+
408+
$array = [
409+
'button-primary' => true,
410+
'button-large' => true,
411+
'button-disabled' => false,
412+
];
413+
414+
Str::classNames($base, $array);
415+
// 'button button-primary button-large'
416+
```
417+
418+
Finally, if a third argument is provided, and all array values are false, it will be prepended to the base string as a fallback:
419+
420+
```php
421+
use \TypeRocket\Utility\Str;
422+
423+
$base = 'button';
424+
425+
$array = [
426+
'button-primary' => false,
427+
'button-large' => false,
428+
];
429+
430+
$fallback = 'button-disabled';
431+
432+
Str::classNames($base, $array, $fallback);
433+
// 'button button-primary button-large'
434+
```
435+
366436
### Str::contains
367437

368438
Use the method `Str::contains()` to test if a string contains a value.
@@ -416,9 +486,67 @@ Str::notBlank(' ');
416486
// true
417487
```
418488

489+
### Str::replaceFirst
490+
491+
The `Str::replaceFirst()` method replaces the first occurrence of a given value in the string:
492+
493+
```php
494+
use \TypeRocket\Utility\Str;
495+
496+
$replace = 'mom';
497+
$with = 'po';
498+
$string = 'momster mom';
499+
500+
Str::replaceFirst($replace, $with, $string);
501+
// 'poster mom'
502+
```
503+
504+
### Str::replaceFirstRegex
505+
506+
The `Str::replaceFirstRegex()` method replaces only the first match in a given string that is escaped by default with `preg_quote()`:
507+
508+
```php
509+
use \TypeRocket\Utility\Str;
510+
511+
$replace = 'mom';
512+
$with = 'po';
513+
$string = 'momster mom';
514+
515+
Str::replaceFirstRegex($replace, $with, $string);
516+
// 'poster mom'
517+
```
518+
519+
Further, you can disable regex escaping of the replacement argument to enable regex:
520+
521+
```php
522+
use \TypeRocket\Utility\Str;
523+
524+
$replace = '/mo./';
525+
$with = 'po';
526+
$string = 'monster mo.';
527+
528+
Str::replaceFirstRegex($replace, $with, $string, false);
529+
// 'poster mo.'
530+
```
531+
532+
### Str::replaceLast
533+
534+
The `Str::replaceLast()` method replaces the last occurrence of a given value in the string:
535+
536+
```php
537+
use \TypeRocket\Utility\Str;
538+
539+
$replace = 'mom';
540+
$with = 'po';
541+
$string = 'monster mom';
542+
543+
Str::replaceLast($replace, $with, $string);
544+
// 'momster po'
545+
```
546+
419547
### Str::snake
420548

421-
Use the method `Str::snake()` to convert a string to snake case.
549+
The `Str::snake()` method converts a string to snake case.
422550

423551
```php
424552
use \TypeRocket\Utility\Str;

0 commit comments

Comments
 (0)