Wrap
in package
Neater closures!
don't use this.
This is now less of an abomination.
In the interest of reducing the millions of closures about the place and making code look kind of neater.
It's like this:
// Old way
$arrays = array_map(function($item) {
return $item->toArray();
}, $results);
// Better way (php7.4)
$arrays = array_map(fn($item) => $item->toArray(), $results);
// This way
$arrays = array_map(Wrap::method('toArray'), $results);
// And then maybe
$filtered = array_map(Wrap::item('active'), $arrays);
// Or even
$filtered = array_filter($results, Wrap::property('active'));
// Lastly
$mapped = array_map(Wrap::construct(Thing::class), $results);
Table of Contents
Methods
- construct() : callable
- Wrap a constructor.
- instanceOf() : callable
- Wrap instanceof.
- item() : callable
- Create a wrapper that gets the key of an array.
- method() : callable
- Create a wrapper that calls the method of an object.
- property() : callable
- Create a wrapper that gets te property of an object.
Methods
construct()
Wrap a constructor.
public
static construct(string $name) : callable
fn(...$args) => new $name(...$args);
Parameters
- $name : string
-
a class name
Tags
Return values
callable —(...$args) => object
instanceOf()
Wrap instanceof.
public
static instanceOf(string $name) : callable
fn($item) => $item instanceof $name;
Parameters
- $name : string
-
a class name
Return values
callable —($item) => bool
item()
Create a wrapper that gets the key of an array.
public
static item(string|int $key) : callable
fn($item) => $item[$key];
Parameters
- $key : string|int
Return values
callable —(array) => mixed
method()
Create a wrapper that calls the method of an object.
public
static method(string $name, array<string|int, mixed> ...$args) : callable
fn($item) => $item->$name(...$args);
Parameters
- $name : string
- $args : array<string|int, mixed>
Return values
callable —(object) => mixed
property()
Create a wrapper that gets te property of an object.
public
static property(string $name) : callable
fn($item) => $item->$name;
Parameters
- $name : string
Return values
callable —(object) => mixed