Test Failed
Pull Request — master (#34)
by Anatoly
04:34
created

UrlEncodedRequest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 48
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createBody() 0 13 2
A __construct() 0 17 3
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message\Request;
13
14
use Psr\Http\Message\StreamInterface;
15
use Sunrise\Http\Message\Exception\InvalidArgumentException;
16
use Sunrise\Http\Message\Request;
17
use Sunrise\Http\Message\Stream\PhpTempStream;
18
use TypeError;
19
20
use function gettype;
21
use function http_build_query;
22
use function is_array;
23
use function is_object;
24
use function sprintf;
25
26
use const PHP_QUERY_RFC1738;
27
use const PHP_QUERY_RFC3986;
28
29
/**
30
 * @since 3.1.0
31
 */
32
final class UrlEncodedRequest extends Request
33
{
34
    public const ENCODING_TYPE_RFC1738 = PHP_QUERY_RFC1738;
35
    public const ENCODING_TYPE_RFC3986 = PHP_QUERY_RFC3986;
36
37
    /**
38
     * @param mixed $uri
39
     * @param array<array-key, mixed>|object $data
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed>|object at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>|object.
Loading history...
40
     * @param self::ENCODING_TYPE_* $encodingType
41
     *
42
     * @throws InvalidArgumentException
43
     */
44
    public function __construct(string $method, $uri, $data, int $encodingType = self::ENCODING_TYPE_RFC1738)
45
    {
46
        /**
47
         * @psalm-suppress DocblockTypeContradiction
48
         * @phpstan-ignore-next-line
49
         */
50
        if (!is_array($data) && !is_object($data)) {
51
            throw new TypeError(sprintf(
52
                'Argument #3 ($data) must be of type string, %s given',
53
                gettype($data),
54
            ));
55
        }
56
57
        parent::__construct($method, $uri);
58
59
        $this->setBody(self::createBody($data, $encodingType));
60
        $this->setHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
61
    }
62
63
    /**
64
     * @param array<array-key, mixed>|object $data
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed>|object at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>|object.
Loading history...
65
     * @param self::ENCODING_TYPE_* $encodingType
66
     */
67
    private static function createBody($data, int $encodingType): StreamInterface
68
    {
69
        if ($data instanceof StreamInterface) {
70
            return $data;
71
        }
72
73
        $encodedData = http_build_query($data, '', null, $encodingType);
74
75
        $stream = new PhpTempStream('r+b');
76
        $stream->write($encodedData);
77
        $stream->rewind();
78
79
        return $stream;
80
    }
81
}
82