Tempest in depth

Primitive utilities

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.

Overview

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.

Namespaced functions

Most utilities provided by Tempest have a function-based implementation under the Tempest\Support namespace. You may look at what is available on GitHub:

Tempest also provides a trait to work with enumerations, since a functional API is not useful in this case.

String utilities

Tempest provides string utilities through namespaced functions or a fluent, object-oriented API, which comes in an immutable and a mutable flavor.

Providing a string value, you may create an instance of Tempest\Support\Str\ImmutableString or Tempest\Support\Str\MutableString:

use Tempest\Support\Str\ImmutableString;

$slug = new ImmutableString('/blog/01-chasing-bugs-down-the-rabbit-hole/')
    ->stripEnd('/')
    ->afterLast('/')
    ->replaceRegex('/\d+-/', '')
    ->slug()
    ->toString();

Note that you may use the str() function as a shorthand to create an ImmutableString instance.

Array utilities

Tempest provides array utilities through namespaced functions or a fluent, object-oriented API, which comes in an immutable and a mutable flavor.

Providing a iterable value, you may create an instance of Tempest\Support\Arr\ImmutableArray or Tempest\Support\Arr\MutableArray:

use Tempest\Support\Arr\ImmutableArray;

$items = new ImmutableArray(glob(__DIR__ . '/content/*.md'))
    ->reverse()
    ->map(function (string $path) {
        // …
    })
    ->mapTo(BlogPost::class);

Note that you may use the arr() function as a shorthand to create an ImmutableArray instance.

Primitive utilities — Tempest — Tempest