Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| ObjectProxy | |
100.00% |
10 / 10 |
|
100.00% |
4 / 4 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| unwrap | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| in | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celema\Boiler\Proxy; |
| 6 | |
| 7 | use Celema\Boiler\Contract\Wrapper; |
| 8 | use Celema\Boiler\Exception\UnexpectedValueException; |
| 9 | use JsonSerializable; |
| 10 | use Override; |
| 11 | use Stringable; |
| 12 | use Traversable; |
| 13 | |
| 14 | /** |
| 15 | * @api |
| 16 | * |
| 17 | * @implements Proxy<object> |
| 18 | */ |
| 19 | final class ObjectProxy implements JsonSerializable, Proxy, Stringable |
| 20 | { |
| 21 | use ObjectAccess; |
| 22 | use RefusesJson; |
| 23 | |
| 24 | /** @param bool $safe whether the object's string conversion is safe HTML and skips escaping */ |
| 25 | public function __construct( |
| 26 | private readonly object $value, |
| 27 | private readonly Wrapper $wrapper, |
| 28 | private readonly bool $safe = false, |
| 29 | ) { |
| 30 | if ($this->value instanceof Traversable) { |
| 31 | throw new UnexpectedValueException('Traversable objects must be wrapped as iterator proxies'); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[Override] |
| 36 | public function unwrap(): object |
| 37 | { |
| 38 | return $this->value; |
| 39 | } |
| 40 | |
| 41 | #[Override] |
| 42 | public function is(mixed $other): bool |
| 43 | { |
| 44 | return $this->value === ($other instanceof Proxy ? $other->unwrap() : $other); |
| 45 | } |
| 46 | |
| 47 | /** @param ArrayProxy|array<array-key, mixed> $haystack */ |
| 48 | #[Override] |
| 49 | public function in(ArrayProxy|array $haystack): bool |
| 50 | { |
| 51 | if ($haystack instanceof ArrayProxy) { |
| 52 | $haystack = $haystack->unwrap(); |
| 53 | } |
| 54 | |
| 55 | /** @var mixed $item */ |
| 56 | foreach ($haystack as $item) { |
| 57 | if ($this->is($item)) { |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | } |