frostaly /
var-exporter
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Frostaly\VarExporter\Encoders; |
||
| 6 | |||
| 7 | use Frostaly\VarExporter\Contracts\EncoderInterface; |
||
| 8 | use Frostaly\VarExporter\Encoder; |
||
| 9 | |||
| 10 | class ArrayEncoder implements EncoderInterface |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * {@inheritDoc} |
||
| 14 | */ |
||
| 15 | public function supports(mixed $value): bool |
||
| 16 | { |
||
| 17 | return is_array($value) || $value instanceof \ArrayAccess; |
||
| 18 | } |
||
| 19 | |||
| 20 | /** |
||
| 21 | * {@inheritDoc} |
||
| 22 | * |
||
| 23 | * @param array|\ArrayAccess $value |
||
| 24 | */ |
||
| 25 | public function encode(Encoder $encoder, mixed $value): array |
||
| 26 | { |
||
| 27 | $array = (array) $value; |
||
| 28 | $omitKey = array_is_list($array); |
||
| 29 | |||
| 30 | foreach ($array as $key => $value) { |
||
| 31 | $encoded[] = [...$encoder->encode($value), ',']; |
||
| 32 | $omitKey ?: array_unshift( |
||
| 33 | $encoded[array_key_last($encoded)], |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 34 | var_export($key, true) . ' => ', |
||
| 35 | ); |
||
| 36 | } |
||
| 37 | return isset($encoded) ? ['[', $encoded, ']'] : ['[]']; |
||
| 38 | } |
||
| 39 | } |
||
| 40 |