| 1 | <?php |
||
| 6 | abstract class Body |
||
| 7 | { |
||
| 8 | /** |
||
| 9 | * @param string $inputStream |
||
| 10 | * @return Body|JsonBody |
||
| 11 | * @throws UnsupportedRequestBodyException |
||
| 12 | */ |
||
| 13 | public static function fromSuperGlobals(string $inputStream = 'php://input'): Body |
||
| 14 | { |
||
| 15 | $content = file_get_contents($inputStream); |
||
| 16 | if (empty($content)) { |
||
| 17 | return new EmptyBody(); |
||
| 18 | } |
||
| 19 | |||
| 20 | if (!isset($_SERVER['CONTENT_TYPE']) || empty($_SERVER['CONTENT_TYPE'])) { |
||
| 21 | return new RawBody($content); |
||
| 22 | } |
||
| 23 | |||
| 24 | switch ($_SERVER['CONTENT_TYPE']) { |
||
| 25 | case ContentType::JSON: |
||
| 26 | return new JsonBody($content); |
||
| 27 | } |
||
| 28 | throw new UnsupportedRequestBodyException(); |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @return bool |
||
| 33 | */ |
||
| 34 | abstract public function isJson(): bool; |
||
| 35 | } |
||
| 36 |