Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
CRAP
n/a
0 / 0
Celema\Boiler\unwrap
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5namespace Celema\Boiler;
6
7use Celema\Boiler\Proxy\ArrayProxy;
8use Celema\Boiler\Proxy\IteratorProxy;
9use Celema\Boiler\Proxy\ObjectProxy;
10use Celema\Boiler\Proxy\Proxy;
11use Celema\Boiler\Proxy\StringProxy;
12use Traversable;
13
14/**
15 * Returns the original of a value that Boiler wrapped for a template, for
16 * PHP code that gets values from templates. Unwraps the items of a plain
17 * array and returns other values as they are. Templates use `u()`.
18 *
19 * @api
20 *
21 * @template T
22 *
23 * @param T $value
24 *
25 * @return (T is StringProxy ? string : (T is ArrayProxy ? array<array-key, mixed> : (T is IteratorProxy ? Traversable<mixed, mixed> : (T is ObjectProxy ? object : (T is array ? array<key-of<T>, mixed> : T)))))
26 */
27function unwrap(mixed $value): mixed
28{
29    if ($value instanceof Proxy) {
30        /** @var ArrayProxy|IteratorProxy|ObjectProxy|StringProxy $value */
31        return $value->unwrap();
32    }
33
34    if (!is_array($value)) {
35        return $value;
36    }
37
38    $unwrapped = [];
39
40    /** @var mixed $item */
41    foreach ($value as $key => $item) {
42        /** @psalm-suppress MixedAssignment unwrapping returns mixed by design */
43        $unwrapped[$key] = unwrap($item);
44    }
45
46    return $unwrapped;
47}