| 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 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param self::ENCODING_TYPE_* $encodingType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 5 |  |     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 | 5 |  |         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 | 5 |  |         parent::__construct($method, $uri); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 5 |  |         $this->setBody(self::createBody($data, $encodingType)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 5 |  |         $this->setHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param array<array-key, mixed>|object $data | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param self::ENCODING_TYPE_* $encodingType | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 5 |  |     private static function createBody($data, int $encodingType): StreamInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 5 |  |         if ($data instanceof StreamInterface) { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 1 |  |             return $data; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 4 |  |         $encodedData = http_build_query($data, '', '', $encodingType); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 4 |  |         $stream = new PhpTempStream('r+b'); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 4 |  |         $stream->write($encodedData); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 4 |  |         $stream->rewind(); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 4 |  |         return $stream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  |