Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MultipartDecoder
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 withParsedBody
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 withOptions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 decode
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 requiresCsrf
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Apie\Serializer\Encoders;
3
4use Apie\Core\ValueObjects\Utils;
5use Apie\Serializer\Interfaces\DecoderInterface;
6
7class MultipartDecoder implements DecoderInterface
8{
9    private array|null $parsedBody = null;
10
11    public function withParsedBody(null|array|object $parsedBody): DecoderInterface
12    {
13        $res = clone $this;
14        $res->parsedBody = $parsedBody === null ? null : (array) $parsedBody;
15        return $res;
16    }
17    
18    public function withOptions(string $options): DecoderInterface
19    {
20        return $this;
21    }
22
23    public function decode(string $input): string|int|float|bool|array|null
24    {
25        if (!is_array($this->parsedBody) || !array_key_exists('form', $this->parsedBody)) {
26            $form = [];
27        } else {
28            $form = Utils::toArray($this->parsedBody)['form'] ?? [];
29        }
30        if (is_string($form)) {
31            $data = json_decode($form, true);
32        } else {
33            $data = (new FormSubmitDecoder())->withParsedBody($this->parsedBody)->decode(http_build_query($this->parsedBody));
34        }
35        return $data;
36    }
37
38    public function requiresCsrf(): bool
39    {
40        return true;
41    }
42}