cdn77/functions

PHP Functions

0.2.5 2024-09-12 12:06 UTC

README

GitHub Actions Code Coverage Downloads Packagist

Functions

absurd()

Function that should have never been called. Useful for default case in exhaustive matching.

assert_return()

Asserts the value via the expression and returns it.

It's useful when you want to assert inline so for example you can keep the arrow function in place.

It uses native assert() internally.

use function Cdn77\Functions\assert_return;

array_map(
    fn (mixed $value) => new RequiresInt(assert_return($value, is_int(...))),
    [1, 2, 3]
);

noop()

Does nothing. Useful e.g. for match expression that currently supports single-line expressions in blocks.

match ($val) {
  '1' => throw new Exception,
  '2' => foo(),
  default => noop(),
};

Ds

mapFromEntries()

Creates a map from an iterable of entries.

use function Cdn77\Functions\mapFromEntries;

$map = mapFromEntries([
  ['foo', 'bar'],
  ['baz', 'qux'],
]);

assert($map->get('foo') === 'bar');

Iterable

find()

Finds a value in an iterable.

use function Cdn77\Functions\Iterable\find;

$iterable = [0, 1, 2, 3];
$option = find($iterable, static fn (mixed $_, int $value) => $value < 2);

assert($option->unwrap() === 0);