1616use JetBrains \PhpStorm \Pure ;
1717use LogicException ;
1818use Override ;
19+ use RequestParseBodyException ;
1920use stdClass ;
2021use UnexpectedValueException ;
2122
@@ -35,7 +36,11 @@ class Request extends Message implements RequestInterface
3536 /**
3637 * @var array<mixed>|null
3738 */
38- protected ?array $ parsedBody = null ;
39+ protected ?array $ parsedBodyTexts = null ;
40+ /**
41+ * @var array<mixed>|null
42+ */
43+ public ?array $ parsedBodyFiles = null ;
3944 /**
4045 * @var array<string,mixed>|null
4146 */
@@ -529,32 +534,56 @@ protected function parseDigestAuth(string $attributes) : array
529534 *
530535 * @see Request::filterInput()
531536 *
537+ * @throws RequestParseBodyException
538+ *
532539 * @return array<mixed>|mixed|string|null
533540 */
534541 public function getParsedBody (
535542 ?string $ name = null ,
536543 ?int $ filter = null ,
537544 array | int $ filterOptions = 0
538545 ) : mixed {
546+ // TODO: If enable_post_data_reading ini-setting is disabled, getPost will always be empty!
539547 if ($ this ->getMethod () === Method::POST ) {
540548 return $ this ->getPost ($ name , $ filter , $ filterOptions );
541549 }
542- if ($ this ->parsedBody === null ) {
543- // TODO: On PHP 8.4 use isForm() and request_parse_body()
544- // [$this->parsedBody, $_FILES] = request_parse_body();
545- // Add methods {get,set}ParseBodyOptions(?array $options = null)
546- $ this ->isFormUrlEncoded ()
547- ? \parse_str ($ this ->getBody (), $ this ->parsedBody )
548- : $ this ->parsedBody = [];
550+ if ($ this ->parsedBodyTexts === null ) {
551+ $ this ->parseBody ($ this ->getParseBodyOptions ());
549552 }
550553 $ variable = $ name === null
551- ? $ this ->parsedBody
552- : ArraySimple::value ($ name , $ this ->parsedBody );
554+ ? $ this ->parsedBodyTexts
555+ : ArraySimple::value ($ name , $ this ->parsedBodyTexts );
553556 return $ filter !== null
554557 ? \filter_var ($ variable , $ filter , $ filterOptions )
555558 : $ variable ;
556559 }
557560
561+ /**
562+ * @param array<string,mixed>|null $options
563+ *
564+ * @throws RequestParseBodyException
565+ * @throws LogicException
566+ *
567+ * @return static
568+ */
569+ public function parseBody (?array $ options = null ) : static
570+ {
571+ if ($ this ->isParsedBody ()) {
572+ throw new LogicException ('Parse error: the request body has already been parsed ' );
573+ }
574+ $ options ??= $ this ->getParseBodyOptions ();
575+ [
576+ $ this ->parsedBodyTexts ,
577+ $ this ->parsedBodyFiles ,
578+ ] = \request_parse_body ($ options );
579+ return $ this ;
580+ }
581+
582+ public function isParsedBody () : bool
583+ {
584+ return isset ($ this ->parsedBodyTexts );
585+ }
586+
558587 /**
559588 * @return array<string,mixed>|null
560589 */
0 commit comments