-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathLazyValue.php
48 lines (41 loc) · 1004 Bytes
/
LazyValue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace Kirby\Toolkit;
use Closure;
/**
* Store a lazy value (safe from processing inside a closure)
* in this class wrapper to also protect it from being unwrapped
* by normal `Closure`/`is_callable()` checks
*
* @package Kirby Toolkit
* @author Nico Hoffmann <nico@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class LazyValue
{
public function __construct(
protected Closure $value
) {
}
/**
* Resolve the lazy value to its actual value
*/
public function resolve(mixed ...$args): mixed
{
return call_user_func_array($this->value, $args);
}
/**
* Unwrap a single value or an array of values
*/
public static function unwrap(mixed $data, mixed ...$args): mixed
{
if (is_array($data) === true) {
return A::map($data, fn ($value) => static::unwrap($value, $args));
}
if ($data instanceof static) {
return $data->resolve(...$args);
}
return $data;
}
}