|
1 | 1 | --- |
2 | 2 | title: Primitive utilities |
| 3 | +description: "Working with strings and arrays in PHP is notoriously hard due to the lack of a standard library. Tempest comes with a bunch of utilities to improve the experience in this area." |
3 | 4 | --- |
4 | 5 |
|
5 | | -Tempest comes with a handful of classes that improve working with primitive types such as strings and arrays. The most important feature is an object-oriented API around PHP's built-in primitive helper functions. Let's take a look at what's available. |
| 6 | +## Overview |
6 | 7 |
|
7 | | -## Strings |
| 8 | +Tempest provides a set of classes that make working with scalar values easier. It provides an object-oriented API for handling strings and arrays, along with many namespaced functions to work with regular expressions, random values, pluralization or filesystem paths. |
8 | 9 |
|
9 | | -The `ImmutableString` and `MutableString` classes wraps a normal string, and provide a fluent API to manipulate that string. |
| 10 | +## Namespaced functions |
| 11 | + |
| 12 | +Most utilities provided by Tempest have a function-based implementation under the [`Tempest\Support`](https://github.com/tempestphp/tempest-framework/tree/main/src/Tempest/Support/src) namespace. You may look at what is available on GitHub: |
| 13 | + |
| 14 | +- [Regular expressions](https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Support/src/Regex/functions.php) |
| 15 | +- [Random values](https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Support/src/Random/functions.php) |
| 16 | +- [Filesystem paths](https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Support/src/Path/functions.php) |
| 17 | +- [Pluralization](https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Support/src/Language/functions.php) |
| 18 | +- [PHP namespaces](https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Support/src/Namespace/functions.php) |
| 19 | + |
| 20 | +Tempest also [provides a trait](https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Support/src/IsEnumHelper.php) to work with enumerations, since a functional API is not useful in this case. |
| 21 | + |
| 22 | +## String utilities |
| 23 | + |
| 24 | +Tempest provides string utilities through [namespaced functions](https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Support/src/Str/functions.php) or a fluent, object-oriented API, which comes in an immutable and a mutable flavor. |
| 25 | + |
| 26 | +Providing a string value, you may create an instance of {`\Tempest\Support\Str\ImmutableString`} or {`\Tempest\Support\Str\MutableString`}: |
10 | 27 |
|
11 | 28 | ```php |
12 | 29 | use Tempest\Support\Str\ImmutableString; |
13 | 30 |
|
14 | | -$slug = new ImmutableString('https://tempestphp.com/docs/framework/14-primitive-helpers') |
15 | | - ->trim('/') |
| 31 | +$slug = new ImmutableString('/blog/01-chasing-bugs-down-the-rabbit-hole/') |
| 32 | + ->stripEnd('/') |
16 | 33 | ->afterLast('/') |
17 | 34 | ->replaceRegex('/\d+-/', '') |
| 35 | + ->slug() |
18 | 36 | ->toString(); |
19 | 37 | ``` |
20 | 38 |
|
21 | | -Note that you can also use the `str()` helper function, which is a shorthand for `ImmutableString`: |
| 39 | +Note that you may use the `str()` function as a shorthand to create an {b`\Tempest\Support\Str\ImmutableString`} instance. |
22 | 40 |
|
23 | | -```php |
24 | | -use function Tempest\Support\str; |
| 41 | +## Array utilities |
25 | 42 |
|
26 | | -$path = str('https://tempestphp.com/docs/framework/14-primitive-helpers'); |
| 43 | +Tempest provides array utilities through [namespaced functions](https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Support/src/Arr/functions.php) or a fluent, object-oriented API, which comes in an immutable and a mutable flavor. |
27 | 44 |
|
28 | | -if (! $path->startsWith('/docs')) { |
29 | | - // … |
30 | | -} |
31 | | -``` |
32 | | - |
33 | | -These string helpers encapsulate many of PHP's built-in string functions, as well as several regex-based functions. You can check out the [full API on GitHub](https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Support/src/Str/ManipulatesString.php). |
34 | | - |
35 | | -## Arrays |
36 | | - |
37 | | -The `ImmutableArray` and `MutableArray` classes wrap an array, and provide a fluent API to manipulate it. |
| 45 | +Providing a iterable value, you may create an instance of {`\Tempest\Support\Arr\ImmutableArray`} or {`\Tempest\Support\Arr\MutableArray`}: |
38 | 46 |
|
39 | 47 | ```php |
40 | 48 | use Tempest\Support\Arr\ImmutableArray; |
41 | 49 |
|
42 | | -$items = new ImmutableArray(glob(__DIR__ . '/Content/*.md')) |
| 50 | +$items = new ImmutableArray(glob(__DIR__ . '/content/*.md')) |
43 | 51 | ->reverse() |
44 | 52 | ->map(function (string $path) { |
45 | 53 | // … |
46 | 54 | }) |
47 | 55 | ->mapTo(BlogPost::class); |
48 | 56 | ``` |
49 | 57 |
|
50 | | -Note that you can also use the `arr()` helper function instead of manually creating an `ImmutableArray` object: |
51 | | - |
52 | | -```php |
53 | | -use function Tempest\Support\arr; |
54 | | - |
55 | | -$codeBlocks = arr(glob(__DIR__ . '/*.md')) |
56 | | - ->mapWithKeys(function (string $path) { |
57 | | - preg_match('/(\d+).md/', $path, $matches); |
58 | | - |
59 | | - $index = $matches[1]; |
60 | | - |
61 | | - yield "code{$index}" => $this->markdown->convert(file_get_contents($path)); |
62 | | - }) |
63 | | - ->toArray(); |
64 | | -``` |
65 | | - |
66 | | -You can check out the [full API on GitHub](https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Support/src/Arr/ManipulatesArray.php). |
67 | | - |
68 | | -## Enums |
69 | | - |
70 | | -The `IsEnumHelper` traits provides a bunch of useful methods that can be added to any enum: |
71 | | - |
72 | | -```php |
73 | | -use Tempest\Support\IsEnumHelper; |
74 | | - |
75 | | -enum MyEnum |
76 | | -{ |
77 | | - use IsEnumHelper; |
78 | | - |
79 | | - case FOO; |
80 | | - case BAR; |
81 | | -} |
82 | | - |
83 | | -MyEnum::FOO->is(MyEnum::BAR); |
84 | | -MyEnum::names(); |
85 | | - |
86 | | -// … |
87 | | -``` |
88 | | - |
89 | | -You can check out the [full API on GitHub](https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Support/src/IsEnumHelper.php). |
| 58 | +Note that you may use the `arr()` function as a shorthand to create an {b`\Tempest\Support\Arr\ImmutableArray`} instance. |
0 commit comments